Problem with ssh and checking if file exists

Hi All,

I am facing a problem while checking for existence of file over ssh !

Basically, i want to ssh and check if file exists.. If file exists return 1. If file does not exits return 0 (or any value)

I am using the below code

file_avail=`ssh username@host "if [[ -f directory/structure/Daily Report.xls ]]; then exit 1; else echo exit 0; fi"`

Please note that the file name here has spaces in it !

But i am not able to retrieve the status from ssh.

Please help !!

Try:

file_avail=`ssh username@host 'if [[ -f "directory/structure/Daily Report.xls" ]]; then exit 1; else echo exit 0; fi'`

Hi Klashxx,

I tried ur option.

file_avail=`ssh username@host 'if [[ -f "directory/structure/Daily Report.xls" ]]; then exit 1; else exit 0; fi'`

Still i am getting the output for file_avail as NULL

+ file_avail=''

Pls let me know what i am missing here!

ok , use:

file_avail=`ssh username@host 'if [[ -f "directory/structure/Daily Report.xls" ]]; then echo 1; else echo 0; fi'`

Personally , i prefer:

ssh username@host 'if [[ -f "Daily Report.xls" ]]; then exit 1; else exit 0; fi'
file_avail=$?
1 Like

Thanks a lot for helping out on this Klashxx !

It worked !!!

That code is rather redundant, this would do:

ssh username@host '[ -f "directory/structure/Daily Report.xls" ]'
file_avail=$?
2 Likes

So are we saying that SSH returns the exit code of the remote command? Remote-shell never did. You got the return code for "Did remote shell connect" pretty much, so we ended up with a fudge such that we run the remote shell commands with

rsh ${REMOTEHOST} "$COMMAND $ARGS ; echo \\"Final_RC=\\\$?\\"\""> logfile 2>&1

We then extract the one line with "Final_RC" and work out if the remote command ran okay.

It seems much better with SSH, thanks goodness! Roll on the upgrades here that make SSH available.

It's a shame we still have so much AIX 4.3.3, 5.1 and HP-UX 11.11 !! :o

Robin

Absolutely. ssh is quite sophisticated. It even has separate stdout/stderr streams.

:eek: I feel so ancient. :o

Guys, another quick question..

Is it possible to parameterize the directory structure path before passing it to ssh command?

Like Below:

SHARE_PATH=directory/structure
 
file_avail=`ssh username@host 'if [[ -f "$SHARE_PATH/Daily Report.xls" ]]; then echo 1; else echo 0; fi'`

Apparently the variable $SHARE_PATH is not being read in ssh !

---------- Post updated 01-10-14 at 01:11 AM ---------- Previous update was 01-09-14 at 11:24 PM ----------

Hi, figured out to pass variables to ssh command [Here my file name has spaces]

Below code worked :

file_name="Daily Report.xls"

file_avail=`ssh username@host "if [[ -f $SHARE_PATH/'$file_name' ]]; then echo 1; else echo 0; fi"`

Yes, you need the correct quotes to prevent your local shell from expanding it before executing the command that opens SSH and invokes the arguments as remote commands.

Robin