2004-11-15, 13:51(+01), Birger Blixt:
[...]
Quote:>> ================================
>> #!/bin/sh
>> for i in $(ls -R /home/user1/Test)
>> do
>> echo $i
>> done
>> ================================
>> The problem with this is that it puts each seperate word on its own
>> line. I cannot do anything with it like 'tail $i'.
>> T.
> Your shebang is /bin/sh, but $(command) is a bash thing, that works for you since /bin/sh is a link
> to /bin/bash on your machine. `command` is the sh syntax.
No, $(...) is a POSIX shell thing. /bin/sh nowadays is no more a
Bourne shell but a POSIX conformant shell (except in one or two
old-fashioned systems such as Solaris or Tru64).
Quote:> ls -R /home/user1/Test | while read line
> do
> echo "$line"
> done
> Remember , it's "$line" NOT $line
> $ touch "foo bar"
> $ ls foo* | while read line; do wc $line;done
> wc: cannot open foo
> wc: cannot open bar
> 0 0 0 total
> $ ls foo* | while read line; do wc "$line";done
> 0 0 0 foo bar
> $
Now, you can try the same after:
touch ./-c './foo\ bar' './foo '
It should be:
ls foo* | while IFS= read -r line; do wc -- "$line"; done
(read -r is not Bourne but is POSIX).
(Still, that doesn't work for filenames with newlines)
--
Stephane