本文介绍了在bash中,我怎么能检查一个字符串开头一定的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查一个字符串节点例如开始node001。类似
I would like to check if a string begins with "node" e.g. "node001". Something like
if [ $HOST == user* ]
then
echo yes
fi
我如何做是正确的?
How can I do it correctly?
我还需要结合前pressions检查HOST可以是用户1或节点
I further need to combine expressions to check if HOST is either "user1" or begins with "node"
if [ [[ $HOST == user1 ]] -o [[ $HOST == node* ]] ];
then
echo yes
fi
> > > -bash: [: too many arguments
如何正确呢?
推荐答案
此片段在说:
# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.
[[ $a == z* ]] # True if $a starts with an "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
所以,你有它的近的正确;你所需要的双的支架,不是单一的括号内。
So you had it nearly correct; you needed double brackets, not single brackets.
至于你的第二个问题,你可以这样写:
With regards to your second question, you can write it this way:
HOST=user1
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes1
fi
HOST=node001
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes2
fi
这将呼应
yes1
yes2
击的如果
语法是很难习惯(IMO)。
Bash's if
syntax is hard to get used to (IMO).
这篇关于在bash中,我怎么能检查一个字符串开头一定的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!