Hello, On Fri, 21 Jan 2011, Joachim Schrod wrote:
David Haller wrote:
On Thu, 20 Jan 2011, David C. Rankin wrote:
I stumbled across a neat use of ls -A for testing for an empty dir that I thought I would pass on:
#!/bin/bash
if [[ $(ls -A ${1}) ]]; then ^^^^ *BLAM* _ALWAYS_ QUOTE ARGUMENTS
And I'd prefer the more readable and portable
if test -n "$(ls -A "$1")"; then
David, I know that you know it; but for the sake of the archives:
Quotes are wrong, that should be "$(ls -A \"$1\")".
Actually, with $(), they're not only correct, escaping them is wrong: $ bash -c 'set -x; for f in *; do ls -b "$(ls -A -- \"$f\")"; done;' + for f in '*' ++ ls -A -- '"a' 'b"' ls: cannot access "a: No such file or directory ls: cannot access b": No such file or directory With `` though, you're right, should be escaped. Wonderingly, I realize while testing, that even ash does it right with $() and ``. Testbed: $ ls -b a\nb a\ b a"b a'b a*b a_b $ ash -c 'set -x; for f in *; do ls -b "$(ls -A -- "$f")"; done;' [..] $ ash -c 'set -x; for f in *; do ls -b "`ls -A -- "$f"`"; done;' [..] $ bash -c 'set -x; for f in *; do ls -b "$(ls -A -- "$f")"; done;' [..] $ ksh -c 'set -x; for f in *; do ls -b "`ls -A -- "$f"`"; done;' [..] etc. So, use "$( "" )" with $() (always ""), use "` \"\" `" with `` (escaping depending on level).
If the directory name starts with an hyphen, it'll get an error, too. So better make it "$(ls -A -- \"$1\")".
Yes. -dnh -- Computers make very fast, very accurate mistakes. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org