Check if actions in bash shell script are performed well
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The right solution is most times to check the exit status of the given application. After all, that's what the exit status is for.
mysqldump will spit an integer when it's done running. Usually, 0 means success, and any other things means that something bad happened. The concrete value can be used for different errors. Usually, the man of info page should specify that.
In any case, this return code can be used in many ways to branch your script depending on the success of failure of a given instruction.
In addition, in bash, 0 is true, while anything else is false. So, you could perfectly do something like:
Code:
while ! <command>
do
:
done
That will loop indefinitely until the exit status of <command> is zero (success).
You can even use the logical AND and OR operators to bifurcate
What part of 'i92guboj' post indicates exit code or status? Most properly written applications provide a 'exit code'. You will use that 'exit code' or status to decide within your script a specific operation to be performed relative to the indicated error.
proNick, the problem with saving the output to a variable, if you ask me, is that variables have a very finite space available. So, if anything nasty happens the output will probably not fit into your variable. If you really want to save the stuff for reference, redirect both stderr and stdout to a file, and for the rest, use the exit status to determine if the thing was successful. If it was, there's absolutely no point in parsing any output, is there? If the exit status is not zero, then you can go to the file and grep, sed, cut or awk it to your desire.
But if you are going to do intensive parsing in long logs, bash is probably not the best.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.