script help
Can anyone tell how i can stop a shell script if i get an error
or if the script cant run one of the commands can anyone help with the command of what i need to do |
I think you want to look into the $? variable which shows the exit status of the last command. See here: http://tldp.org/LDP/abs/html/exit-status.html
|
If aborting a running script is what you are asking for, then issue CTRL-C on the same commandline console where you launched your script - this will stop the script.
During debugging you can add 'return;' to your shell script, so it will not proceed to run, at least not in the given function. If your script has no functions defined, then 'return' will simply stop any further script parsing. Also add some meaningful diagnostic messages to your script. |
Without specifically knowing what your script looks like it's hard to say exactly, but you can use something like they said above return or $?. I also add returns and status codes to my functions so they display things like...
"Running function_blah... [SUCCESS]" or [FAILED] This is pretty easy to do and I can give you an example if you want. Also don't forget its sometimes useful when you're debugging shell scripts to run them as ' sh -x script.sh ' which will run and display the output of every line that gets run in the script. |
I agree with djsoundfx in that we can't really help with a better explanation of what you are trying to do. Maybe a code sample would help as well.
And while the $? variable can be helpful, please be aware that if you put in a command with a pipe, $? returns the exit status of part of the command, but not the whole command. This can lead to some rather confusing results. Maybe what you want is the || operator (assuming that you are using bash). It says, "do this if the previous command failed". Code:
$ touch /abc.txt 2>/dev/null || echo "No can do. Not root." So, in your case, you could do a ... Code:
$ my_command || exit 999 And FYI, you can use && if you want to do something if the previous command completed succusfully. |
Quote:
Code:
whatever && echo "whatever worked" || echo "whatever failed" Code:
#!/bin/bash |
thanks everyone i used
$? |
|
All times are GMT -5. The time now is 03:26 AM. |