- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- pipes and return codes
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:24 AM
06-18-2003 07:24 AM
pipes and return codes
A script contains the following:
function X
{
sed 's/^/### /p'
}
commandB | X
My problem is to catch the code ($?) returned by commandB without using a intermediate file. I need that value to be returned to the parent process (the process that calls my script)
any ideas ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:27 AM
06-18-2003 07:27 AM
Re: pipes and return codes
Easiest way is to use a file:-
commandB | >>/tmp/X
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:28 AM
06-18-2003 07:28 AM
Re: pipes and return codes
Easiest way is to use a file:-
commandB >>/tmp/X
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:28 AM
06-18-2003 07:28 AM
Re: pipes and return codes
commandB| echo $? | X
X will get in input the RETCOD.
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:31 AM
06-18-2003 07:31 AM
Re: pipes and return codes
sorry for a mispelling:
(commandB ); echo $? | X
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:32 AM
06-18-2003 07:32 AM
Re: pipes and return codes
commandB > /dev/null
echo $? | X
I need the output from commandB BE the input to X.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:34 AM
06-18-2003 07:34 AM
Re: pipes and return codes
so you need both the output of the command and the retcode, is this correct ?
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:35 AM
06-18-2003 07:35 AM
Re: pipes and return codes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:37 AM
06-18-2003 07:37 AM
Re: pipes and return codes
# (commandB; echo $? > /tmp/rc.txt) | X
commandB's output is passed thru the pipe while its return code is saved in /tmp/rc.txt
Hai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:41 AM
06-18-2003 07:41 AM
Re: pipes and return codes
How about putting commandB into a function too, ie:
function commandBfunction
{
commandB
retcodeB=$?
}
You can then use retcodeB elsewhere in the script.
regards,
Darren.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:51 AM
06-18-2003 07:51 AM
Re: pipes and return codes
{
commandB
RVAL=$?
}
Z | X
return $RVAL
Darren, it's a good idea but it still doesn't work because Z and X are run in two subshells. See the manual pages of sh-posix(1):
A pipeline is a sequence of one or more commands separated by a bar
(|) and optionally preceded by an exclamation mark (!). The standard
output of each command but the last is connected by a pipe (see
pipe(2)) to the standard input of the next command. Each command is
run as a separate process; the shell waits for the last command to
terminate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:55 AM
06-18-2003 07:55 AM
Re: pipes and return codes
Rewritten function X:
function X
{
RC=`echo $?`
sed 's/^/###/p'
}
Rewrite "commandD | X" to:
export RC
commandB | X
echo $RC # this is the return code of commandB
Hai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 07:55 AM
06-18-2003 07:55 AM
Re: pipes and return codes
use
export RETVALB=$?
should to the final trick.
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 08:03 AM
06-18-2003 08:03 AM
Re: pipes and return codes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 08:08 AM
06-18-2003 08:08 AM
Re: pipes and return codes
function Z
{
commandB
RVAL=$?
}
export RVAL
Z | X
echo $RVAL
This should be independent of the pipe and should always work since RVAL is a global variable.
Hai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 08:18 AM
06-18-2003 08:18 AM
Re: pipes and return codes
For executing Z, the shell runs a subshell. RVAL value is changed in this subshell. Thus, when subshell dies, its RVAL variable is destroyed.
I think the only solution is using an intermediate file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 08:23 AM
06-18-2003 08:23 AM
Re: pipes and return codes
Although it's more messy, I think that dumping the return code into a file is your best option, though it's been fun trying to find ways round it!
regards,
Darren.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 08:27 AM
06-18-2003 08:27 AM
Re: pipes and return codes
{
commandB
RVAL=`echo $?`
}
export RVAL
Z | X
echo $RVAL
I caught the error in function Z. RVAL=$? is corrected to RVAL=`echo $?`
Hai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 09:52 PM
06-18-2003 09:52 PM
Re: pipes and return codes
The trick is the subshell, you put both output to the suvsequent command. When you want to test the RETCOD, you seach with awk for it.
[massimo@localhost massimo]$ (ls infos.txt; echo RETCODE=$?) | grep -v ciccio
infos.txt
RETCODE=0
[massimo@localhost massimo]$ (ls infos1.txt; echo RETCODE=$?) | grep -v ciccio
ls: infos1.txt: No such file or directory
RETCODE=1
[massimo@localhost massimo]$
As you can see:
- no file
- both output of the command, standard error, and return code are echoed to the command after the pipe.
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2003 11:06 PM
06-18-2003 11:06 PM
Re: pipes and return codes
I've tried ... like others :-), but with no success, and I can't see any 'generic' way of doing it without a temp file. Some solutions will work depending of the command on the right side of the pipe, but it's always easy to find a pipe command which wouldn't work ... For example, if I need to grep ALL output from the left side, no solution using echo $? can work.
Sorry for you ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 03:00 PM
06-19-2003 03:00 PM
Re: pipes and return codes
{
cat - |
sed 's/^/### /p'
RVAL2=`echo $? xxxxxxxxxxxxxxxxxxxxxxxxx`
RVAL1=`cat RVAL$$;rm RVAL$$`
}
Z()
{
cat /etc/passwd
echo $? yyyyyyyyyyyyyyyyyyy > RVAL$$
}
Z |X
echo XXXXXXXXXXXXXXXXXX $RVAL1 XXXXXXXXXXXXXX
echo XXXXXXXXXXXXXXXXXX $RVAL2 XXXXXXXXXXXXXX
It is not pretty
the cat /etc/passwd is commandB I needed something real.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2003 10:51 PM
06-19-2003 10:51 PM
Re: pipes and return codes
I'm sure it works, but you've cheated :-) He didn't wanted to use a tmp file, and you use RVAL$$. If a tmp file is allowed, you have many simple solutions ...
#!/usr/bin/sh
RETCOD=/tmp/retcod$$
(command ; echo $? > $RETCOD) | sed 's/^/###/' # or any function ...
exit $(cat $RETCOD)
Regards.