LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-28-2014, 11:13 PM   #1
HWDPlinux
Member
 
Registered: Mar 2014
Posts: 32

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, 02:30 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
$? 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, 04:21 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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, 09:51 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 10:52 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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, 04:51 PM   #6
HWDPlinux
Member
 
Registered: Mar 2014
Posts: 32

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 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
Open Source Consulting | Domain Registration