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

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 12-06-2016
Peasant's Unix or Linux Image
Peasant Peasant is offline Forum Advisor  
Registered User
 
Join Date: Mar 2011
Last Activity: 24 April 2017, 5:49 AM EDT
Posts: 1,037
Thanks: 28
Thanked 298 Times in 261 Posts
If you have spaces.

Code:
+ mv untitled folder.tar untitled folder.tar
mv: target `folder.tar' is not a directory

Don't escape in input, rather double quote the variables mv command
mv "$var" "$var id"

Spaces and other 'special' chars are something you would want to avoid if possible when creating file system hierarchy.

Try to sanitize the input (ab)user puts in (like spaces or exclamation marks and such), then execute.

You might want to consider getopts shell builtin to create a script and pass your variables as options (which much more control, ability to automate later etc.)

Hope that helps
Regards
Peasant.
Sponsored Links
    #9  
Old Unix and Linux 12-06-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 April 2017, 3:34 PM EDT
Location: Aachen, Germany
Posts: 10,682
Thanks: 263
Thanked 3,282 Times in 3,024 Posts
As already pointed out by Peasant, the command mv untitled folder.tar untitled folder.tar sees four arguments due to the missing quotes around the variables to be expanded. On top, the shell expanded $id_ to the empty string, so source and target were identical leading to another error condition for mv.The "error log" output something went wrong with the mv command, please do manually was missing in posts #3 and #5, which was rather misleading in the analysis.
Sponsored Links
    #10  
Old Unix and Linux 12-06-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 12 April 2017, 3:55 PM EDT
Posts: 54
Thanks: 5
Thanked 3 Times in 2 Posts
i worked it out but thank you for everyones help, i had to quote all the 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

The Following 2 Users Say Thank You to robertkwild For This Useful Post:
Corona688 (12-06-2016), drl (12-06-2016)
    #11  
Old Unix and Linux 12-14-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 12 April 2017, 3:55 PM EDT
Posts: 54
Thanks: 5
Thanked 3 Times in 2 Posts
Shell script - archive tar script

hi all,

made a script and thought i would show you guys about it,



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 tar file"
            read -e tar

            base=$(basename "$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"_"$base" ; then
                echo "something went wrong with the mv command, please do manually"
                exit
            fi

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

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

        2)
            echo "please specify full path to directory you want to be made into a tar"
            read -e dir

            cd $dir
            cd ..

            base=$(basename "$dir")

            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 ! tar -cf "$id"_"$base".tar "$base" ; then
                echo "something went wrong with the tar, please do manually"
                exit
            fi

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

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

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

            ;;

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

            ;;

    esac

Sponsored Links
Reply

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 10:43 PM.