How to check that a shell script executed properly?

Xampp Apache
July 19, 2013 at 18:02:55
Specs: linux kali
Is there any easy way to determine that all the commands in a script executed properly or do I need to check exit variable $? after every command? For eg.

Command 1
$a=$?
Command 2
$b=$?
.
.
So on

then at the last of the script, I do the following:

$z=$a+$b+ . . . So on

and finally using if, to check variable $z


if [ $z -eq 0 ] then
echo "the script executed successfully" else
echo "some commands may not have executed properly"
fi

any suggestions?


See More: How to check that a shell script executed properly?

Report •

#1
July 20, 2013 at 07:48:24
Updating syntax (plz suggest further improvement or better way/idea)

Command 1
a=$?
Command 2
b=$?
.
.
so on

then at the last of the script i do the following:

z='expr $a + $b + . . . so on'

and finally using "if statment" to check variable z


if [ $z -eq 0 ] then
echo "the script executed successfully" else
echo "some commands may not have executed properly"
fi


Report •

#2
July 20, 2013 at 08:30:13
The worst thing would be to not have any error checking, which is the case in most shell scripts I've seen. Next to that would be adding up the exit codes of the commands and only check it at the end. If one or more of them failed, how would you know which one(s) failed?

I don't write shell scripts. My language choice is Perl. I try to always write defensively, which means I do error checking/handling early and often. This is especially important if the the results of the failed command will effect other portions of the script or the overall result of the script. When is the result of a command not important? I say never, unless you intentionally want to write buggy code.

Sometimes the failure is minor and only requires the output of a warning message. Other times it could be a fatal error requiring the script to exit and sometimes it might require reverting prior operations before exiting.


Report •

#3
July 20, 2013 at 19:14:10
Yes I understand that why I need some quick professional solution. As you know:
"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late”.

Report •

Related Solutions

#4
July 21, 2013 at 08:20:32
I'd change that quote to: "The trouble with bad programmers is that you can never tell what the programmer is doing until it’s too late”.

Your question is to general to give a specific concrete answer, but generally speaking if a segment of code has the potential to fail, then error checking/handling should be applied to that section. If the code is not properly self documenting, then clear comments should be added to it so that others will know/understand what the code is doing and possibly why it's being done.

Waiting until the end of the script to check for possible failures that could have happened anywhere prior to that point is the wrong approach and demonstrates bad program design.


Report •

#5
July 21, 2013 at 20:01:34
All the commands in the script are important few of them are linked to each & other are indirectly linked.
If a single command behaves abnormally (aborts/executes partially etc.) then the result from the whole script becomes useless.

No matter how well you code, there is always trade offs. For eg.
1) Is a program efficient i.e. how much resources (eg. memory) it takes.

2) Is it secure?

3) Is it fast?

Even if one makes a program 100% then the big point is how well it mingles with other programs or the OS itself?

That is the reason why you find many big companies like microsoft rolling out updates/patches/fixes.

Hence, even if I get some solution to my problem (till now i dont feel that some shell coder will respond to my question) but one thing that I feel very sure about programming is:
It is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots.

So far, the Universe is winning :-) (see no hard feelings).


Report •

#6
July 21, 2013 at 22:08:25
There's no specific need for a shell programmer to respond to your question. Your question could be answered by someone who has no programming experience.

Your question boils down to "should I apply error checking where/when the error occurred or only check at the end of the script where I have no idea what portion of the script failed and hence won't know how to fix it?" The answer should be obvious.


Report •

#7
July 24, 2013 at 16:42:09
All I want to know is, the options available to determine whether a command or commands in the script (or the whole script) were executed successfully or not

We know that exit code $? becomes 0 on successful execution of command so,

1) What happens in the case of partial execution of command, for eg. If internet disconnects when command
"apt-get -y install elinks" is running.

what will be the value of $? in such a case? Is there any other way to find the status of command?

Is there a way to determine exit code of full shell script?

How can I think about designing a good program, if am still struggling with it's syntax?

A good tech support should always carefully read the problem and then make decisions:

1) Is the question within his/her area of interest or expertise.

2) One should not pick something which seems to be more interesting in the question, just for the sake of answering or to show off your IQ.

Questioner never post questions to challenge or to check the knowledge of a tech support?

3) Lastly, one should prepare answer according to the level of questioner?

For eg. nobody is interested in optimizing a program till basics of programming like syntax, control statements (If then else etc.), all types of variables, codes, etc. are clear in concept and well known.

So, (instead of being egostic) always look for simple things first, for eg. the code in my question stated above still contains syntax errors.

Computing.Net is a place where people don't post problems on to become software engineers.(even if there are some then they won't waste their precious time here)

If somebody is interested to know a little bit about shell programming to carry out a task then he will not join a institute which teaches perl or software engineering.


Report •

#8
July 28, 2013 at 20:51:23
to execute a shell script ./ in some cases just.(dot)

Report •

#9
January 11, 2014 at 11:23:22
I agree with Fishmonger, you need to errorcheck each command/line/call individually. If you want to know what commands failed by running 1 script : look at the output. If it doesn't show, write decent error handling. I don't see why you would want to add exitcodes. By far as I remember, any script fails at the very first exit to non-zero, and I don't see why any script would want to continue.

Hi there.


Report •

Ask Question