2

I made this script which checks to make sure that an offshore script is created. I works ok &emdash; I put in a function to make it a little cleaner. But I don't think that the function works because it keeps on printing out the same time, in seconds.

#!/bin/bash
#set -x
check_offshore ()
{
    local RESULTS
    RESULTS=$(ssh -q -T casper@mybox "ls -ltr  /come/and/play/with/us/danny/DropBox| grep foreverr_and_$today.csv")
    rc=$?
}

today=$(/bin/date +%Y%m%d)
time=$(/bin/date +"%T.%3N")
iterate=0
while [ $iterate -le 5 ]
do
     check_offshore
     if [[ $rc != 0 ]] ; then
        echo "$time foreverr_and_$today.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox"
        fi
     sleep 5
     iterate=$((iterate+1 ))
     done

This is the log that it creates &emdash; the log time never changes. It stays the same, forever and ever and ever until the script stops.

17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox
17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox
17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox
17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox
17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox
17:42:28.380 foreverr_and_20150102.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox

What's up and how do I fix it?

2
  • 3
    Since you don't set time=$(...) inside the loop, how do you expect it to change? Commented Jan 2, 2015 at 23:13
  • 1
    If you're using Bash, you might prefer for ((iterate = 0; iterate <= 5; iterate++)) to control the loop. Commented Jan 2, 2015 at 23:14

1 Answer 1

4

Note that the problem has nothing to do with the function as such; the trouble is in the loop and its immediate surroundings.

Since you don't set time=$(...) inside the loop, how do you expect it to change?

Revise the loop so that the time is re-evaluated on each iteration. Since the date can change if you run this script after something like 23:59:30, you might also want to set the date on each iteration. You might also use a Bash-specific loop control mechanism:

for ((iterate = 0; iterate <= 5; iterate++))
do
    today=$(/bin/date +%Y%m%d)
    time=$(/bin/date +"%T.%3N")
    check_offshore
    if [[ $rc != 0 ]] ; then
       echo "$time foreverr_and_$today.csv is not present in casper@mybox:/come/and/play/with/us/danny/DropBox"
    fi
    sleep 5
done

You may prefer to stick with the original date, in which case you can continue to set today once outside the loop. Note that your logging becomes a little dubious if things go from 23:59:56.234 to 00:00:02.197 because things took a while just before midnight. Whether that matters to you or not is a judgement call you'll have to make. I recommend being unambiguous with the date+time in the log file; it is easier to diagnose what happened when several days later.

Sign up to request clarification or add additional context in comments.

1 Comment

timmy () { time=$(/bin/date +"%Y%m%d-%T.%3N") } added a subroutine with the date and time - thank you

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.