user258030
user258030

Reputation: 305

bash copy file to new desintation if it doesnt exists make a new directory

Hello i am currently optimizing some code i have and am now wondering why this snippet of code does not work.

what is supposed to happen is when i copy the file i have to type in a destination for the copy if i type a destination that doesn't exist then the script will create a directory and copy that file to it.

this is what i have and i am wondering why it is not working

echo "Current Directory "
        ls -a;
        echo -n "Please Enter file name to Copy: "
        read fileToCopy
        echo -n "Enter Destination for Copy: ~/ "
        read location
        if [ -d $location ]
        then
                cp $fileToCopy $location
                echo "File Successfully Copied to ~/ $location "
        elif [ !-d $location ]
        then
                mkdir $location
                cp $fileToCopy $location
                echo "$location was created and the File was Copied to It! "
                echo -n "Press Enter to Continue: "
        else
                echo "That file Does Not Exist! "
        fi

Upvotes: 0

Views: 939

Answers (2)

David W.
David W.

Reputation: 107040

It's always good to error check. What if the user didn't enter a directory name or a file to copy?

In shell, a null variable will cause an error in a statement like this:

if [ $dir -eq foo ]
then
    …
fi

That's because the shell will directly substitute the value and then interpret the line. If $dir is null, the statement will be:

if [ -eq foo ]
then
    …
fi

This isn't valid. If you had quotes around $dir:

if [ "$dir" -eq foo ]
then
    …
fi

You'd get this:

if [ "" -eq foo ]
then
    …
fi

Which is valid.

If you're using bash it's preferable to use the double square brackets:

if [[ $dir -eq foo ]]
then
    …
fi

This is a special improved syntactic test that will handle null variables and variables with spaces without having to use quotes.

The ! is a special shell operator that negates the return value of a command/statement. For the shell to understand the operator, you must make sure there is white space on either side of it. Also test the output of your mkdir statement to make sure it worked. And use the -p parameter. This will create parent directories.

 if [[ ! -d "$dir" ]]
 then
     if ! mkdir -p "$dir" 
     then
         echo "Could not create dir '$dir'" 1>&2
         exit 2
     fi
 fi

Note that I always put quotes around my variable and I use the if statement to test to see if my command succeeded or not. After all, the user might have tried to create a directory where I don't have write permission.

One more secret trick. You can use set -xv to turn on shell script debugging. This will print out each statement as written and then as the shell interprets it. To turn it off use set +xv.

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77095

Two issues:

One:

elif [ !-d $location ]

You need a space between ! and -d.

Two:

Quote your variables.

Upvotes: 1

Related Questions