[ test_0 && test_1 ] fails
[ test_0 && test_1 ] fails
am 04.04.2008 18:37:46 von Hans-Peter Sauer
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?
Re: [ test_0 && test_1 ] fails
am 04.04.2008 18:45:30 von Hans-Peter Sauer
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 ] ...
Re: [ test_0 && test_1 ] fails
am 04.04.2008 18:59:25 von PK
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.
Re: [ test_0 && test_1 ] fails
am 04.04.2008 19:00:16 von Ed Morton
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.
Re: [ test_0 && test_1 ] fails
am 04.04.2008 19:28:21 von wayne
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
Re: [ test_0 && test_1 ] fails
am 05.04.2008 07:30:43 von Dan Stromberg
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.