LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-10-2009, 07:52 PM   #1
fhsm
Member
 
Registered: Jan 2009
Posts: 51

Rep: Reputation: 17
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.
 
Old 06-10-2009, 09:48 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 55
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-)
 
Old 06-11-2009, 10:28 AM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by blacky_5251 View Post
...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 10:29 AM.
 
Old 06-11-2009, 10:37 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by jan61 View Post
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.
 
Old 06-11-2009, 10:55 AM   #5
fhsm
Member
 
Registered: Jan 2009
Posts: 51

Original Poster
Rep: Reputation: 17
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.
 
Old 06-11-2009, 03:18 PM   #6
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
"fhsm" can you simplify your question? What are you trying to do? Explain it in two senteces.
 
Old 06-11-2009, 04:08 PM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
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 04:12 PM.
 
Old 06-11-2009, 09:43 PM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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
 
Old 06-12-2009, 09:33 AM   #9
fhsm
Member
 
Registered: Jan 2009
Posts: 51

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by ramram29 View Post
"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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash question slimm609 Linux - General 3 06-12-2007 05:47 AM
Bash question armandino101 Linux - General 6 04-24-2007 01:18 PM
another bash question. . downbound010 Programming 3 03-23-2006 04:37 AM
Bash Question mbjunior99 Programming 5 08-31-2005 05:33 AM
little bash question gplacek Linux - General 2 09-01-2004 01:10 PM


All times are GMT -5. The time now is 02:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration