I have created a shell script to complete 3 simple tasks (zip a directory, rename the final file and delete the source directory). The script source is below:
#!/bin/bash
if [[ $2 == *"/data/"* ]]
then
src= $2"/*";
dest= $1".zip";
# Just for test
echo $src;
# Commented for now
#zip -r -j $dest $src
#mv $dest $1
#rm -rf $2
else
echo "Invalid arguments";
fi
When I run the script above with these parameters I have with error:
./scriptzip.sh /data/file-new /data/file-old
./scriptzip.sh: line 4: /data/file-old/*: No such file or directory
./scriptzip.sh: line 5: /data/file-new.zip: No such file or directory
How can prevent the shell to test a variable for "directory like" string? There is any way to skip that?
The space is significant in the shell, as that's what divides the tokens in commands such as
echo foo bar
or worserm -rf /some/where /
. Hence, assignment must not include spaces around the=
:Variables may also need quoting, depending on exactly what you want to happen for here glob expansion and also word splitting (incorrect word splitting to
rm
can result in unexpected (and hilarious) filesystem cleanups).