LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 02-09-2009, 03:40 AM   #1
proNick
Member
 
Registered: Apr 2005
Posts: 104

Rep: Reputation: 15
Check if actions in bash shell script are performed well


Hi all!

I have a shell script that creates MySQL dump, like:


#!/bin/bash

/usr/local/bin/mysqldump -hXXX -uYYY etc etc



I would like that if for some reasion dump is not properly created, script make attempts until it's done.

Can you help me how to do it?

Thank you in advance!
 
Old 02-09-2009, 03:50 AM   #2
kauuttt
Member
 
Registered: Dec 2008
Location: Kolkata, India
Distribution: Ubuntu
Posts: 123

Rep: Reputation: 25
put the command in a loop...check for the dump file whether it is created or not..if it is created,break from the loop...
 
Old 02-09-2009, 04:05 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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

Code:
<command> && <to_do_if_success> || <to_do_if_not>
 
Old 02-09-2009, 06:55 AM   #4
proNick
Member
 
Registered: Apr 2005
Posts: 104

Original Poster
Rep: Reputation: 15
Thank you on your answers, but I need more informations. I will try to explain a bit more...

As we know, mysql dump is created with something like:

mysqldump -hmyhost -umyuname -ppasswd dbname > dbname.sql


But sometimes, this command fails to execute properly to the end.

So, how can I "catch" if error occured? It would be great if I can put error message in variable, and work with it's value.

Of course, I tryed to solve it by myself...

I can not do it with something like mysqldump ... 2> error, because I have to make output in file and in variable.

I also tryed to work with tee command, but no luck to solve this:

mysqldump -hXXX -uYYY -pZZZ 2>&1 1> mydump.sql | tee error.log


So, how can I work with last tee error.log as a variable (if possible without creating file error.log)?

Thank you in advance!
 
Old 02-09-2009, 12:01 PM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 11,057
Blog Entries: 1

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
Hi,

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.
 
Old 02-09-2009, 12:33 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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.
 
Old 02-09-2009, 07:25 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,247

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Basic version
Code:
some_cmd >some_cmd.log 2>&1
if [[ $? -ne 0 ]]
then
    /bin/mail -s "$SUBJECT" "$EMAIL_ADDR" < some_cmd.log
fi
There's little point in making it keep trying if you don't know what's broken.
 
  


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
shell script (continuous pinging and actions when host is up) xtraya Programming 4 02-19-2008 07:11 PM
How to send mails automatically when cvs check-in is performed ? deepugopi Linux - Server 6 09-19-2007 01:37 PM
bash script to check mount is ok procfs Programming 2 01-17-2007 06:19 PM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM
How to check the bash script syntax? mesh2005 Linux - General 2 04-23-2006 08:22 AM


All times are GMT -5. The time now is 08:19 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