LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-23-2013, 01:04 PM   #1
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 443

Rep: Reputation: Disabled
bash error checking


I want to write a script which will be configuring and making a series of programs. I would like to have some form of error checking. It doesn't have to be that sophistocated. Just something that will stop the script on errors.
How can I do this?
 
Old 06-23-2013, 01:16 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,454

Rep: Reputation: 440Reputation: 440Reputation: 440Reputation: 440Reputation: 440
typically in BASH, and mind you im still relatively new to BASH myself, if there is an error in the script it will crash at that point.

you can always use test statements to verify before you continue, that is one form of error checking. what exactly are you looking for and what have you written so far? please place your script in "code" flags. you get this by going to advance posting options or by placing code /code inside of []
 
Old 06-23-2013, 02:25 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by mreff555
Just something that will stop the script on errors.
If you just want the script to stop when an error occurs you can use bash's -e option.
Code:
#!/bin/bash -e

..code goes here ...

# or

#!/bin/bash
set -e

... code goes here ...
Both stop executing the script when an error occurs.
 
Old 06-23-2013, 06:23 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
If you want to check cmds individually
Code:
#!/bin/bash

cmd_here
if [[ $? -ne 0 ]]
then
    echo "cmd errored .."
    exit
fi
Depending on cmd, you may be able to capture output and echo that, or just catch the whole script
Code:
./script.sh >scriptlog 2>&1
http://rute.2038bug.com/index.html.gz
 
Old 06-25-2013, 09:46 AM   #5
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 443

Original Poster
Rep: Reputation: Disabled
ok, here is where I'm getting a little fuzzy with command line operators.

2>&1

Could someone explain this to me?

As I understand it, it pushes stderr as well as stdout to the screen/log whatever is this correct. could someone break this down?
 
Old 06-25-2013, 09:50 AM   #6
mddesai
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch
Posts: 479

Rep: Reputation: 132Reputation: 132
Quote:
Originally Posted by mreff555 View Post
ok, here is where I'm getting a little fuzzy with command line operators.

2>&1

Could someone explain this to me?

As I understand it, it pushes stderr as well as stdout to the screen/log whatever is this correct. could someone break this down?
Redirects error to where output is going

Here both means same
Code:
./script.sh > scriptlog 2>&1
./script.sh >& scriptlog

Last edited by mddesai; 06-25-2013 at 10:00 AM.
 
Old 06-25-2013, 08:19 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
All Unix processes are automatically given 3 i/o channels on creation/startup

chan 0 = stdin
chan 1 = stdout
chan 2 = stderr

So we're directing '1' (stdout) to logfile and directing 2 (stderr) to the same address (&) as 1 ie the logfile.
Its possible to have separate output and err files, but its tricky to match them up afterwards, so usually you put them both to the same file.
Code:
#separate logs
./script.sh >script.log 2>script.err
Note that the '1' is optional here (and above), as its 'assumed' by the shell.

You can in fact capture the output/err of any prog launched from the shell the same way, UNLESS its already coded internally to put the o/p or err info elsewhere.

http://rute.2038bug.com/index.html.gz
 
Old 06-26-2013, 06:10 AM   #8
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 443

Original Poster
Rep: Reputation: Disabled
Ok, thats kinda what I thought was going on. As for the ampersand.(&)
Since that only appears to be used when directing error to the output stream, would that be akin to a reference operator in c?
 
Old 06-26-2013, 07:28 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
Kind of ... BUT only in this context

(although in C its called the address operator iirc. The Ref (aka ptr) operator is '*' )

Used like this
Code:
./myscript &
'&' puts the script into the background.
Basically, like all (most?) of the punct symbols, it has multiple 'definitions' dependent on context
Code:
./myscript >myscript.log 2>&1 &
Can you guess what's going on here ?

See the link in my prev post.

In a sed cmd '&' has yet another meaning, and so it goes...

Last edited by chrism01; 06-26-2013 at 07:31 PM.
 
Old 06-27-2013, 05:42 AM   #10
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 443

Original Poster
Rep: Reputation: Disabled
Code:
(although in C its called the address operator iirc. The Ref (aka ptr) operator is '*' )
I always get those two mixed up. My reference was here: www.cplusplus.com/doc/tutorial/pointers/

Code:
Can you guess what's going on here ? ;)
I believe that it would run the script in the background, sending both stdio and stderror to the log file.

Code:
See the link in my prev post.

In a sed cmd '&' has yet another meaning, and so it goes...[/QUOTE]
Thanks for the link.

Last edited by mreff555; 06-27-2013 at 05:43 AM.
 
Old 06-28-2013, 09:49 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
This link has a great explanation on how file descriptors work in the shell:

redirections and file descriptors explained
 
  


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
[SOLVED] Bash checking if action failed fatalerror0x00 Programming 12 10-29-2012 07:42 AM
[SOLVED] BASH checking if ttyS0 is connected hogar.strashni Programming 13 01-25-2012 05:30 AM
Bash scripting with directory checking if exists netpumber Programming 6 12-01-2009 07:35 PM
Checking variables in bash sharky Linux - Software 4 02-13-2009 08:07 PM
bash - Checking for file existance? rignes Programming 5 02-18-2006 03:02 PM


All times are GMT -5. The time now is 11:10 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration