Bash Continue Even If Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow bash if error code the company Business Learn more about hiring developers or posting ads with us Stack bash if error exit Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
Bash Stop If Error
4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Execute a shell command from a shell script without stopping if error occurs up vote 20 down vote
Bash Check If Error
favorite 5 In a sort of try/catch form I want to execute a bash that doesn't stop if an error occurs. The specific bash is: #!/bin/sh invoke-rc.d tomcat stop rm -fr /var/webapps/ cp -R $WEBAPP /var/webapps/ invoke-rc.d tomcat start I want to exec "invoke-rc.d tomcat stop" and even if Tomcat is not running, continue to execute the other bash commands. linux bash error-handling share|improve this question edited Jul 2 '09 at 16:51 bash check if error occurred Jonathan Leffler 437k61507821 asked Jul 2 '09 at 15:31 JorgeO 67721022 5 Isn't that bash's default behavior? I could have sworn it was (you need to take explicit action to terminate a script if some command fails...) –Alex Martelli Jul 2 '09 at 15:35 If you execute this script and tomcat is not running the script will stop execution since the first command return error. I want the script to continue. –JorgeO Jul 2 '09 at 15:38 @Alex: it is default but there is probablt something that overrides the default setting. –Milan Babuškov Jul 2 '09 at 16:23 3 I'm with Alex - bash is misbehaving, or has been configured to (mis)behave by exiting on an error. Use: 'set +e' to counteract the effect of 'set -e' (which means terminate on error). And sort out what it is in your environment setup that makes bash misbehave - all sorts of scripts will fail unexpectedly if your shell bails on the first error. –Jonathan Leffler Jul 2 '09 at 16:54 add a comment| 5 Answers 5 active oldest votes up vote 24 down vote accepted Try: invoke-rc.d tomcat stop > /dev/null 2>&1 || true A little background: user@tower: # true user@tower: # echo $? 0 user@tower: # false user@
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Bash Script If Error
Business Learn more about hiring developers or posting ads with us Unix & Linux Questions bash if exist Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, bash test exit status FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top http://stackoverflow.com/questions/1075083/execute-a-shell-command-from-a-shell-script-without-stopping-if-error-occurs How to conditionally do something if a command succeeded or failed up vote 77 down vote favorite 16 How can I do something like this in bash? if "`command` returns any error"; then echo "Returned an error" else echo "Proceed..." fi bash control-flow share|improve this question edited Oct 16 '11 at 23:57 Michael Mrozek♦ 44.9k19144179 asked Oct 16 '11 at 21:25 Shinmaru migrated from programmers.stackexchange.com Oct 16 '11 at 22:29 This question came from our site http://unix.stackexchange.com/questions/22726/how-to-conditionally-do-something-if-a-command-succeeded-or-failed for professional programmers interested in conceptual questions about software development. add a comment| 7 Answers 7 active oldest votes up vote 103 down vote That's exactly what bash's if statement does: if command ; then echo "Command succeeded" else echo "Command failed" fi Adding information from comments: you don't need to use the [ ... ] syntax in this case. [ is itself a command, very nearly equivalent to test. It's probably the most common command to use in an if, which can lead to the assumption that it's part of the shell's syntax. But if you want to test whether a command succeeded or not, use the command itself directly with if, as shown above. share|improve this answer edited Nov 23 '13 at 0:28 answered Oct 16 '11 at 21:50 Keith Thompson 8,13232235 2 Note that the semicolon is important. –Thorbjørn Ravn Andersen Oct 17 '11 at 7:37 8 Or you can just put then on a separate line. –l0b0 Oct 17 '11 at 9:00 8 @Joe: I think you mean if ! command ; then ... ; fi. [ is itself a command, and it's not needed in this case. –Keith Thompson Jan 13 '12 at 10:19 8 @Joe: My way also has the virtue of being correct. if [ ! command ] doesn't execute command; it treats comma
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here. Search Forums Show Threads Show Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... learn http://www.unix.com/shell-programming-and-scripting/241905-how-ignore-error-command-bash-script.html linux and unix commands - unix shell scripting How to ignore error in command in bash script? Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 12-10-2013 postcd Banned Join Date: Oct 2013 Last Activity: 11 August 2016, 12:58 PM EDT Posts: 87 Thanks: 14 Thanked 2 Times in 2 Posts How to ignore error in command in bash script? Hello, i have bash script where im if error cycling some command for different lines in external file. example: Code: while read domain;do nslookupout=$(nslookup -type=ns $domain) || true another commands done < filenamewithdomains i added: || true after the command in belief it will just skip failures. But i got: nslookup: '.somedomain.com' is not a legal name (empty label) and it breaken running the script.. please how to achieve so this error is skipped and continuing to the next entry? i know i can fix my domain list so it bash if error dont contains inproper values, but i prefer skipping invalid entries. if anyone know how to remove lines in a file containing two dots at one line by sed, please kindly share. Remove advertisements Sponsored Links postcd View Public Profile Visit postcd's homepage! Find all posts by postcd #2 12-10-2013 Akshay Hegde Forum Advisor Join Date: Nov 2012 Last Activity: 28 September 2016, 2:06 AM EDT Posts: 1,783 Thanks: 97 Thanked 620 Times in 553 Posts You can do something like this Code: your command 2>&1 >/dev/null Remove advertisements Sponsored Links Akshay Hegde View Public Profile Find all posts by Akshay Hegde #3 12-10-2013 hergp Problem Eliminator Join Date: Jan 2010 Last Activity: 29 September 2016, 3:33 PM EDT Location: Vienna, Austria Posts: 841 Thanks: 26 Thanked 190 Times in 169 Posts I don't see, why your code should not work. The || true even takes care of the situation when the shell's -e option is in effect (assuming another command is not failing while -e is in effect). However, all entries in the filenamewithdomains should not start with a dot. hergp View Public Profile Visit hergp's homepage! Find all posts by hergp #4 12-10-2013 wisecracker Registered User Join Date: Jan 2013 Last Activity: 29 September 2016, 12:54 PM EDT Location: Loughborough Posts: 1,069 Thanks: 295 Thanked 280 Times in 216