home
NEWS       BLOGS       FORUMS       NEWSLETTERS       RESEARCH       EVENTS       DIGITAL LIBRARY       CAREERS  
Network Computing Network Computing Powered by InformationWeek Business Technology Network

IMMERSE YOURSELF:

SOA

  |

Data Center

  |

802.11n

  |

Data Privacy

  |
APO  |

Virtualization

  |

NAC

  |

Security

  |

Network Mgmt

  |

Enterprise Apps

  |

Storage & Servers




``Answers to Unix'' Column: No. 003

Questions regarding this column should be sent to the author at ray@cse.ucsc.edu.

Can A Pipe Have More Than One Exit?

By Ray Swartz

Question: I was a subscriber of UnixWorld and now I am a reader of UnixWorld Online. I'm a Korn shell user and I have a problem in the execution of the following pipe:

 
command1 | command2

How do I save the execution status of the first command (command1) in the pipe line? I know that the exit status, after the execution of a command is saved in $?, but in the previous expression this variable takes the exit status of the second command in the pipe line (command2).

Rafael Pascual / Madrid, Spain

Answer: You are correct that the exit status of a pipeline is reported as the exit status of the last command in the pipeline. The exit status is reported by the shell as it executes the pipeline. It is the shell that stores the pipe's exit status in the $? variable.

What happens to the exit status of the first (or other) command? It is ignored by the shell. However, you should be able to capture it with a little ingenuity.

A command's exit status is the value returned to the process that called it. A command can only have one exit status because only a single integer is reserved by the UNIX kernel for a process's exit status. Because a pipeline has, at least, two commands, the designers of the UNIX system had to decide which exit status to report back to the shell for the command that implements a pipeline. The one they chose was the exit status of the command at the tail of the pipeline.

You can capture the exit status of the first (or other) command in a pipeline by running that command inside a shell script whose sole task is running the command and then reporting the exit status of the command executed.

Here is the code for one such script, which we'll name exit.report:

#!/bin/ksh
"$@"
echo "Exit status of command ("$@") was " $? >&2

This script executes the command line specified on its invocation command line and reports the command exit status to the standard error file to separate it from standard output, which could be redirected to a file or into a pipe.

The "$@" construct represents all the command line arguments enclosed in double quotes, so that substitution is performed, but the result is protected from further processing by the shell. The >&2 construct, which is shorthand for 1>&2, causes the output of echo to sent to standard error (which is associated with the terminal).

Here the exit.report script has been passed a simple command line (not a pipe line):

 
$ exit.report cat exit.report
#!/bin/ksh
"$@"
echo "Exit status of command ("$@") was " $? >&2
Exit status of command (cat exit.report) was 0
$ []

which it executes and then displays its exit status (0 in this example) because the cat command succeeded.

Here is an example with a pipeline:

$ exit.report cat exit.report | wc
Exit status of command (cat exit.report) was 0
3 12 64
$ []

This command line sends the output of the cat exit.report command to wc via the pipeline to give the count report. The exit status output from exit.report is sent to the standard error, not into the pipeline, so it is displayed on the terminal before the output from wc.

Here is another example where the command at the head of the pipeline fails:

$ exit.report grep Answer exit.report | wc -l
exit.report grep Answer exit.report | wc -l
Exit status of command (grep Answer exit.report) was 1
0
$ []

exit.report also works in the middle of a pipeline:

$ cat exit.report | exit.report grep Answer | wc -l
Exit status of command (grep Answer) was 1
0
$ []
Print This Page


e-mail Send as e-mail





Ready to take that job and shove it?

Function:

Keyword(s):

State:
SPONSOR
RECENT JOB POSTINGS
CAREER NEWS
Aneesh Chopra is looking to other CIOs to advise him on fleshing out a more detailed agenda to best serve the president's IT agenda.

IT spending is expected to decline by 3.8 percent in 2009 according to Gartner.










2009 IT Salary Survey: Meager Raises, Solid Prospects
Though raises are notably smaller than a year ago, and job security’s shrinking, IT careers are looking safer than many others in this economic downturn. Get all the findings in InformationWeek's 2009 IT Salary Survey. Available FREE for a limited time.
 
ROLLING RIGHT ALONG
Follow key Network Computing Reviews from conception to completion. This Week: Holistic APM.



Network Computing Reports Emerging Enterprise Podcast Series: Secrets to Success








TechSearch


Microsite of the Week


Powerful Information at Your Fingertips



Techweb
Informationweek Business Technology Network
InformationweekInformationweek 500Informationweek 500 ConferenceInformationweek AnalyticsInformationweek Events
Informationweek MagazineGlobal CIOIWK Government ITbMightyByte and SwitchDark Reading
Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingPlug Into The CloudDr. DobbsContentinople
space
TechWeb Events Network
InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0Mobile Business ExpoNoJitter
Black HatGTECEnergy CampCloud ConnectGov 2.0 ExpoGov 2.0 Summit
space
Light Reading Communications Network
Light ReadingLight Reading AsiaUnstrungCable Digital NewsInternet EvolutionPyramid Research
Heavy ReadingLight Reading LiveLight Reading InsiderEthrnet ExpoTelco TVTower Technology Summit
space
Financial Technology Network
Advanced TradingBank Systems and TechnologyInsurance and TechnologyWall Street and TechnologyAccelerating WallstreetBST SummitBuyside Trading SummitIT Summit
space
Microsoft Technology Network
MSDNTechNetTotal IT ProTotal Dev ProNET Total Dev Pro CommunitySQL Total Dev Pro Community
space


App Infrastructure   |   Messaging & Collaboration   |   Network & Systems Mgmt   |   Network Infrastructure   |   Security  |   Storage & Servers   |   Wireless   |   Enterprise Apps
About Us  |  Contact Us  |  Site Map  |  Technology Marketing Solutions  |  Advertising Contacts  |   Briefing Centers
Copyright © 2009  United Business Media LLC  |  Privacy Statement  |  Terms of Service