Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Shell script - if statements dont work

Programming


Tags
solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-06-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 20 July 2017, 5:24 AM EDT
Posts: 68
Thanks: 7
Thanked 5 Times in 4 Posts
Shell script - if statements dont work

hi all,

i have made a shell script and it runs until it reaches the if statement, doesn't 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

Sponsored Links
    #2  
Old Unix and Linux 12-06-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 16 August 2017, 12:34 PM EDT
Location: Lancashire, UK
Posts: 3,204
Thanks: 1,368
Thanked 616 Times in 557 Posts
Hello robertkwild,

What input do you use and what errors do you get?

You might be hitting a number of problems because you have minimal error checking and your mv command might have a null input so becomes invalid too. This might not be a problem for a simple helper script for yourself but might be more confusing if you delegate it.

Is a null response to the source directory okay? That would change to the user's home directory.



Robin
Sponsored Links
    #3  
Old Unix and Linux 12-06-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 20 July 2017, 5:24 AM EDT
Posts: 68
Thanks: 7
Thanked 5 Times in 4 Posts
this is the result on what the end user sees -

https://s17.postimg.org/a71wi9xxr/script.png

and after the end user presses the enter key when they tab the stored directory, it suddenly quits the script
    #4  
Old Unix and Linux 12-06-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 16 August 2017, 12:34 PM EDT
Location: Lancashire, UK
Posts: 3,204
Thanks: 1,368
Thanked 616 Times in 557 Posts
I regret I cannot open that link - office internet access restriction. Can you post the content as text in CODE tags?



Robin
Sponsored Links
    #5  
Old Unix and Linux 12-06-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 20 July 2017, 5:24 AM EDT
Posts: 68
Thanks: 7
Thanked 5 Times in 4 Posts

Code:
is this archive for an audio tar (press 1) or an audio directory (press 2)
1
please specify full path to directory where the tar file is in
/to_be_archived/
please copy/paste filename here of tar file (including .tar)
untitled\ folder.tar 
please enter ID number ie ID1234
ID1234
please specify where you want the tar file to be stored
/archived_projects/

the above to get the directory or tar file all i need to do is tab as i have the e option which means interact with shell
Sponsored Links
    #6  
Old Unix and Linux 12-06-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 16 August 2017, 12:34 PM EDT
Location: Lancashire, UK
Posts: 3,204
Thanks: 1,368
Thanked 616 Times in 557 Posts
Thank you for this. Can you run it again with bash -x your_script and paste the output. It will look messy, but it will show what your code is actually trying to do and what conditions are met in the if statements.



Thanks again,
Robin
Sponsored Links
    #7  
Old Unix and Linux 12-06-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 20 July 2017, 5:24 AM EDT
Posts: 68
Thanks: 7
Thanked 5 Times in 4 Posts
not wrong there when you said it looks ugly!!!


Code:
[root@robw-linux /]# bash -x test.sh
+ echo 'is this archive for an audio tar (press 1) or an audio directory (press 2)'
is this archive for an audio tar (press 1) or an audio directory (press 2)
+ read method
1
+ case $method in
+ echo 'please specify full path to directory where the tar file is in'
please specify full path to directory where the tar file is in
+ read -e dir
/to_be_archived/
+ cd /to_be_archived/
+ echo 'please copy/paste filename here of tar file (including .tar)'
please copy/paste filename here of tar file (including .tar)
+ read -e tar
untitled\ folder.tar 
+ echo 'please enter ID number ie ID1234'
please enter ID number ie ID1234
+ read id
ID1234
+ echo 'please specify where you want the tar file to be stored'
please specify where you want the tar file to be stored
+ read -e dest
/archived_projects/
+ mv untitled folder.tar untitled folder.tar
mv: target `folder.tar' is not a directory
+ echo 'something went wrong with the mv command, please do manually'
something went wrong with the mv command, please do manually
+ exit
[root@robw-linux /]#

---------- Post updated at 09:10 AM ---------- Previous update was at 09:07 AM ----------

mmm... its not appending the ID number to it ie ID1234_untitled folder.tar
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Dont want to mention user id passwd in shell script ni3b007 Shell Programming and Scripting 1 12-05-2013 03:24 AM
keyboard and mouse dont work after sometime from boot in linux. sanjay.login UNIX for Dummies Questions & Answers 1 02-23-2010 06:55 AM
Substitution in a file dont work with an Array in filename Lock3 Shell Programming and Scripting 0 01-08-2010 02:48 AM
if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work wiseguy Shell Programming and Scripting 1 01-27-2009 08:46 AM
why the date format dont work in crontab p4cldba UNIX for Advanced & Expert Users 7 06-30-2008 08:09 PM



All times are GMT -4. The time now is 05:51 PM.