LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2007, 07:07 AM   #1
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Rep: Reputation: 15
C shell $? status returns output


Hello All,

#!/depot/local/bin/perl5.8 -w

my $stat;
$stat=`rsh linx01 'ls ;echo \$status'`;
print $stat;


$stat variable contains the ls command returned output (i mean current directory ls execution output present in the $stat). $stat is being initialized from "echo $status".

I need only the execution status not the ls command output in the $status shell variable.

can anyone help me to get execution status of the ls command in the $status variable and ls command output should display in the screen.

Thanks
dayalan
 
Old 09-20-2007, 08:23 AM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Interesting that your title references $?, but your script doesn't.

Use:

$stat=`rsh linx01 'ls >/dev/null;echo $?'`;

I'm not sure what your overall point is. But if you don't want the output of ls, then route it to /dev/null; and, if you what the status of the command, it is $?, not $status. Also, with the single quotes, you don't want to escape the $.
 
Old 09-20-2007, 08:52 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
security note: consider using ssh instead of rsh.
 
Old 09-20-2007, 09:43 PM   #4
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by choogendyk View Post
Interesting that your title references $?, but your script doesn't.

Use:

$stat=`rsh linx01 'ls >/dev/null;echo $?'`;

I'm not sure what your overall point is. But if you don't want the output of ls, then route it to /dev/null; and, if you what the status of the command, it is $?, not $status. Also, with the single quotes, you don't want to escape the $.

Hi,

i need "ls" command output should display in STDOUT. the execution status should return in to $? or $status. now the issue is "ls" command output and return status both are appended in $? or $status.

how can i extract the execution status from $? or $status?

Please let me know.
 
Old 09-21-2007, 02:38 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think (if I understand you correctly) you want something like:
Code:
$status = system("ls");
die "$program exited funny: $?" unless $status == 0;
 
Old 09-21-2007, 11:36 PM   #6
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
I think (if I understand you correctly) you want something like:
Code:
$status = system("ls");
die "$program exited funny: $?" unless $status == 0;
Please go through the mail communication, have a look at that perl script. echo $status returns exectuion STDOUT as well as command execution status. i wanted to extract the return status from $status variable.

please let me know.
 
Old 09-22-2007, 09:53 PM   #7
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
OK, you're getting tangled up because you are running a shell command from inside of a perl script and you want to capture both the output and the status from the shell back to perl. Even within a pure shell script that can sometimes get complicated.

Just to give a somewhat messy example, I have a script in which I want to run a ufsdump and capture the status code from that. But I want to pipe the output of the ufsdump through ssh as a different user to a tape drive on a remote machine. Just a bit messy. I'm going to simplify it by pulling a lot of variables and options that were in my original.

( ufsdump <options-etc>; STATUS=$?; echo $STATUS > $FILE ) | su - backuser \
-c "ssh $TAPESERVER \"dd obs=64b of=/dev/rmt/0n\"";

Then I can do

STATUS=`cat $FILE`;

So, basically, I put the ufsdump into a subprocess within which I captured the status code. The standard out of that entire subprocess was piped to the su, which issued the ssh and ran it through to dd on the remote server to write the tape.

In the next step in the script I could then test the success of the ufsdump.

In your case you have (quoting exactly without subsequent comments or suggestons):

$stat=`rsh linx01 'ls ;echo \$status'`;
print $stat;

You can change this to send the $? somewhere that you can pick it up from, or you can change it to send the ls output somewhere that you can pick it up from.

You might decide that having the ls output go to a file to allow further manipulation might be the way to go. Then you could do something like:

$status=`rsh linx01 'ls > $file; echo $?'`;

following which you can immediately use the return value of $status to decide what your program does next. If it was successful, it can process the output of the ls command from the file -- copying it out, or whatever you want.
 
  


Reply



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
running an executable from a bourne shell which returns a value anekkan Programming 6 09-19-2005 07:59 PM
get a status output cccc Programming 12 07-28-2005 05:33 PM
EtherExpress Pro returns status 0x440 ichrispa Linux - Hardware 4 07-12-2005 07:47 AM
Shell output location pk_fox Linux - General 4 03-24-2005 07:44 AM
Rhythmbox returns failed to activate the shell: (null) Minderbinder Linux - Software 0 06-04-2004 05:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

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