[opensuse] Re: bash test for empty dir
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\")". If the directory name starts with an hyphen, it'll get an error, too. So better make it "$(ls -A -- \"$1\")". Joachim -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Joachim Schrod Email: jschrod@acm.org Roedermark, Germany -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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
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
On Friday, January 21, 2011 07:40:06 pm David Haller wrote: 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
David, Anders, Joachim: Thank you all as always. Your feedback provides insight of not only 'howto' but 'why'. That's worth it's weight in gold :-) -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Tue, 25 Jan 2011, David C. Rankin wrote:
Thank you all as always. Your feedback provides insight of not only 'howto' but 'why'. That's worth it's weight in gold :-)
How much do a couple dozen bytes weigh? -dnh -- Dinner not ready...(A)bort (R)etry (P)izza -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Thu, 27 Jan 2011, James Knott wrote:
David Haller wrote:
How much do a couple dozen bytes weigh? Well, a byte is 8 bits or 4 x 2 bits = $1.00. So how much does a quarter weigh? ;-)
Ahhh, yes, nice way of conversion! You know, there are Dollar coins minted weighing 8.100 g (0.260 troy oz). I'll use Quarters (5.670g / 0.1823 troy oz) though (both weights from en.wikipedia.org). So at current rates for gold[1]: 1 Byte = 8 Bits = 4 Quarters ~= 0.73 troy oz in gold ~= 954 US$ dcr, that could get expensive for you ("couple dozen" >= ~22 kUSD), even using dollar coins would be ~340 USD/Byte ;P -dn'*SCNR*'h, having calculated his weight to be about 3.6e+37 eV (or 3.6e+13 YeV), according to E=mc² :) Inconvenient, that 'units' doesn't do the kg <=> J conversion. I've noted the way of the conversion though. I suggest EOT/fup2poster ;) [1] I updated units.dat with todays rate from xe.com, and then used 'units "0.7292 goldounces"' for the conversion. The "shipped" rate of the units.dat in oS 11.2 of ~728 USD/troy oz seemed a bit way off the current rate. So I'm still On-Topic :) -- [..] alas, reminded me of the old definition of mixed emotions: "Watching your mother-in-law drive off a cliff in your new truck." -- Kevin Martin -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
David C. Rankin
-
David Haller
-
James Knott
-
Joachim Schrod