Operating System - HP-UX
1834459 Members
2390 Online
110067 Solutions
New Discussion

Re: Capture output of successful command in a shell.

 
Shashibhushan Gokhale
Occasional Contributor

Capture output of successful command in a shell.

Hello All,

How to get the results of a sucessfull command into a variable?

I know the way to get the output of an "unsuccessful" command into a variable, from the discussion today initiated by Sean on this forum.


For Example,
I want to grab the output of sleep when it is sucessful, to get the process id of the process:

I execute the following command:
$ sleep 10 &
[1] 13482

I want to capture the output containing the process id.

Regards,
Shashi
3 REPLIES 3
John Palmer
Honored Contributor

Re: Capture output of successful command in a shell.

You can't 'capture' that output.

However, the process id of the last background command invoked by the shell (13482) in your example is held in the $! variable.

Thus:
sleep 10&
CHILD=${!}

$CHILD will contain the relevant PID.

Regards,
John
Christian Gebhardt
Honored Contributor

Re: Capture output of successful command in a shell.

Hi

Try this
#(sleep 10 ;echo $? > output.txt ) &

Chris
Robin Wakefield
Honored Contributor

Re: Capture output of successful command in a shell.

Hi Shashi,

I know it doesn't directly put the output into a variable, but you could use the script command:

script /tmp/output.txt
sleep 1 &
Ctrl-D

cat /tmp/output.txt
Script started on Fri Feb 21 14:16:14 2003
# sleep 1 &
[1] 18447
ln4p2714-64 #

script done on Fri Feb 21 14:16:21 2003

rgds, Robin