LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-24-2008, 10:15 AM   #1
mahmoud
Member
 
Registered: Apr 2006
Location: UK
Distribution: Mandriva, Debain, Redhat, Fedora, Ubuntu, FreeBSD
Posts: 269

Rep: Reputation: 30
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
 
Old 11-24-2008, 11:09 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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
 
Old 11-24-2008, 11:28 AM   #3
salter
Member
 
Registered: Oct 2008
Posts: 100

Rep: Reputation: 15
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.

Last edited by Tinkster; 10-30-2010 at 06:18 PM.
 
Old 11-24-2008, 11:39 AM   #4
djsoundfx
Member
 
Registered: May 2008
Distribution: Gentoo, Ubuntu, RHEL, CentOS, BSD, Solaris
Posts: 82

Rep: Reputation: 23
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.
 
Old 11-24-2008, 01:32 PM   #5
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
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.
 
Old 11-24-2008, 02:01 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by Hobbletoe View Post
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.
 
Old 11-25-2008, 11:34 AM   #7
mahmoud
Member
 
Registered: Apr 2006
Location: UK
Distribution: Mandriva, Debain, Redhat, Fedora, Ubuntu, FreeBSD
Posts: 269

Original Poster
Rep: Reputation: 30
thanks everyone i used
$?
 
Old 11-25-2008, 12:54 PM   #8
nehaandrew
Member
 
Registered: Nov 2008
Posts: 53

Rep: Reputation: 15
Using the $? should help you achieve what you want.

Linux

Last edited by nehaandrew; 11-30-2008 at 02:13 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 03:41 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 09:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration