ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
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 $.
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 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.
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
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.
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):
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.