Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Below is my script to take count for files which are present in remote server. But it is not working. Please help me.


spawn ssh sduser@
expect "123"
send "123"
dayStamp=`(date --date='1 day ago' '+%Y%m%d')`
if [ -d $destPath ]
     cd $destPath
     `cat 'emg_audit_1_'$dayStamp.csv|grep 'ACTPSO_NE_'$dayStamp|wc -l > /home/stsuser/eastregioncount/NEremote$dayStamp.txt`
        echo "ERROR: Path $destPath Does not Exists."
share|improve this question
why do you have backticks (`) around the cat command? – jofel Apr 30 '13 at 7:51
Why are you making an extra subshell inside of the backticks on line 8? – Evan Teitelman Apr 30 '13 at 22:53

To get this to work I would suggest two major changes:

  1. use public/private keypairs
  2. send commands on the commandline to ssh

You can use ssh-keygen to generate a new private-public key pair and use ssh-copy-id to install the public key you generate to the new account on After that you don't have to go through the expect "123" sequence anymore.

That also enables you to call ssh with the command on the commandline more easily (assuming the timezondes on both machines are the same):

dayStamp=`(date --date='1 day ago' '+%Y%m%d')`
ssh sduser@ "cd $destPath; cat 'emg_audit_1_'$dayStamp.csv| grep 'ACTPSO_NE_'$dayStamp| wc -l > /home/stsuser/eastregioncount/NEremote$dayStamp.txt"
if [ $? -ne 0 ] 
    echo "ERROR"

The cd command already will tell you that $destPath does not exists, no need to echo that yourself. But you can test the exit code and do something more meaningful (than the echo "Error") if necessary.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.