LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script help (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-685744/)

mahmoud 11-24-2008 09:15 AM

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

Telemachos 11-24-2008 10:09 AM

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

salter 11-24-2008 10:28 AM

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.

djsoundfx 11-24-2008 10:39 AM

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.

Hobbletoe 11-24-2008 12:32 PM

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."
Should fail as a general user will not be able to create a file in /.

So, in your case, you could do a ...

Code:

$ my_command || exit 999
Give each command a different exit status if you want to be able to see where in your script it actually bombed out.

And FYI, you can use && if you want to do something if the previous command completed succusfully.

i92guboj 11-24-2008 01:01 PM

Quote:

Originally Posted by Hobbletoe (Post 3352964)
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."
Should fail as a general user will not be able to create a file in /.

So, in your case, you could do a ...

Code:

$ my_command || exit 999
Give each command a different exit status if you want to be able to see where in your script it actually bombed out.

And FYI, you can use && if you want to do something if the previous command completed succusfully.

I use this intensively in command line as a sort of fast if-then-else construct.

Code:

whatever && echo "whatever worked" || echo "whatever failed"
On shell scripts I use things like this:

Code:

#!/bin/bash

function die() {
  echo "$1"
  exit 1 # error
}

# do this, do that, ...

dowhatever || die "whatever couldn't be done"
# whatever_else

You can as well go collecting $? all around the script and storing return status into variables, and decide at a later stage using case, if or whatever you feel is better for your purpose. It all depends on what are you exactly doing.

mahmoud 11-25-2008 10:34 AM

thanks everyone i used
$?

nehaandrew 11-25-2008 11:54 AM

Using the $? should help you achieve what you want.

Linux


All times are GMT -5. The time now is 03:26 AM.