bash 带有 wc -l 的 Shell 脚本,如果语句不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9430542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script with wc -l, if statement ain't working
提问by user1204032
The problem is with this code:
问题在于这段代码:
words=`wc -l /home/tmp/logged.log | awk '{print }'`;
if [ $words == 26 ]
then
echo $words
echo Good
else
echo Not so good
fi
it always returns the else statement. Even tho the result is 26. I also tried
它总是返回 else 语句。即使结果是 26。我也试过
words=`wc -l < /home/jonathan/tmp/logged.log`;
采纳答案by triclosan
try to use [ $words -eq 26 ]
instead of [ $words == 26 ]
尝试使用[ $words -eq 26 ]
而不是[ $words == 26 ]
or [ 26 == 26 ]
to check that statement works properly
或[ 26 == 26 ]
检查该语句是否正常工作
回答by Shiplu Mokaddim
Because ==
is not valid. Use =
因为==
无效。用=
if [ $words = 26 ]
By the way you can use cut
instead of awk
.
顺便说一句,您可以使用cut
代替awk
.
wc -l /home/tmp/logged.log | cut -f1 -d" "
回答by rkosegi
Try this:
尝试这个:
words=`wc -l /home/tmp/logged.log | awk '{print }'`;
if test $words -eq 26; then
echo $words
echo Good
else
echo Not so good
fi