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? |
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 [] |
Quote:
Code:
#!/bin/bash -e |
If you want to check cmds individually
Code:
#!/bin/bash Code:
./script.sh >scriptlog 2>&1 |
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? |
Quote:
Here both means same Code:
./script.sh > scriptlog 2>&1 |
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 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 |
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? |
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 & Basically, like all (most?) of the punct symbols, it has multiple 'definitions' dependent on context Code:
./myscript >myscript.log 2>&1 & See the link in my prev post. In a sed cmd '&' has yet another meaning, and so it goes... |
Code:
(although in C its called the address operator iirc. The Ref (aka ptr) operator is '*' ) Code:
Can you guess what's going on here ? ;) Code:
See the link in my prev post. |
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. |