cancel
Showing results for 
Search instead for 
Did you mean: 

script regarding ssh

 
Highlighted
Frequent Advisor

Re: script regarding ssh

Hi dennis,

 

Thnx for the script. I have tested a test script based on above script and it is working successfully. Just one more concern i m having. I am providing the test script which i have tested:

 

#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@austsapdr ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
else
:  echo "Some files not matched:"
:  comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files

 

>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh

If the exit status is not 0 then the script will not run. But if the exit status is zero and the script when runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this??

 

Regards,

Highlighted

Re: script regarding ssh

>Thnx for the script. I have tested a test script based on above script and it is working successfully.

 

(If you are happy, please click on the Kudos! star of the posts that helped.)

 

>sh /home/rtns/x/a.sh

 

There is no need to use "sh" on this line.  Just make sure your script is executable and has "#!..." on the first line.


>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh

>if the exit status is zero and the script runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this?

 

You can either put that ssh into a.sh (but it would have to know about serverB) or add that ssh right after.

Do you also need to check the exit status of a.sh?

Highlighted
Frequent Advisor

Re: script regarding ssh

>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh

 

>>Do you also need to check the exit status of a.sh?

 

No. I dont need the exit status of a.sh. I am concern of only that if a.sh runs (i.e. status of [ $? -eq 0 ] is zero) then only ssh is to do done to serverB to run a script there and if a.sh do not runs (i.e. status of [ $? -eq 0 ] is non-zero), then ssh should not be done to serverB.

 

Regards,

Highlighted

Re: script regarding ssh

>I am concern of only that if a.sh runs then only ssh is to do done to serverB to run a script there ...

 

That "if" will do that.  You just need to add that ssh in the "then" block after a.sh.

Highlighted
Frequent Advisor

Re: script regarding ssh

I tried adding another "then" as below :

 

#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh; then

ssh root@serverB sh /home/rtns/testdir1/testscript
else
:  echo "Some files not matched:"
:  comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files

 

But it given the following error :

 

root@serverA:/home/rtns>./testscript2 (testscript2 is the above script)
Password:
Password:
./testscript2[5]: Syntax error at line 7 : `then' is not expected.

 

Regards,

 

Highlighted

Re: script regarding ssh

>I tried adding another "then" as below:

 

You don't need another one:  (Nor should you need those "sh" commands.)

if [ $? -eq 0 ]; then
   /home/rtns/x/a.sh

   ssh root@serverB /home/rtns/testdir1/testscript
else
:  echo "Some files not matched:"
:  comm -3 serverA_files serverB_files
fi

Highlighted
Frequent Advisor

Re: script regarding ssh

>>ssh root@serverB /home/rtns/testdir1/testscript

The above script is not running on serverB although it is not giving any error.

 

Regards,

Highlighted

Re: script regarding ssh

>The above script is not running on serverB although it is not giving any error.

 

Add the following (after the #! line):  set -x

 

This will trace the commands in the script.

You could also do it in the original script on serverA to make sure you are getting to that ssh.

Highlighted
Frequent Advisor

Re: script regarding ssh

Hi Dennis,

 

The script i am running on serverB through ssh is as below. I used set -x option also in the script.

 

#!/usr/bin/sh
set -x
for i in abc*.xyz
do
if  (test -f  "$i")
then
newname=${i#abc}
/usr/bin/mv $i /home/rtns/testdir2/$newname
fi
done

 

But it is giving the output as:

+ test -f abc*.xyz

 

I run the above script on serverB itself and it is running successfully. But using ssh to run this script giving the above message.

 

Regards,

Highlighted

Re: script regarding ssh

>The script I am running on serverB through ssh is as below.
>for i in abc*.xyz

 

Are you running this script in the right directory?  Otherwise with ssh, it would default to the home directory.


>if  (test -f  "$i")

 

I wouldn't use test(1).  I would just use:

   if [ -f "$i" ]; then


>But it is giving the output as: + test -f abc*.xyz
>I run the above script on serverB itself and it is running successfully. But using ssh to run this script giving the above message.

 

As I guessed above, it depends on what directory you start in.