Bash 条件:如何“和”表达式?(如果 [ ! -z $VAR && -e $VAR ])
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8920245/
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
Bash conditionals: how to "and" expressions? (if [ ! -z $VAR && -e $VAR ])
提问by atxdba
I guess I'm not clear on how to do "and" tests. I wanted to make sure an argument existed which was working well with [ -e $VAR ]
, but it turns out that was also evaluating as true on an empty string; which I do not want.
我想我不清楚如何进行“和”测试。我想确保存在一个与 配合良好的参数[ -e $VAR ]
,但事实证明,它在空字符串上也被评估为真;我不想要。
How do I 'and' them together? Or is there another unary test that accomplishes what I want?
我如何“和”他们在一起?或者是否有另一个一元测试可以完成我想要的?
回答by jaypal singh
if [ ! -z "$var" ] && [ -e "$var" ]; then
# something ...
fi
回答by paxdiablo
From the bash
manpage:
从bash
联机帮助页:
[[ expression ]]
- return a status of 0 or 1 depending on the evaluation of the conditional expression expression.
[[ expression ]]
- 根据条件表达式表达式的计算结果返回 0 或 1 状态。
And, for expressions, one of the options is:
而且,对于表达式,选项之一是:
expression1 && expression2
- true if bothexpression1
andexpression2
are true.
expression1 && expression2
- 如果expression1
和expression2
都为真,则为真。
So you can and
them together as follows (-n
is the opposite of -z
so we can get rid of the !
):
因此,您可以and
按如下-n
方式将它们组合在一起(与-z
这样我们可以摆脱!
):
if [[ -n "$var" && -e "$var" ]] ; then
echo "'$var' is non-empty and the file exists"
fi
However, I don't think it's needed in this case, -e xyzzy
is true if the xyzzy
fileexists and can quite easily handle empty strings. If that's what you want then you don't actually need the -z
non-empty check:
但是,我认为在这种情况下不需要它,-e xyzzy
如果xyzzy
文件存在并且可以很容易地处理空字符串,则为 true 。如果那是您想要的,那么您实际上并不需要-z
非空检查:
pax> VAR=xyzzy
pax> if [[ -e $VAR ]] ; then echo yes ; fi
pax> VAR=/tmp
pax> if [[ -e $VAR ]] ; then echo yes ; fi
yes
In other words, just use:
换句话说,只需使用:
if [[ -e "$var" ]] ; then
echo "'$var' exists"
fi
回答by Slava Semushin
if [ -n "$var" -a -e "$var" ]; then
do something ...
fi
回答by user123444555621
Simply quote your variable:
简单地引用你的变量:
[ -e "$VAR" ]
This evaluates to [ -e "" ]
if $VAR
is empty.
这评估为[ -e "" ]
if$VAR
为空。
Your version does not work because it evaluates to [ -e ]
. Now in this case, bash simply checks if the single argument (-e
) is a non-empty string.
您的版本不起作用,因为它评估为[ -e ]
. 现在在这种情况下,bash 只是检查单个参数 ( -e
) 是否为非空字符串。
From the manpage:
从联机帮助页:
test and [ evaluate conditional expressions using a set of rules based on the number of arguments. ...
1 argument
The expression is true if and only if the argument is not null.
test 和 [ 使用一组基于参数数量的规则来评估条件表达式。...
1 个参数
当且仅当参数不为空时,表达式为真。
(Also, this solution has the additional benefit of working with filenames containing spaces)
(此外,此解决方案还具有使用包含空格的文件名的额外好处)
回答by rororo
I found an answer now. Thanks for your suggestions!
我现在找到了答案。感谢您的建议!
for e in ./*.cutoff.txt; do
if grep -q -E 'COX1|Cu-oxidase' $e
then
echo xyz >$e.match.txt
else
echo
fi
if grep -q -E 'AMO' $e
then
echo abc >$e.match.txt
else
echo
fi; done
Any comments on that? It seems inefficient to grep twice, but it works...
对此有何评论?grep 两次似乎效率低下,但它有效......