如何比较Bash脚本字符串 [英] How to compare strings in Bash script
本文介绍了如何比较Bash脚本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要比较一些字符串变量(和做一些事情,如果他们匹配)。
I need to compare a variable to some string (and do something if they match).
推荐答案
试试这个:
if [ "$x" == "valid" ]; then
echo "x has the value 'valid'"
fi
如果你想要做的事时,他们不匹配,替换 ==
与!=
。你想要围绕 $引号X
,因为如果 $ X
是空的,你会得到如果[==有效] ...
这是一个语法错误。
If you want to do something when they don't match, replace ==
with !=
. You want the quotes around $x
, because if $x
is empty, you'll get if [ == "valid" ]...
which is a syntax error.
这篇关于如何比较Bash脚本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文