LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2011, 01:16 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
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?

Last edited by metallica1973; 10-24-2011 at 01:19 PM.
 
Old 10-24-2011, 01:23 PM   #2
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
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
 
Old 10-24-2011, 01:37 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
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
??

Last edited by metallica1973; 10-24-2011 at 02:33 PM.
 
Old 10-24-2011, 04:13 PM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
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.
 
  


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
Question about flow and error control magodiafano Linux - Networking 5 12-13-2010 12:34 AM
Korn Shell Scripting case statement help!! wfdeng117 Programming 3 07-22-2008 10:51 AM
A question of flow-control on nic cards Cpoc Linux - Networking 0 06-26-2006 07:46 PM
case airflow, and fan control, HD cable question, and stress testing help a NEWBIE zetsui Linux - Hardware 3 03-17-2004 03:36 AM
Stupid Question driving me crazy ViGs Linux - General 2 06-06-2003 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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