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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-10-2009, 06:52 PM
|
#1
|
|
Member
Registered: Jan 2009
Posts: 51
Rep:
|
Bash question
I'm trying to write bash script that uses the exit code of an earlier command in flow control. The problem is that I want to use the exit code of the first application in a pipeline:
Code:
DESC=$(apt-cache show $1 | grep --max-count=1 -i "description:" | cut -c14-)
What I want to do is use apt-cache show's exit status for flow control to direct what I do with DESC.
Right now what I'm doing is running:
Code:
TEST=$(apt-cache show $1)
c=$?
Before DESC=... then using c to do my flow control. This is obviously not the best way to accomplish my goal.
Is it possible to go back a couple of commands and get an exit code? If so I could step back three commands to get to the exit code I want.
Slightly better, is it possible to interrupt a pipe to test the exit code immediately?
Perhaps most obvious, is it possible to capture the output (and exit status) of apt-cache show $1 in a variable while maintaining line breaks and then run that into grep --max-count=1 ... if appropriate given the exit status?
Can someone either give me some guidance on executing one of the three solutions I have imagined or suggest a better alternative?
Thank you.
|
|
|
|
06-10-2009, 08:48 PM
|
#2
|
|
Member
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569
Rep:
|
Have you considered using the && and || constructs after the apt-cache command. Something like:-
Code:
apt-cache show $1 && do the next bit because apt worked || do this because apt failed
If you only want to do something if apt-cache works, perhaps your command might be:-
Code:
DESC=$(apt-cache show $1 && grep --max-count=1 -i "description:" | cut -c14-)
|
|
|
|
06-11-2009, 09:28 AM
|
#3
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
Moin,
Quote:
Originally Posted by blacky_5251
...If you only want to do something if apt-cache works, perhaps your command might be:-
Code:
DESC=$(apt-cache show $1 && grep --max-count=1 -i "description:" | cut -c14-)
|
In my opinion this will not work because grep will not get any input - it doesn't know anything about apt-cache's output.
I think the only way is to redirect the output of "apt-cache" to a file, then check it's exit code and in case of success use this file for grep's input.
Jan
EDIT: If apt-cache fails - is there any output? If not, the resulting $DESC will be empty and you could check that.
Last edited by jan61; 06-11-2009 at 09:29 AM.
|
|
|
|
06-11-2009, 09:37 AM
|
#4
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
Quote:
Originally Posted by jan61
In my opinion this will not work because grep will not get any input - it doesn't know anything about apt-cache's output.
|
No, but the shell knows what apt-cache's exit status was (0 on success, any other whole number on failure), and so can determine whether it should run grep based on the boolean logic operators && and ||. You can check the exit status of a command using the variable $? echoed after a command.
Code:
pwc101@linux:~> ls / > /dev/null 2>&1; echo $?
0
pwc101@linux:~> ls /nonexistent_directory > /dev/null 2>&1; echo $?
2
pwc101@linux:~>
Here, the first command succeeded because ls can list the contents of the root directory, so the exit status ($?) was 0. The second example shows that ls failed since there's no directory in / called nonexistent_directory and ls gives us an exit status of 2, which according to the man page means the following:
Quote:
|
Originally Posted by man ls
Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
|
|
|
|
|
06-11-2009, 09:55 AM
|
#5
|
|
Member
Registered: Jan 2009
Posts: 51
Original Poster
Rep:
|
Jan61 is correct, grep will not get any input if I use &&. Assuming apt-cache works (exit 0) apt-cache will output to stdout and then bash will run grep with out any input, which will just block the shell. This is unhelpful. If apt-cache fails (exit >0) it will output to stderr and grep will not be run run.
I'd considered going through a file but I'd rather not.
The suggestion of checking DESC for empty is a good one. If apt-cache fails it outputs to stderr but nothing on stdout so DESC will be empty. I'll try it tonight and report back.
|
|
|
|
06-11-2009, 02:18 PM
|
#6
|
|
Member
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Rep:
|
"fhsm" can you simplify your question? What are you trying to do? Explain it in two senteces.
|
|
|
|
06-11-2009, 03:08 PM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
i think you are confused.
a pipeline doesn't run sequentially
they run in parallel,
the apt-cahe runs into the grep into the cut simultaneously.
it isn't apt-cache runs, exits, then grep starts.
you cannot get the exit code earlier in the pipeline it will be the exit code of
the last in the pipeline even if an earlier one fails.
observe, the ls fails but the grep works:
Code:
$ 2>&1 ls NO_FILE | grep No
ls: NO_FILE: No such file or directory
$ echo $?
0
# same but grep fails
$ 2>&1 ls NO_FILE | grep XXX
$ echo $?
1
an if
Code:
if TEST=pipeline
then
:
else
TEST=FAILED
endif
Last edited by bigearsbilly; 06-11-2009 at 03:12 PM.
|
|
|
|
06-11-2009, 08:43 PM
|
#8
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
Hi.
I haven't used this often, but it may be of some value in this situation ... cheers, makyo
Quote:
PIPESTATUS
An array variable (see Arrays below) containing a list of exit
status values from the processes in the most-recently-executed
foreground pipeline (which may contain only a single command).
--excerpt from man bash
|
|
|
|
|
06-12-2009, 08:33 AM
|
#9
|
|
Member
Registered: Jan 2009
Posts: 51
Original Poster
Rep:
|
Quote:
Originally Posted by ramram29
"fhsm" can you simplify your question? What are you trying to do? Explain it in two senteces.
|
Use the exit code of an application, other than the last application, for flow control in a bash script.
@mayko that is really helpful. I'm not at a system I can test that on but if it is what it sounds like that's a great tip.
Turns out the work around on this problem is just to check if DISC is empty. If apt-cache failed but somehow wrote "description:" to stdout this wouldn't get me what I want but it's close enough.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:22 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|