This is probably a concept error on my side, but I am a bit lost at this point.

I want to create an script (to be later launched by Nagios, but that's a different story) so that I check the timestamp of the latest created file on a certain folder (including subfolders) in both local and a remote host, so that I can see if the rsync processes we have running are working fine.

So, I researched and came up with the following command:

find ${LOCAL_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1 | { read -a array ; echo ${array[0]} ; }

And its ssh version for the remote host and folder:

ssh -i ${RSA_PRIVATE_KEY} ${REMOTE_USER}@${REMOTE_IP} 'find ${REMOTE_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1 | { read -a array ; echo ${array[0]} ; }

If I just launch these commands in a terminal with my normal session user (not an admin account), they work fine and I get a UNIX timestamp.

So, I created a bash script replicating both commands, substracting the results, so that I can warn if the difference is greater than N seconds. But then, it doesn't work. It doesn't seem a code bug, it just seems that the ssh command returns a different value when launched from the bash script. A VERY different value, with years of difference.

IMHO, it is not getting access through ssh, and it is somehow getting access to an empty folder just including a hidden folder (such as "./ssh/" or something like that). But I don't know why.

The script has the following code:


last_local_timestamp=$(find ${LOCAL_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1 | { read -a array ; echo ${array[0]} ; })
last_remote_timestamp=$(ssh -i ${RSA_PRIVATE_KEY} ${REMOTE_USER}@${REMOTE_IP} 'find ${REMOTE_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1 | { read -a array ; echo ${array[0]} ; }')
test=$(ssh -i ${RSA_PRIVATE_KEY} ${REMOTE_USER}@${REMOTE_IP} 'find ${REMOTE_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1 ')
echo $test
time_diff=$((${last_local_timestamp%.*} - ${last_remote_timestamp%.*}))
echo LOCAL: ${last_local_timestamp%.*}
echo REMOTE: ${last_remote_timestamp%.*}
echo DIFF: ${time_diff}

Any idea of what's going on?

EDIT: Correction attempt with heredocs below:

last_local_timestamp=$(find ${LOCAL_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | sed -e 's/^[^ ]* //')
last_remote_timestamp=$(ssh -i ${RSA_PRIVATE_KEY} ${REMOTE_USER}@${REMOTE_IP} <<- EOF
    find ${REMOTE_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | sed -e 's/^[^ ]* //'
    EOF )
test=$(ssh -i ${RSA_PRIVATE_KEY} ${REMOTE_USER}@${REMOTE_IP} <<- EOF
    find ${REMOTE_FOLDER} -type f -printf "%C@ %p\n" | sort -rn | head -n 1
    EOF )

1 Answer 1

  1. Why are you piping the output of head -n 1 into read -a array and then printing the first element of the array? try just | sed -e 's/^[^ ]* //' to strip everything before the first space instead. i.e. strip the timestamp from the beginning of the line).

  2. Quote your variables. using curly braces is not a substitute for quoting, their only purpose is to disambiguate a variable name from adjacent characters that would otherwise be interpreted as being part of the variable's name.

  3. ${REMOTE_FOLDER} inside single-quotes (i.e. inside the ssh command) is a literal string, it does not evaluate to the value of $REMOTE_FOLDER. Quoting is always a PITA in shell, especially when you have (or need) multiple levels of nested quotes. Try using a heredoc instead of quotes, it'll be easier to work with.

  • Never heard abuou heredocs before. I tried to apply it (see edited answer) and also to change tot he sed cmd tou suggested. Unsure of why should I quote my variables if they do not include whitespaces although. Could you review it? I have never used heredoc before
    – Roman Rdgz
    Jul 29, 2021 at 9:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .