Reputation: 23
I have created a simple script to list the contents in a bunch of folders. The folder list has been extracted from a database and all exists in the system.
listOfFolders='/home/jca/List_of_folders.csv'
for folder in `cat $listOfFolders`
do
a=`expr substr $folder 1 2`
b=`expr substr $folder 3 2`
AbsolutePath=`printf $a'/'$b'/'$folder`
echo $AbsolutePath
# ls $AbsolutePath
done
If I run the script like that I get the correct path to each folder I want to ls
like this:
37/88/37886
38/28/38284
38/35/38359
15/74/15746
38/78/38789
38/79/38793
38/93/38934
38/93/38937
38/94/38941
39/17/39173
40/38/40380
39/36/39364
39/75/39752
39/83/39832
39/91/39910
If I randomly pick up one line and do an ls
and I get all the contents of the folder as expected.
When I comment the echo $AbsolutePath
and uncomment the ls $AbsolutePath
I get a list of errors:
: No such file or directory
: No such file or directory
I am pretty sure that it is the correct directory because when I perform the ls 39/91/39910
in the shell I get the expected result.
Please help.
Thanks,
Upvotes: 0
Views: 283
Reputation: 59516
Your experience is weird, probably you mistook something. If the output with the echo
is as you post it, the output of the ls
should not be error messages about empty strings (starting with a colon).
It might help you to debug your stuff using this:
printf "[%q]\n" "$AbsolutePath"
instead of the simple echo. Mind the quotes! They are important. When using the value of a variable almost always you should use double quotes, otherwise spaces and other nasty characters in the value have (mostly) unwanted effects. So better try this:
ls "$AbsolutePath"
Upvotes: 1
Reputation: 3389
'ls $AbsolutePath'
and "ls $AbsolutePath"
is not the same , inside the ' '
the $ symbol has not any power
Upvotes: 0