|
Prev: [ test_0 && test_1 ] fails
Next: sed
From: Greg Russell on 4 Apr 2008 12:45 On Fri, 04 Apr 2008 16:37:46 +0000, Greg Russell wrote: > $ if [ `wc -l /tmp/post | awk '{print $1}'` -gt 30 &&\ > ! `grep -l "\#00000" /tmp/post` ]; then cat /tmp/post; fi > [: missing `]' > $ > > Where is my syntax error, please? Geez, my head must be numb today: .... [ test_0 ] && [ test_1 ] ...
From: pk on 4 Apr 2008 12:59 Greg Russell wrote: > I need to cat a file if it has more than 30 lines and doesn't contain > "#00000". Either of the two tests below work fine individually, but when > combined with the && operator the test fails: > > $ if [ `wc -l /tmp/post | awk '{print $1}'` -gt 30 &&\ > ! `grep -l "\#00000" /tmp/post` ]; then cat /tmp/post; fi > [: missing `]' > $ Separate the tests: if [ foo ] && [ bar ]; then ... fi -- All the commands are tested with bash and GNU tools, so they may use nonstandard features. I try to mention when something is nonstandard (if I'm aware of that), but I may miss something. Corrections are welcome.
From: Ed Morton on 4 Apr 2008 13:00 On 4/4/2008 11:37 AM, Greg Russell wrote: > I need to cat a file if it has more than 30 lines and doesn't contain > "#00000". Either of the two tests below work fine individually, but when > combined with the && operator the test fails: > > $ if [ `wc -l /tmp/post | awk '{print $1}'` -gt 30 &&\ > ! `grep -l "\#00000" /tmp/post` ]; then cat /tmp/post; fi > [: missing `]' > $ > > Where is my syntax error, please? That seems kinda complicated. Try this: awk 'NR==FNR{if(/#00000/)exit;next} NR<32{exit} 1' /tmp/post /tmp/post Ed.
From: Wayne on 4 Apr 2008 13:28 Greg Russell wrote: > I need to cat a file if it has more than 30 lines and doesn't contain > "#00000". Either of the two tests below work fine individually, but when > combined with the && operator the test fails: > > $ if [ `wc -l /tmp/post | awk '{print $1}'` -gt 30 &&\ > ! `grep -l "\#00000" /tmp/post` ]; then cat /tmp/post; fi > [: missing `]' > $ > > Where is my syntax error, please? The AND operator is '-a' not '&&'. Use '&&" to run one command (such as test) after another has succeeded. -Wayne
From: Dan Stromberg on 5 Apr 2008 01:30 On Fri, 04 Apr 2008 13:28:21 -0400, Wayne wrote: > Greg Russell wrote: >> I need to cat a file if it has more than 30 lines and doesn't contain >> "#00000". Either of the two tests below work fine individually, but >> when combined with the && operator the test fails: >> >> $ if [ `wc -l /tmp/post | awk '{print $1}'` -gt 30 &&\ >> ! `grep -l "\#00000" /tmp/post` ]; then cat /tmp/post; fi >> [: missing `]' >> $ >> >> Where is my syntax error, please? > > The AND operator is '-a' not '&&'. Use '&&" to run one command (such as > test) after another has succeeded. > > -Wayne [ cond1 ] && [ cond2 ] is a little more portable than [ cond1 -a cond2 ] - but I doubt there are many shells still around that don't do -a well. Specifically, I believe some -a's bind like multiplication with -o binding like addition, but in other tests the binding is left to right or right to left.
|
Pages: 1 Prev: [ test_0 && test_1 ] fails Next: sed |