LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash error checking (https://www.linuxquestions.org/questions/linux-newbie-8/bash-error-checking-4175467090/)

mreff555 06-23-2013 01:04 PM

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?

lleb 06-23-2013 01:16 PM

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 []

druuna 06-23-2013 02:25 PM

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.

chrism01 06-23-2013 06:23 PM

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

mreff555 06-25-2013 09:46 AM

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?

Madhu Desai 06-25-2013 09:50 AM

Quote:

Originally Posted by mreff555 (Post 4978363)
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


chrism01 06-25-2013 08:19 PM

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

mreff555 06-26-2013 06:10 AM

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?

chrism01 06-26-2013 07:28 PM

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...

mreff555 06-27-2013 05:42 AM

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.

David the H. 06-28-2013 09:49 AM

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

redirections and file descriptors explained


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