I have:
This is my zip file.zip
in path $dec
If I manually copy this code over to the terminal window, I get the correct output.
But, if I take the same exact code and put it into a .sh file and execute it with set -x
, I get this:
++ ls ''
ls: cannot access : No such file or directory
Here is the code I'm using...
#!/bin/bash
set -x
for k in $(ls "$dec"); do
echo $k
if [[ "$k" == "" ]];
then
echo -e "Nothing Here\n"
elif [[ "$k" =~ \.(doc|txt)$ ]];
then
echo "Document"
elif [[ "$k" =~ ".zip" ]];
then
echo -e "ZIP"
fi
done
Does anyone see why it will run when I manually copy it, but not when I execute it?
$dec
is already defined. I just left it out of this partial script.
~/Scripts/mypath$ echo $dec
/home/adam/Scripts/mypath/archived
If
$dec
has no value, you're doingls ""
. Try it and you'll see you get the same error message. Notels ""
is NOT the same asls
(without arguments). Check your script to see why$dec
is not defined.If, as you say,
$dec
is defined before you run the script, you probably need to export it so it's known to scripts you run.Read this for more information on
export
: https://stackoverflow.com/questions/7411455/what-does-export-do-in-shell-programming