在if语句中使用管道不起作用 [英] Using pipe in if statement is not working
本文介绍了在if语句中使用管道不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Shell脚本的新手,并且对此感到困惑-我尝试了以下if子句:
I'm new to shell scripting and stuck with this - I tried this if clause:
if [ unrar l "$filename" | grep -P "\.(?:r\d\d|r\d\d\d|.rar)$ ];
then...
它不起作用,因此尝试了此调试输出-我没有输出到 $ grep_output
:
It's not working, so tried this debug output - I get no output to $grep_output
:
$grep_output = unrar l "$filename" | grep -P "\.(?:r\d\d|r\d\d\d|.rar)$"
如果我直接在shell上执行此操作,它将正常工作:
If I execute this directly on the shell it's working without any problems:
unrar l "$filename" | grep -P "\.(?:r\d\d|r\d\d\d|.rar)$
我的错误在哪里?在此先感谢!
Where is my mistake? Thanks in Advance!
推荐答案
删除方括号;这些不是 if
语句的语法的一部分,而是调用 test
命令的另一种方法,通常是为了使用二进制运算符:
Remove the square brackets; these are not part of the syntax of the if
statement, but an alternative way of invoking the test
command, usually in order to use a binary operator:
if unrar l "$filename" | grep -P "\.(?:r\d\d|r\d\d\d|.rar)$";
then...
fi
这篇关于在if语句中使用管道不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文