Skip to main content
2 of 2
Correction attempt with heredocs
Roman Rdgz
  • 135
  • 1
  • 6

Different find command result when launched from terminal and from bash script

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:

LOCAL_FOLDER=/home/myuser/myfolder/data/ REMOTE_FOLDER=/var/www/sites/all/data/ RSA_PRIVATE_KEY=/home/myuser/.ssh/id_rsa_Test REMOTE_IP=XXX.XXX.XXX.XXX REMOTE_USER=my-remote-user 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 ) 
Roman Rdgz
  • 135
  • 1
  • 6