I've a question regarding a smbclient-Command in my shell script.
Here my relevant script part:
fileattr=`/opt/samba/bin/smbclient --authentication-file=$AUTH_FILE //$SMB_HOST/$SMB_SHARE 2> /dev/null
prompt off
cd $SMB_DIR
put $LOC_FILE $SMB_FILE
dir /$SMB_DIR/$SMB_FILE
exit
`
Unfortunately if $SMB_DIR does not exists the file is written to the root directory of share. Is there any possibility to avoid this? Is it possible to stop the file copy if "cd $SMB_DIR" is not successful?
thanks for your help, I already had the same idea but apparently I can not use if-statements within smbclient command. If I do so, I get the following error message:
if: command not found
then: command not found
#do: command not found
fi: command not found
---------- Post updated at 06:27 AM ---------- Previous update was at 06:25 AM ----------
fileattr=`/opt/samba/bin/smbclient --authentication-file=$AUTH_FILE //$SMB_HOST/$SMB_SHARE 2> /dev/null
prompt off
if [[ -d $SMB_DIR ]]
then
cd $SMB_DIR
put $LOC_FILE $SMB_FILE
dir /$SMB_DIR/$SMB_FILE
fi
exit
`
Did this script ever work? AFAIK smbclient needs commands from stdin, so a "here document" might work. Anyhow, the shell is NOT reading those lines after the smbclient command, and thus no variable expansion nor an if evaluation should be expected. Have you tried to put the smb commands into a file and source it to smbclient?
I have to correct myself: some expansion may be done in (bash!) here documents:
Please always post what Operating System and version you are running and what Shell you use. There is so much variation.
Please also post the Operating System and version of the remote (Microsoft?) server.
(Deleted code suggestion because I fell into the same trap as other posters. The Directory concerned is on a remote server and not visible to basic unix commands.)
Afterthought.
Could do this in two runs of smbclient.
1) Just issue a cd and check what happens
2) If successful, run the full script (obviously repeating the cd but knowing that it will work).
This is where my smbmount proposal comes into play. You smbmount the remote resource and - voila - all your unix commands will work.
This is what I first thought of, but I'm not sure the result of cd will be conveyed correctly. And, it's quite clumsy, ain't it? He/she still will need a mechanism to expand the varables with his filenames etc.
Update: exit code will NOT reflect CD failure. You will have to write an error file and analyse it.
Unfortunately smbmount is not available at the company I want to run my script.
However I changed my script regarding the workaround methyl and RudiC suggested:
fileattr=`/opt/samba/bin/smbclient --authentication-file=$AUTH_FILE //$SMB_HOST/$SMB_SHARE 2> /dev/null <<END
prompt off
cd $SMB_DIR
exit
END
`
grep -c 'NT_STATUS_OBJECT_PATH_NOT_FOUND' $LogFile
if [ $? != 0 ] ; then
exit 1
fi
fileattr=`/opt/samba/bin/smbclient --authentication-file=$AUTH_FILE //$SMB_HOST/$SMB_SHARE 2> /dev/null <<END
prompt off
cd $SMB_DIR
put $LOC_FILE $SMB_FILE
dir /$SMB_DIR/$SMB_FILE
exit
END
`