LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-02-2009, 05:40 AM   #1
siloko
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
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
 
Old 10-02-2009, 06:12 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by siloko View Post
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.
 
Old 10-02-2009, 06:28 AM   #3
siloko
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
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
 
Old 10-02-2009, 06:35 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,373

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
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
 
Old 10-02-2009, 06:53 AM   #5
siloko
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
That's it - Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ping Return Remote Host Information qwerty_tele Linux - Security 3 11-24-2008 11:05 AM
checking for return status between multiple commands vickylife Linux - Newbie 3 11-19-2008 12:33 PM
Scripting Remote Logon - Starting Remote Gnome Desktop klmukp Linux - Newbie 1 09-21-2008 12:13 AM
c preprocessor commands & return types(?) kpachopoulos Programming 4 12-06-2005 09:18 AM
Pause between commands when scripting jwijesundra Linux - General 1 07-03-2004 02:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration