views:

63

answers:

3

I have a file called inp.txt which lists 3 directory names

#!/bin/sh
while read dirname
do
        echo $dirname
        "ls -l" $dirname
done < inp.txt

When I run the above, I get this error:

line 5: ls -l: command not found

If I do just "ls" instead of "ls -l", it works fine. What am I missing here?

+1  A: 

You should remove the quotes around ls -l.

schot
+7  A: 

Get rid of the quotes.

while read dirname
do
        echo "$dirname"
        ls -l "$dirname"
done < inp.txt

When you have quotes you're saying, "treat this as a single word." The shell looks for an executable named ls -l rather than passing the argument -l to the command ls.

Nitpicker's note: If you want to use quotes properly, add them around "$dirname". Otherwise, if you had a directory named "Music Files" with a space in the name, without quotes your script would treat that as two directory names and print something like:

ls: Music: No such file or directory
ls: Files: No such file or directory
John Kugelman
ah ok...so I was putting the quotes at the wrong place :-)...Thanks!
sunny8107
+1  A: 

The shell interprets space as argument separaters. By putting the quotes around something, you force shell to see it as one argument. When executing, the shell interprets the first argument as the command to execute. In this case you're telling it to execute the command named "ls -l" with no arguments, instead of "ls" with argument "-l".

jdizzle
yep, learned that lesson!
sunny8107