LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Flow Control Question Korn if/ifelse/Case driving me crazy (https://www.linuxquestions.org/questions/programming-9/flow-control-question-korn-if-ifelse-case-driving-me-crazy-909862/)

metallica1973 10-24-2011 01:16 PM

Flow Control Question Korn if/ifelse/Case driving me crazy
 
I am wiping off the dust to my shell scipting days and had this question: I have this script that I have created, and cannot figure out where my flow control issue is within this script.

Code:

#!/bin/ksh
#Basic script to backup server
#Home directories to the external
#############################################################
## Functions using Expect##

ssh_mysql_bak()
{
expect <<EOD
set timeout 30
exp_internal 1

spawn ssh -p 3202 testser@192.168.0.32 "mysqldump --opt -u root -ppassword mysql > mysql.bak.dump"
expect "testuser@192.168.0.32's password:"
send "password\r"
wait
EOD
}

scp_mysql_bak ()
{
expect <<EOD
set timeout 30
exp_internal 1
spawn scp -P 3202 testuser@192.168.0.32:/home/testuser/mysql.bak.dump $home
expect "testuser@192.168.0.32's password:"
send  "password\r"
wait
EOD
}

############################################################### MAIN ################################################################################

results=$1
home=/home/testuser
backup_dir=/media/caca/extract
date=$(date +%m%d%y)
reg_date=$(date)
id_check=$(whoami)
search=find
archive="cpio -oavc"
mail_to=me@testemail.com 
sendto=/usr/bin/mutt
ping -c 1 $remote-host >/dev/null 2>&1
ping_stat=$?      # 0 for success; non-zero for failure

        if (( $ping_stat == 0 )) && [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]]; then
        cd $home
        # Excludes copying of hidden folders to conserve space while testing
        echo " $date Full Backup Log                                            " > $backup_dir/full_status$date.log
        $search . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print | $archive > $backup_dir/full$date.cpio 2>> $backup_dir/full_status$date.log
        ssh_mysql_bak && scp_mysql_bak;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2>> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to < $backup_dir/full_status$date.log
        else
                echo ""                                                                                  > $backup_dir/fullerror$date.log
                echo " $reg_date  ----------Full Backup Error Log----------                          " >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/fullerror$date.log
                echo " The "Full Backup" did not execute for one of the Following reasons:      "      >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " 1 - Ran as non-root user                                                "      >> $backup_dir/fullerror$date.log
                echo " 3 - Share not mounted                                                    "      >> $backup_dir/fullerror$date.log     
                echo " 3 - Incorrect Syntax                                                    "      >> $backup_dir/fullerror$date.log
                $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/incerror$date.log
exit 1
fi

ping -c 1 $remote-host >/dev/null 2>&1
ping_stat=$?      # 0 for success; non-zero for failure

        if (( $ping_stat == 0 )) && [[ $id_check == "root" && -d $backup_dir &&  $results == "incremental" ]]; then

        cd $home
# Searches for only files that have been modified with 24 hours excluding hidden directories to conserve space while testing
        $search . -depth -mtime 0 ! -ipath './.*' ! -ipath './Downloads*' -print | $archive > $backup_dir/inc$date.cpio 2> $backup_dir/inc$date.log
        ssh_mysql_bak 2>&1 /dev/null && scp_mysql_bak 2>&1 /dev/null ;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
        else
                echo ""                                                                                > $backup_dir/error$date.log
                echo " $reg_date  ----------Incremental Backup Error Log----------                          "    >> $backup_dir/incerror$date.log
                echo ""                                                                                        >> $backup_dir/incerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/incerror$date.log
                echo ""                                                                                >> $backup_dir/incerror$date.log
      $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/incerror$date.log
exit 1
fi

case "$results"
in

        full) echo full;;

        incremental) echo incremental;;

        *)echo "Incorrect syntax usage. Use <backup.ksh> {full|incremental} or target directory does not exist. Please verify share exist";;

esac

The problem is that when I do not specify "full or incremental, it proceeds to execute the "Full Backup" else statement instead of flagging incorrect syntax usage from my case area. If full or incremental are not specified then the syntax error should appear:


Code:


sudo ksh -x ./backup.ksh
+ results=''
+ home=/home/testuser
+ backup_dir=/media/caca/extract
+ date +%m%d%y
+ date=102411
+ date
+ reg_date='Mon Oct 24 10:27:13 EDT 2011'
+ whoami
+ id_check=root
+ search=find
+ archive='cpio -oavc'
+ mail_to=me@testemail.com
+ sendto=/usr/bin/mutt
+ remote_host=192.168.0.32
+ ping -c 2 192.168.0.32
+ 1> /dev/null 2>& 1
+ ping_stat=1
+ (( 1 == 0 ))
+ echo ''
+ 1> /media/caca/extract/fullerror102411.log
+ echo ' Mon Oct 24 10:27:13 EDT 2011  ----------Full Backup Error Log----------                          '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' Make sure you are root and backup share is mounted on server **/media/backup_drive** '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' The Full' 'Backup did not execute for one of the Following reasons:      '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ''
+ 1>> /media/caca/extract/fullerror102411.log
+ echo ' 1 - Ran as non-root user                                                '
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 2 - Share not mounted\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ echo $' 3 - Incorrect Syntax\t\t\t\t\t\t\t'
+ 1>> /media/caca/extract/fullerror102411.log
+ /usr/bin/mutt -s 'Daily MYSQL Log' me@testemail.com
+ 0< /media/caca/extract/fullerror102411.log
+ exit 1

What should have been executed:


Code:


*)echo "Incorrect syntax usage. Use <backup.ksh> {full|incremental} or target directory does not exist. Please verify share exist";;

Where is my flow control issue?

metallica1973 10-24-2011 01:23 PM

I also tried switched around the placement of the variables:

Code:


if [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]] ; then

        remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then
 cd $home
        # Excludes copying of hidden folders to conserve space while testing
        echo " $date Full Backup Log                                            " > $backup_dir/full_status$date.log
        $search . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print | $archive > $backup_dir/full$date.cpio 2>> $backup_dir/full_status$date.log
        ssh_mysql_bak && scp_mysql_bak;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2>> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to < $backup_dir/full_status$date.log
        else
                echo ""                                                                                  > $backup_dir/fullerror$date.log
                echo " $reg_date  ----------Full Backup Error Log----------                          " >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/fullerror$date.log
                echo " The "Full Backup" did not execute for one of the Following reasons:      "      >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " 1 - Ran as non-root user                                                "      >> $backup_dir/fullerror$date.log
                echo " 3 - Share not mounted                                                    "      >> $backup_dir/fullerror$date.log     
                echo " 3 - Incorrect Syntax                                                    "      >> $backup_dir/fullerror$date.log
                $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/incerror$date.log
exit 1
fi

if [[ $id_check == "root" && -d $backup_dir &&  $results == "incremental" ]] ; then

        remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then

        cd $home
# Searches for only files that have been modified with 24 hours excluding hidden directories to conserve space while testing
        $search . -depth -mtime 0 ! -ipath './.*' ! -ipath './Downloads*' -print | $archive > $backup_dir/inc$date.cpio 2> $backup_dir/inc$date.log
        ssh_mysql_bak 2>&1 /dev/null && scp_mysql_bak 2>&1 /dev/null ;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
        else
                echo ""                                                                                > $backup_dir/error$date.log
                echo " $reg_date  ----------Incremental Backup Error Log----------                          "    >> $backup_dir/incerror$date.log
                echo ""                                                                                        >> $backup_dir/incerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/incerror$date.log
                echo ""                                                                                >> $backup_dir/incerror$date.log
      $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/incerror$date.log
exit 1
fi


metallica1973 10-24-2011 01:37 PM

It looks like I need the additional "fi"'s

Code:

if [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]] ; then

        remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then
 cd $home
        # Excludes copying of hidden folders to conserve space while testing
        echo " $date Full Backup Log                                            " > $backup_dir/full_status$date.log
        $search . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print | $archive > $backup_dir/full$date.cpio 2>> $backup_dir/full_status$date.log
        ssh_mysql_bak && scp_mysql_bak;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2>> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to < $backup_dir/full_status$date.log
        else
                echo ""                                                                                  > $backup_dir/fullerror$date.log
                echo " $reg_date  ----------Full Backup Error Log----------                          " >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/fullerror$date.log
                echo " The "Full Backup" did not execute for one of the Following reasons:      "      >> $backup_dir/fullerror$date.log
                echo ""                                                                                >> $backup_dir/fullerror$date.log
                echo " 1 - Ran as non-root user                                                "      >> $backup_dir/fullerror$date.log
                echo " 3 - Share not mounted                                                    "      >> $backup_dir/fullerror$date.log     
                echo " 3 - Incorrect Syntax                                                    "      >> $backup_dir/fullerror$date.log
                $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/incerror$date.log
exit 1
  fi
fi

if [[ $id_check == "root" && -d $backup_dir &&  $results == "incremental" ]] ; then

        remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then

        cd $home
# Searches for only files that have been modified with 24 hours excluding hidden directories to conserve space while testing
        $search . -depth -mtime 0 ! -ipath './.*' ! -ipath './Downloads*' -print | $archive > $backup_dir/inc$date.cpio 2> $backup_dir/inc$date.log
        ssh_mysql_bak 2>&1 /dev/null && scp_mysql_bak 2>&1 /dev/null ;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
        else
                echo ""                                                                                > $backup_dir/error$date.log
                echo " $reg_date  ----------Incremental Backup Error Log----------                          "    >> $backup_dir/incerror$date.log
                echo ""                                                                                        >> $backup_dir/incerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/incerror$date.log
                echo ""                                                                                >> $backup_dir/incerror$date.log
      $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/incerror$date.log
exit 1
  fi
fi

but now it is not executing the backup thus not executing:

Full

Code:

  remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then
 cd $home
        # Excludes copying of hidden folders to conserve space while testing
        echo " $date Full Backup Log                                            " > $backup_dir/full_status$date.log
        $search . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print | $archive > $backup_dir/full$date.cpio 2>> $backup_dir/full_status$date.log
        ssh_mysql_bak && scp_mysql_bak;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2>> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to < $backup_dir/full_status$date.log
exit 1

or

Incremental

Code:

  remote_host=192.168.0.32
        ping_stat=$?
        ping -c 2 $remote_host >/dev/null 2>&1

                if [[ $ping_stat -ne 0 ]] ; then

        cd $home
# Searches for only files that have been modified with 24 hours excluding hidden directories to conserve space while testing
        $search . -depth -mtime 0 ! -ipath './.*' ! -ipath './Downloads*' -print | $archive > $backup_dir/inc$date.cpio 2> $backup_dir/inc$date.log
        ssh_mysql_bak 2>&1 /dev/null && scp_mysql_bak 2>&1 /dev/null ;$search ./Downloads -iname mysql.bak.dump | $archive > $backup_dir/mysql$date.cpio 2> $backup_dir/mysql_status$date.log
        $sendto -s "Daily MYSQL Log" $mail_to  < $backup_dir/mysql_status$date.log
        $sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
        else
                echo ""                                                                                > $backup_dir/error$date.log
                echo " $reg_date  ----------Incremental Backup Error Log----------                          "    >> $backup_dir/incerror$date.log
                echo ""                                                                                        >> $backup_dir/incerror$date.log
                echo " Make sure you are root and backup share is mounted on server **/media/backup_drive** " >> $backup_dir/incerror$date.log
                echo ""                                                                                >> $backup_dir/incerror$date.log
      $sendto -s "Daily MYSQL Log" $mail_to < $backup_dir/incerror$date.log
exit 1

??

metallica1973 10-24-2011 04:13 PM

I made another adjustment,fixed some of the logic and it fixed my issues. I created a pinger function and just called it when I needed it as opposed to using a variable constant:

Code:

pinger ()
{
remote_host=192.168.0.32
ping -c 1 $remote_host >/dev/null 2>&1
ping_stat=$?
}
##############

if [[ $id_check == "root" && -d $backup_dir &&  $results == "full" ]] ; then

                pinger

                if [[ $ping_stat -eq 0 ]] ; then
        cd $home
        ......

I also added exit 1's at the end of conditions

Code:

$sendto -s "Daily Backup Log" $mail_to  < $backup_dir/inc$date.log
exit 1
...

I will paste the finish product in a few.


All times are GMT -5. The time now is 03:20 AM.