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


spawn ssh [email protected]
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."
  • 1
    why do you have backticks (`) around the cat command?
    – jofel
    Apr 30, 2013 at 7:51
  • Why are you making an extra subshell inside of the backticks on line 8?
    – user26112
    Apr 30, 2013 at 22:53

1 Answer 1


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 [email protected] "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.

You must log in to answer this question.

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