smbclient - check if 'cd' is sucessful?

Hi,

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?

Thx a lot in advance.

Check last command status by $?

echo "$?"

0 - Success
2 - Fail
1 Like

Only 2 is not failure. Check here.

1 Like

Unfortunately I don't have an answer at hand, as a failing cd will complain like

cd \Josef\: NT_STATUS_OBJECT_NAME_NOT_FOUND

but evaluating this within a script is difficult.
Why don't you smbmount that share and use unix/linux cp to move files over?

1 Like

We can also check for the presence of directory ..

if [[ -d $dir_name ]]
then
#do what you want here..
fi
1 Like

Hi,

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 ----------

thx for this hint. I'll check it

Give dir_name as path of the directory you want to check...

if [[ -d $dir_name ]] 
then 
#do what you want here..
echo "Pass"
fi

I did...same result/error-message :frowning:

	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
`

It so weird that if command is not working there...

and i am doubtful about working of while...

still try this...

cd $dir_name
var=$(echo $?)
echo $var
while [ "$var" == 0 ]; do
echo "Pass" 
var=1;
done

Hope this should work for you..:slight_smile:

1 Like

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:

1 Like

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).

1 Like

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.

1 Like

Thanks all of you for your help and hints. I think I'll give smbmount a try :wink:

OK, pls report back your results.

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
	`

It's not beautiful, but it works :wink:

Surprising, as it is part of the samba suite.

Does it? You redirect stderr to /dev/null, so what do you grep on?

You're right... I copied the code from wrong working version of my script.
Actually I redirect stderr to $LogFile.

@FreddyDaKing
The smbmount command is Operating System dependent.

Too much guesswork when you don't post what Operating System and version you have.