Bash Error Level Checking
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 the company Business Learn more about hiring developers or posting ads
Bash Neq
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack bash if exit code Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign bash exit status up Checking Bash exit status of several commands efficiently up vote 162 down vote favorite 70 Is there something similar to pipefail for multiple commands, like a 'try' statement but within bash. I would like to do something like this: echo
Bash Script Exit On Error
"trying stuff" try { command1 command2 command3 } And at any point, if any command fails, drop out and echo out the error of that command. I don't want to have to do something like: command1 if [ $? -ne 0 ]; then echo "command1 borked it" fi command2 if [ $? -ne 0 ]; then echo "command2 borked it" fi And so on... or anything like: pipefail -o command1 "arg1" "arg2" | command2 "arg1" "arg2" | command3 Because the arguments of each command
Bash Function Return Value
I believe (correct me if I'm wrong) will interfere with each other. These two methods seem horribly long-winded and nasty to me so I'm here appealing for a more efficient method. bash exit share|improve this question edited Nov 28 '14 at 6:39 Andy Shulman 84211027 asked Mar 4 '11 at 15:19 jwbensley 2,445103764 add a comment| 11 Answers 11 active oldest votes up vote 184 down vote accepted You can write a function that launches and tests the command for you. Assume command1 and command2 are environment variables that have been set to a command. function mytest { "$@" local status=$? if [ $status -ne 0 ]; then echo "error with $1" >&2 fi return $status } mytest $command1 mytest $command2 share|improve this answer edited Apr 21 at 21:45 octopusgrabbus 5,55772882 answered Mar 4 '11 at 15:29 krtek 19.3k33872 21 Don't use $*, it'll fail if any arguments have spaces in them; use "$@" instead. Similarly, put $1 inside the quotes in the echo command. –Gordon Davisson Mar 4 '11 at 16:01 edited, thanks for the advice ! –krtek Mar 4 '11 at 16:05 71 Also I'd avoid the name test as that is a built-in command. –John Kugelman Mar 4 '11 at 16:11 1 This is the method I went with. To be honest, I don't think I was clear enough in my original post but this method allows me to write my own 'test' function so I can then perform a
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation Social Media Facebook Twitter bash return Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet
Bash Exit Function
Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] linux exit code How do I error check a command in BASH? Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to http://stackoverflow.com/questions/5195607/checking-bash-exit-status-of-several-commands-efficiently 5 of 5 Thread: [SOLVED] How do I error check a command in BASH? Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode January 18th, 2008 #1 oodlesOfmoodles View Profile View Forum Posts Private Message Just Give Me the Beans! Join Date Jul 2007 Beans 65 [SOLVED] How do I error check a command in BASH? I'm writing a script to automate a few things for https://ubuntuforums.org/showthread.php?t=670885 some new users. I already have a script that installs flash but I also want to install VLC for DVD playback. I have the command: Code: gksudo aptitude install vlc How would I go about error catching on this? I know for a command like wget I can do this: Code: wget http://blah.com/blah.jpg || echo -e "internet not working"; What can I do and further more, what exactly does the '||' mean? thanks! Adv Reply January 18th, 2008 #2 Cappy View Profile View Forum Posts Private Message Visit Homepage Tall Cafè Ubuntu Join Date Feb 2007 Location In my chair Beans 1,485 Re: How do I error check a command in BASH? You can use Code: sudo apt-get install vlz || echo ERROR Use sudo for terminal based apps and gksudo for graphical apps. "II" means "or". If the first argument fails it goes to the next argument. ---- You can also use something like Code: sudo apt-get install vlz if [ $? -gt 0 ]; then echo "ERROR!" fi Currently favorite songs: Miss Hyde, This Song Is About Monsters, Masagin, I Will Try to Blow it Out, La Resistance Visit www.cherrypeel.com for more free indie music =) Adv Reply January 18th, 2008 #3 oodlesOfmoodles View Profile View Forum Posts Private Message Just Give Me the Beans! Join Date Jul 2007 Beans 65 Re: How do I er
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 http://askubuntu.com/questions/29370/how-to-check-if-a-command-succeeded Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 exit code voted up and rise to the top How to check if a command succeeded? up vote 62 down vote favorite 21 Is there any way to check if there is an error in executing a command? Example : test1=`sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ip:1521:$dataBase" $search` valid $test1 function valid () { if $test -eq 1; then echo "OK" else echo "ERROR" fi } I already tried do that but it bash error level seems it isn't working. I don't how do that. command-line share|improve this question edited Mar 7 '11 at 13:41 Octavian Damiean 10.1k54160 asked Mar 7 '11 at 11:40 moata_u 71861523 5 Prefer $(foo) over backticks `foo`, because you can nest it, and it's easier to distinguish from apostrophes. –user unknown Mar 26 '11 at 18:19 add a comment| 5 Answers 5 active oldest votes up vote 97 down vote accepted The return value is stored in $?. 0 indicates success, others indicates error. some_command if [ $? -eq 0 ]; then echo OK else echo FAIL fi Like any other textual value, you can store it in a variable for future comparison: some_command retval=$? do_something $retval if [ $retval -ne 0 ]; then echo "Return code was not zero but $retval" fi For possible comparison operators, see man test. share|improve this answer edited Mar 7 '11 at 13:14 answered Mar 7 '11 at 12:06 Lekensteyn 85.6k34220292 That's nice ...can i hold the output error ??!! , because in this case i have 2 error : command error "text" ex, file not found and my error "text" Which is in this case failed for example –moata_u Mar 7 '11