Register
It is currently Sun Dec 17, 2017 5:45 am

Shell script - if statements dont work


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Dec 06, 2016 5:46 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
hi all,

i have made a shell script and it runs until it reaches the if statement, doesnt the ! mean only if the command fails it will echo me that message and then exit

can anyone please help me what is wrong with my code?

many thanks,

rob
Code:
#!/bin/bash

   echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
   read method

   case $method in
      1)
         echo "please specify full path to directory where the tar file is in"
         read -e dir
         cd $dir

         echo "please copy/paste filename here of tar file (including .tar)"
         read -e tar

         echo "please enter ID number ie ID1234"
         read id

         echo "please specify where you want the tar file to be stored"
         read -e dest

         if ! mv $tar $id_$tar ; then
            echo "something went wrong with the mv command, please do manually"
            exit
         fi

         if ! rsync -avh $id_$tar $dest ; then
            echo "something went wrong with the rsync command, please do manually"
            exit
         fi

         if ! rm -f $id_$tar ; then
            echo "something went wrong with the rm command, please do manually"
            exit
         fi
            
         ;;

      2)
         i have yet to do this one, but this is the directory one
         ;;


      *)
         echo "invalid selection, please re-run the script"
         exit

         ;;

   esac


Top
 Profile  
 PostPosted: Tue Dec 06, 2016 9:22 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
worked it out, i had to put quotes in variables

Code:
#!/bin/bash

   echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
   read method

   case $method in
      1)
         echo "please specify full path to directory where the tar file is in"
         read -e dir
         cd $dir

         echo "please copy/paste filename here of tar file (including .tar)"
         read -e tar

         echo "please enter ID number ie ID1234"
         read id

         echo "please specify where you want the tar file to be stored"
         read -e dest

         if ! mv "$tar" "$id"_"$tar" ; then
            echo "something went wrong with the mv command, please do manually"
            exit
         fi

         if ! rsync -avh "$id"_"$tar" $dest ; then
            echo "something went wrong with the rsync command, please do manually"
            exit
         fi

         if ! rm -f "$id"_"$tar" ; then
            echo "something went wrong with the rm command, please do manually"
            exit
         fi
            
         ;;

      2)
         i have yet to do this one, but this is the directory one
         ;;


      *)
         echo "invalid selection, please re-run the script"
         exit

         ;;

   esac


Top
 Profile  
 PostPosted: Tue Dec 06, 2016 10:15 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
http://www.grymoire.com/Unix/Quote.html


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP