|
|
#1
| |||
| |||
| Capturing program return codes in a bash script? How do I determine if an command completed successfully when run from a bash script to decide what the script should do next? Working on a mysqldump backup script which works pretty nice... but doesn't really care whether the database dump dies, errors out or completes successfully. I figure that's kind of important for a backup script. -- "Remain calm, we're here to protect you!" |
| |
|
#2
| |||
| |||
| Re: Capturing program return codes in a bash script? On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote: > > How do I determine if an command completed successfully when run from a > bash script to decide what the script should do next? Depends on the application. Usually return code is in $? Example: ping -c 1 -w 3 $_target_host > /dev/null if [ $? -eq 0 ] ; then echo "Ping failure on $_target_host" exit 1 fi On some apps I have had to redirect error output to a file and use grep to check for error string. |
|
#3
| |||
| |||
| Re: Capturing program return codes in a bash script? On Wed, 2 Apr 2008 21:30:42 +0000 (UTC), Bit Twister wrote: > On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote: >> >> How do I determine if an command completed successfully when run from a >> bash script to decide what the script should do next? > > Depends on the application. > Usually return code is in $? > > Example: > ping -c 1 -w 3 $_target_host > /dev/null > if [ $? -eq 0 ] ; then Oops. that should read if [ $? -ne 0 ] ; then echo "Ping failure on $_target_host" exit 1 fi For extra points read man test http://tldp.org/LDP/abs/html/index.html |
|
#4
| |||
| |||
| Re: Capturing program return codes in a bash script? On Wed, 02 Apr 2008 21:30:42 +0000, Bit Twister wrote: > On Wed, 02 Apr 2008 16:19:19 -0500, Ivan Marsh wrote: >> >> How do I determine if an command completed successfully when run from a >> bash script to decide what the script should do next? > > Depends on the application. > Usually return code is in $? > > Example: > ping -c 1 -w 3 $_target_host > /dev/null if [ $? -eq 0 ] ; then > echo "Ping failure on $_target_host" exit 1 > fi > > On some apps I have had to redirect error output to a file and use grep > to check for error string. Grepping through an output file is how I have been doing it... which is prone to not being able to plan for the unknown. I figured there had to be a more standardized way of doing it. So I guess I'd have to test the application to see if it produces the $? and adjust accordingly. -- "Remain calm, we're here to protect you!" |
|
#5
| |||
| |||
| Re: Capturing program return codes in a bash script? Ivan Marsh <annoyed@you.now> writes: >How do I determine if an command completed successfully when run from a >bash script to decide what the script should do next? >Working on a mysqldump backup script which works pretty nice... but >doesn't really care whether the database dump dies, errors out or >completes successfully. $? is the return code of the last command run. command1 && command2 executes command2 only if command1 completes suddessfully if command; then command1 |
|
#6
| |||
| |||
| Re: Capturing program return codes in a bash script? On Wed, 02 Apr 2008 16:43:08 -0500, Ivan Marsh wrote: > > Grepping through an output file is how I have been doing it... which is > prone to not being able to plan for the unknown. > > I figured there had to be a more standardized way of doing it. > > So I guess I'd have to test the application to see if it produces the $? > and adjust accordingly. Just repeating the warning, I have had a good return code in $? and a failure in standard error. :( It just depends on the application. |
|
#7
| |||
| |||
| Re: Capturing program return codes in a bash script? Bit Twister writes: > Just repeating the warning, I have had a good return code in $? and a > failure in standard error. That's a bug. -- John Hasler john@dhh.gt.org Dancing Horse Hill Elmwood, WI USA |
|
#8
| |||
| |||
| Re: Capturing program return codes in a bash script? John Hasler <john@dhh.gt.org>: > Bit Twister writes: > > Just repeating the warning, I have had a good return code in $? and a > > failure in standard error. > > That's a bug. Or he's mis-read the manpage's description of return values. Not everything says "$? == 0" on success. Which process in a chain does he want to consider the definitive result? foo > blah | huggghh! ... He might fiddle with wrapping parts in parens (run in subshells). -- Any technology distinguishable from magic is insufficiently advanced. (*) http://blinkynet.net/comp/uip5.html Linux Counter #80292 - - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me. |
|
#9
| |||
| |||
| Re: Capturing program return codes in a bash script? On 2008-04-02, Ivan Marsh wrote: > > How do I determine if an command completed successfully when run from a > bash script to decide what the script should do next? > > Working on a mysqldump backup script which works pretty nice... but > doesn't really care whether the database dump dies, errors out or > completes successfully. > > I figure that's kind of important for a backup script. if command args ## replace with your command then echo success else echo failure fi Or: command args if [ $0 -eq 0 ] then echo success else echo failure fi -- Chris F.A. Johnson, author | <http://cfaj.freeshell.org> Shell Scripting Recipes: | My code in this post, if any, A Problem-Solution Approach | is released under the 2005, Apress | GNU General Public Licence |
|
#10
| |||
| |||
| Re: Capturing program return codes in a bash script? Chris F.A. Johnson wrote: > On 2008-04-02, Ivan Marsh wrote: >> >> How do I determine if an command completed successfully when run from a >> bash script to decide what the script should do next? >> Normally you get $? as the status for the last command. Most programs uses 0 as failure and 1 as success. See man bash on status is set in different situations like pipes. |
|
#11
| |||
| |||
| Re: Capturing program return codes in a bash script? Benny Nielsen wrote: > Chris F.A. Johnson wrote: > >> On 2008-04-02, Ivan Marsh wrote: >>> >>> How do I determine if an command completed successfully when run from a >>> bash script to decide what the script should do next? >>> > > Normally you get $? as the status for the last command. Most programs uses > 0 as failure and 1 as success. Nope. Most programs use a return code of 0 to indicate success, and a non-zero value to indicate the opposite (not always failure). > See man bash on status is set in different > situations like pipes. -- Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ |
| Bookmarks |
| Thread Tools | |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| change directory in a bash script | Adrian Schmitt | Linux | 3 | 02-24-2008 12:10 PM |
| grub in bash script? | confuzious | Linux | 5 | 10-22-2007 09:50 AM |
| Bash script glitch | Mike | Linux | 6 | 08-11-2007 05:30 AM |
| Bash script questions. | Longfellow | Linux | 5 | 05-06-2007 12:26 AM |
| grep in a bash script | hobbzilla | Linux | 4 | 05-06-2007 12:11 AM |
| New To Technology Questions? | Do You Need Help with Your Computer or Device? | Do You Need Help with this site? |