LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-29-2014, 12:13 AM   #1
HWDPlinux
LQ Newbie
 
Registered: Mar 2014
Posts: 26

Rep: Reputation: Disabled
Newb problem with a bash script


Hi guys, newbie here, script stops working after first if statement...
Not sure if it matters, working on a Vbox CentOS6.



Code:
#!/bin/bash
HTTPCONF="/etc/httpd/conf/httpd.conf"
SSLCONF="/etc/httpd/conf.d/ssl.conf"
WP="cp -rRp --copy-contents /var/www/test.com/ /backups/"
TCONF="/etc/httpd/conf.d/test.conf"

echo "copying the /var/www/kdworak.com dir & the config files"

if [ $? == 0 ];
        then
          cp $HTTPCONF $SSLCONF $TCONF /backups/
          #Execute the WP variable
          exec $WP
fi

clear

SQLL="mysqldump -uroot -ppassword Wptestdb > /backups/dump_$(date +%Y%m%d).sql"
if [ $? == 0 ];
        then
        echo "dumping mysql database...."
        exec $SQLL
fi

clear

if [ -f /backups/ssl.conf ];
        then
        #Make a tarbal out of the contents of the /backups/ dir
        tar -cfz /root/WPbackup_$(date +%Y%m%d).tgz /backups/
fi
echo
echo "Backup Complete!"
exit
 
Old 09-29-2014, 03:30 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
$? is the result of the last command, I'm guessing you're trying to detect if various commands worked and then run additional parts of the script accordingly.

If that's the case then your use of $? in that script is going to give you all sorts of unusual results. For example in your first "if" you're checking to see if the previous echo command worked. Same with your second, that's going to give the results of the "clear" command rather than anything else.

You can also put set -x as the second line of your script and that'll give you much more detail into how your script is executing.
 
1 members found this post helpful.
Old 09-29-2014, 05:21 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,514

Rep: Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436
exec command will replace the current shell and execute the command. It will never return. You need to remove exec
 
1 members found this post helpful.
Old 09-29-2014, 10:51 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,138

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Quote:
Originally Posted by TenTenths View Post
Same with your second, that's going to give the results of the "clear" command rather than anything else.
Actually it will show the results of assigning a string to the variable SQLL, and that will always be 0.
 
1 members found this post helpful.
Old 09-29-2014, 11:52 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,305

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Quote:
Originally Posted by rknichols View Post
Actually it will show the results of assigning a string to the variable SQLL, and that will always be 0.
You're absolutely right, my bad on that one!
 
1 members found this post helpful.
Old 09-30-2014, 05:51 PM   #6
HWDPlinux
LQ Newbie
 
Registered: Mar 2014
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thanks for all the replys, they all helped. Script fixed.
 
  


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
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 02:57 AM
Problem running an Expect script within a Bash script mbeipi Programming 8 03-15-2011 05:07 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 06:39 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM


All times are GMT -5. The time now is 07:21 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration