vendredi 6 février 2015

How to pass default value to a variable in shell script?

I have written this piece of code to move one directory to another. What I want with this code is as follows:



  1. User will give a file name or a directory name.

  2. User may give a destination folder. If user doesn't want a destination folder he/she will just press enter.

  3. Then source directory will be copied to destination directory(if given) or a default directory(if destination not given)


Here is the code.



$source_folder
$destination_folder
$destination
read -r directory_source
read -r destination_folder
if [ "$destination_folder" = ""]
then
$destination = "/var/www/html/"
else
$destination = "$destination_folder"
fi
cp -a "$source_folder" "$destination"


Here is the input to this program:



Myfiles.sh (called the shell script in terminal)
Max Sum (This is the source directory)
(Pressed enter i.e. No destination given)


It gives following error:



/usr/local/myfiles/copyDirToHTML.sh: line 6: [: : unary operator expected
/usr/local/myfiles/copyDirToHTML.sh: line 10: =: command not found
cp: cannot stat ‘’: No such file or directory

Aucun commentaire:

Enregistrer un commentaire