Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

#!/usr/bin/expect

spawn ssh sduser@172.0.0.2
expect "123"
send "123"
interact
destPath='/archive/Input/ERICIN/AIR/EMG'
dayStamp=`(date --date='1 day ago' '+%Y%m%d')`
if [ -d $destPath ]
then
     cd $destPath
     `cat 'emg_audit_1_'$dayStamp.csv|grep 'ACTPSO_NE_'$dayStamp|wc -l > /home/stsuser/eastregioncount/NEremote$dayStamp.txt`
else
        echo "ERROR: Path $destPath Does not Exists."
fi
share|improve this question
1  
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
add comment

1 Answer

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 172.0.0.2. 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):

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

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
add comment

Your Answer

 
discard

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.