Shell script - if statements dont work


 
Thread Tools Search this Thread
Top Forums Programming Shell script - if statements dont work
# 8  
Old 12-06-2016
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.
# 9  
Old 12-06-2016
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.
# 10  
Old 12-06-2016
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

These 2 Users Gave Thanks to robertkwild For This Post:
# 11  
Old 12-14-2016
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dont want to mention user id passwd in shell script

Hi, i have one shell script which transfers files from one server to other server through FTP, but i can see login id and password is not mentioned. kindly help to understand the script.then how below script is working if login and password is not mentioned in script #!/bin/sh... (1 Reply)
Discussion started by: ni3b007
1 Replies

2. Programming

Breakpoints dont work in Codeblocks, code does not build

Hey so I've written a program and it crashes every time I run it. I tried placing breakpoints to see what was going but to my surprise they do not work! I've placed them anywhere and they do not stop the execution of the program. In fact if I add code and hit rebuild it doesn't even build that... (1 Reply)
Discussion started by: Cambria
1 Replies

3. Shell Programming and Scripting

Can we use two shebang statements in a single shell script?

Hi, As per my understanding, we can use two shebang statements in a single shell script. Please see below snippet- #!/bin/bash .......## some code A #!/bin/csh .......## some code B exit 0; Here, code A will be executed using bash shell and code B will be executed with c shell. ... (9 Replies)
Discussion started by: tarunmudgal4u
9 Replies

4. UNIX for Dummies Questions & Answers

keyboard and mouse dont work after sometime from boot in linux.

when i started booting linux ,after sometime both the mouse and the keyboard are getting freeze and wont respond(also the system gets hanged and all the process running dont go forward) ,nothing seem to work. Keeping in mind that it will work after sometime ,i waited for one hour also ,but... (1 Reply)
Discussion started by: sanjay.login
1 Replies

5. Shell Programming and Scripting

Substitution in a file dont work with an Array in filename

Hi. I´ve a script that should substitude the 8th line in a file called xxx.num6. The "xxx" is set by an array filled with this command: j=0 for Par in *.sys ; do Par=`echo $Par | sed 's/\(.*\).sys/\1/'` ; Par2="$Par" ; echo "${Par2}" j=$((j + 1)); done Now i try... (0 Replies)
Discussion started by: Lock3
0 Replies

6. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

7. Shell Programming and Scripting

if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work

Hallo, I need to test a String (a special ip number-string). So I want to run that: ipadress=172.0.0.0 # for debugging: echo $ipadress | egrep -e '172\.?\.??\.??$' # the test that doesnt work if test -z `echo $ipadress | egrep -e '172\.?\.??\.??$'` != "" then echo "match" else... (1 Reply)
Discussion started by: wiseguy
1 Replies

8. UNIX for Advanced & Expert Users

why the date format dont work in crontab

Hi I tried to put a cron job which pipes the logfile appended to date +%d but it didnt work . anyone know how to make this happen thanks in advance -prasad (7 Replies)
Discussion started by: p4cldba
7 Replies

9. Shell Programming and Scripting

Running remote shell script containing sql statements

I have a shell script which resides on three SCO machines containing some simple sqlplus statments. I need to run these scripts remotely. Currently, I am trying to use rsh to do so: rsh hostname myscript args The problem is that the arguments to the sqlplus statements in the remote shell... (4 Replies)
Discussion started by: Madbreaks
4 Replies

10. Filesystems, Disks and Memory

OS X / fsck / apps dont work no control panel

Alright. My neighbor kid brings me his iBook and says nothing works. He decided to move the file system to his likeness and that semed to create the problems. Now he doesn't have access rights to his home. I have run fsck over and over again and I keep getting ** fs modified ** I have also... (0 Replies)
Discussion started by: maxwell
0 Replies
Login or Register to Ask a Question