LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newb problem with a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/newb-problem-with-a-bash-script-4175520440/)

HWDPlinux 09-28-2014 11:13 PM

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


TenTenths 09-29-2014 02:30 AM

$? 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.

pan64 09-29-2014 04:21 AM

exec command will replace the current shell and execute the command. It will never return. You need to remove exec

rknichols 09-29-2014 09:51 AM

Quote:

Originally Posted by TenTenths (Post 5246136)
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.

TenTenths 09-29-2014 10:52 AM

Quote:

Originally Posted by rknichols (Post 5246321)
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!

HWDPlinux 09-30-2014 04:51 PM

Thanks for all the replys, they all helped. Script fixed.


All times are GMT -5. The time now is 02:05 AM.