Home > Software engineering >  How to know if file exsits ina Samba share
How to know if file exsits ina Samba share

Time:12-11

I wrote a shellscript as follows, to check for a file in a samba share:

 date_gen=$(date --date="3 days ago"  "%-Y%m%d")
 fileName=${date_gen}"_Combined Reg Report.xlsx"
 if [ ! -f smb://nfs/carboard/"${fileName}" -U  ]
 then
    echo "File does not exist in Bash"
else
  echo ${fileName}
fi
exit 1

Can someone please help me what is wrong with this, I am always getting "File does not exist in Bash". File is there in the folder.

Thanks, Art

CodePudding user response:

Checking the existence of a file with smbclient:

if smbclient -A smbauth.conf '//nfs/carboard' -c "ls $fileName" > /dev/null 2>&1
then
    echo the file exists
else
    echo the file is not there
fi

where smbauth.conf is a file where you store your credentials in this format:

username=myuser
password=mypassword
DOMAIN=MYDOMAIN

What I don't know is how escaping works with smbclient... You might not need it if $fileName is simple enough.

CodePudding user response:

You should check if it's mounted and then check for the file

if mount | grep -q /nfs/cardboard
then
    if [[ ! -f /nfs/cardboard/"${fileName}" ]]
    then
        ...
    fi
else
    echo "not mounted"
fi
  • Related