Bash If Error
Contents |
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 bash if error code About Us Learn more about Stack Overflow the company Business Learn more about
Bash If Error Exit
hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is bash if errorlevel 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
Bash Stop If Error
are 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 bash check if error but it 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
Bash Prompts About Writing Robust Bash Shell Scripts Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of
Bash Check If Error Occurred
their own. Unfortunately shell scripts are full of subtle effects which
Bash Script If Error
result in scripts failing in unusual ways. It's possible to write scripts which minimise these problems. In bash send email if error this article, I explain several techniques for writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't set? I http://askubuntu.com/questions/29370/how-to-check-if-a-command-succeeded know I have, many times. chroot=$1 ... rm -rf $chroot/usr/share/doc If you ran the script above and accidentally forgot to give a parameter, you would have just deleted all of your system documentation rather than making a smaller chroot. So what can you do about it? Fortunately bash provides you with set -u, which will exit your script http://www.davidpashley.com/articles/writing-robust-shell-scripts/ if you try to use an uninitialised variable. You can also use the slightly more readable set -o nounset. david% bash /tmp/shrink-chroot.sh $chroot= david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% Use set -e Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit. Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use: command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi could be replaced with command || { echo "command failed"; exit 1; } or if ! comm
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 http://stackoverflow.com/questions/8886065/bash-script-error-catching Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack 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 up Bash Script Error Catching up vote 10 down vote favorite 3 I am very if error new to bash scripts, and for my first script attempt I am submitting files to my professor's dropbox within the same server. The code is this: #! /bin/bash echo "Submit Lab$1? \c" read choice if [ $choice = "y" ]; then echo "Sending to Prof's dropbox..." cp -r /path/to/lab$1 /path/to/dropbox else echo "Submission terminated." fi Usage for the command is simply "$ submit 1" (1 is a single integer, corresponding bash if error to which lab I want to submit) The bash script appends the entered argument (a single integer) to the necessary filename (a directory), then submits that directory to my prof's dropbox using cp. More for the sake of learning than of absolute necessity, I wanted to create a clean prompt that will catch any cp errors (such as file not existing), and be able to output my own error message. So basically, something like: echo "Submit lab$1?" read choice echo "Send to Prof's dropbox" cp -rv /path/to/lab$1 /path/to/dropbox