LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   scripting - return value of remote commands? (https://www.linuxquestions.org/questions/linux-software-2/scripting-return-value-of-remote-commands-759171/)

siloko 10-02-2009 05:40 AM

scripting - return value of remote commands?
 
Hi,

I'm a bash script newbie . . . I am trying to capture the result of a command executed remotely. I realise this will not work but I'll post what I have just so you know where I'm at!

MOUNTED=`ssh $HOST_IP "mount|grep /dev/sdb1"`;

What I want to capture in the variable MOUNTED is the return value of "mount|grep /dev/sdb1" which is executed on the remote machine at $HOST_IP, but I have no idea how to proceed from the above which obviously doesn't work . . .

Any help or pointers would be great!

Thanks

S

lutusp 10-02-2009 06:12 AM

Quote:

Originally Posted by siloko (Post 3704848)
Hi,

I'm a bash script newbie . . . I am trying to capture the result of a command executed remotely. I realise this will not work but I'll post what I have just so you know where I'm at!

MOUNTED=`ssh $HOST_IP "mount|grep /dev/sdb1"`;

What I want to capture in the variable MOUNTED is the return value of "mount|grep /dev/sdb1" which is executed on the remote machine at $HOST_IP, but I have no idea how to proceed from the above which obviously doesn't work . . .

Any help or pointers would be great!

Thanks

S

By 'return value" do you mean the command's exit code?

Try this:

Code:

# MOUNTED=`ssh $HOST_IP "mount|grep -q /dev/sdb1;echo $?"`
# echo $MOUNTED

This will work because "echo $?" prints the exit code of the most recently executed command. But remember that "0" represents success and a nonzero code represents an error.

But maybe you should actually say what you want. It's not clear from your message.

siloko 10-02-2009 06:28 AM

Quote:

Originally Posted by lutusp (Post 3704877)
By 'return value" do you mean the command's exit code?

But maybe you should actually say what you want. It's not clear from your message.

Thanks for your reply

Ok, what I want to do is check to see if /dev/sdb1 is mounted on the remote machine. If I have

MOUNT=mount|grep -q /dev/sdb1

I can check to see if MOUNT is empty - if so the drive is not mounted. I wanted to repeat the procedure on a remote machine. I guess your solution doesn't work because the exit status is zero for all calls to mount, even though it IS what I asked for!!

Again any help would be appreciated - perhaps I'm barking up the wrong tree!

Cheers

S

Guttorm 10-02-2009 06:35 AM

Hi

ssh also passes the return codes from the remote computer to the host. So you can use $? like usual.

ssh $HOST_IP "mount |grep /dev/sda1" &>/dev/null
echo $?
0
ssh $HOST_IP "mount |grep /dev/sdz1" &>/dev/null
echo $?
1

siloko 10-02-2009 06:53 AM

That's it - Thanks!


All times are GMT -5. The time now is 01:00 AM.