bash true/false redirection error
On bash 4.2.8(1)-release
(x86_64-pc-linux-gnu
) on Ubuntu 11.04 this command
[ $(wc -l /var/w开发者_StackOverflow社区ww/some.log|cut -d " " -f 1) -le 6 ] || echo "no"
yields
echo: command not found
for as long as the ||
clause would come into affect. The &&
clause is working. I have tried changing the order (||
first, &&
first) to no avail.
Funny things is,
true && echo "yes" || echo "no"
yields
yes
and
false && echo "yes" || echo "no"
yields
no
so I suppose something is wrong with my test-clause. I'd like to check if the file length of some.log is less or equal to 6, if not then do something else then if yes.
Double brackets in the test-clause don't not work, either.
The long-form of test does work, though:
test $(wc -l /var/www/some.log|cut -d " " -f 1) -le 6 && echo "yes" || echo "no"
yes
Funny, huh?
As always, thank you for any tips/hints on this.
Christian.
Try this (with 2 [ and ]):
[[ $(wc -l /var/www/some.log|cut -d " " -f 1) -le 6 ]] || echo "no"
^^ ^^
From the bash's man:
Compound Commands
...
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS.
I guess the single [ and ] is not a standard was of performing tests, even if it works on my side.
Edit:
Actually, [ and ] are a standard way of doing tests, it is a buildin command in bash. If [[ ... ]] works and [ ... ] is not working, I would not fully understand it. It might have something to do with expansion performed/not performed in [ and ].
This is the same problem as in your other question here - your space is 0xC2 0xA0
instead of 0x20
. Copy-pasted and stripped from your question:
$ echo " echo " | hexdump -C
00000000 20 c2 a0 65 63 68 6f 20 0a | ..echo .|
00000009
# ^^ ^^ ^^
After deleting and re-typing the two leading spaces:
$ echo " echo " | hexdump -C
00000000 20 20 65 63 68 6f 20 0a | echo .|
00000008
# ^^ ^
Not sure why yours isn't working
Try this
:>bash --version
GNU bash, version 3.1.17(1)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.
[ $( wc -l < /var/www/some.log ) -le 6 ] || echo no
no
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
精彩评论