2

I need to check that a transacions file is published on a remote host. There is no transactions code published for today the 31 of December. I know that for certain. There is one for yesterday. However no matter which date I use, the 30 or the 31, the return code is the same.

What I am looking to check is the return status of ssh which I thought should be return status of the remotely executed command I thought that the 'if statement' would exit with a 0 if the 'ls -ltr' worked and something greater than zero, or false if the 'ls -ltr' did not work. I would think that the 'if' statement would look at the return status of the shh, which should be the return status of the remotely executed command - or the 'ls -ltr'

How do i get thie to work?

this is for the 30 - which is on the remote host, and is successful

casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr  /home/DropBox/transactions_20141230.csv"
-rw-r--r--   1 rr_prd    2233047 Dec 31 07:26 /home/DropBox/transactions_20141230.csv

and this is for the 31 which there is no report yet for the 31, so it is unsuccessful

casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr  /home/DropBox/transactions_20141231.csv"
/home/DropBox/transactions_20141231.csv not found
casper@host:/scripts$

I flip the date with the comment out hashtag

casper@host:/scripts$ ssh -q -T user@capserbox "ls -ltr  /home/DropBox/transactions_20141231.csv"
/home/DropBox/transactions_20141231.csv not found
casper@host:/scripts$



#!/bin/bash
today=$(/bin/date +%Y%m%d)
#today="20141230"
echo $today
if ssh -q -T user@capserbox "ls -ltr  /home/DropBox/transactions_$today.csv" > /dev/null 2>&1 ;
    then
       echo "this worked"
    else
       echo "this did not work"
fi

However - when i use the script for both dates - it is successful, when for the 31, it really should return "this did not work"

casper@host:/scripts$
casper@host:/scripts$ vim offshore_check
casper@host:/scripts$ ./offshore_check
20141230
this worked
casper@host:/scripts$ vim offshore_check
casper@host:/scripts$ ./offshore_check
20141231
this worked
casper@host:/scripts$
casper@host:/scripts$

3 Answers 3

2
file="/home/DropBox/transactions_20141231.csv"
ssh -q -T user@capserbox "test -e $file"
ret="$?"
case $ret in
  0)
    echo "$file exists"
    ;;
  1)
    echo "$file does not exist"
    ;;
  *)
    echo "other problem"
    ;;
esac
Sign up to request clarification or add additional context in comments.

Comments

0

The "if" construct that you're using should work, and it does work for me:

$ if ssh -q -T localhost "ls -ltr /does/not/exist"; then echo succeeded; else echo failed; fi
Password:
ls: /does/not/exist: No such file or directory
failed

I notice that the error printed by your ls program is worded differently than the error that I got (and I tried two different systems). "file not found" in your case, "ls: file: No such file or directory" in mine. My suspicion is that the ls command which you're invoking here isn't the typical modern Unix command. You may be running a nonstandard version, a non-Unix version, or something very old, and it may not actually be exiting with a non-zero exit code for this particular error.

1 Comment

it is an older offshore machine - - SunOS 5.8 Generic_117350-35 - build-version 020
0

Similar issues have been discussed here many times, see scp return code discussion 1

and 300+ answers when searching for scp errors

but to give you a working answer, consider trying this

 if 
     ssh -q -T user@capserbox "ls -ltr  /home/DropBox/transactions_${today}.csv" \
     | grep -q "transactions_${today}\.csv" ;
 then
       echo "this worked" 
 else
       echo "nope"
 fi

Sorry, I don't have any way to test this.

IHTH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.