ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
TEST=$(apt-cache show $1)
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?
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.
pwc101@linux:~> ls / > /dev/null 2>&1; echo $?
pwc101@linux:~> ls /nonexistent_directory > /dev/null 2>&1; echo $?
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:
Originally Posted by man ls
Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
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.