我想检查一个字符串是否以“节点”开始,例如。“node001”。类似的

if [ $HOST == user* ]
  then
  echo yes
fi

我怎样才能正确地做呢?


我进一步需要组合表达式来检查HOST是否为“user1”或以“node”开头

if [ [[ $HOST == user1 ]] -o [[ $HOST == node* ]] ];
then
echo yes
fi

> > > -bash: [: too many arguments

我怎样才能正确地做呢?


当前回答

我更喜欢已经发布的其他方法,但有些人喜欢使用:

case "$HOST" in 
    user1|node*) 
            echo "yes";;
        *)
            echo "no";;
esac

编辑:

我已经在上面的案例陈述中添加了你的替补

在你编辑过的版本中,括号太多了。它应该是这样的:

if [[ $HOST == user1 || $HOST == node* ]];

其他回答

如果您正在使用最新版本的Bash (v3+),我建议使用Bash正则表达式比较运算符=~,例如,

if [[ "$HOST" =~ ^user.* ]]; then
    echo "yes"
fi

要在正则表达式中匹配这个或那个,使用|,例如,

if [[ "$HOST" =~ ^user.*|^host1 ]]; then
    echo "yes"
fi

注意——这是“正确的”正则表达式语法。

User *表示使用和零次或多次出现r,因此使用和userrrr将匹配。 用户。*表示用户和零或多次出现任何字符,因此user1, userX将匹配。 ^用户。*表示匹配模式用户。*在$HOST的开头。

如果您不熟悉正则表达式语法,请尝试参考此资源。

请注意,Bash =~操作符仅在右侧为unquote时进行正则表达式匹配。如果你在右边引用,“模式的任何部分都可以被引用,以迫使它作为字符串进行匹配。”即使在进行参数展开时,也不应该在右边引用。

grep

忘记性能,这是POSIX,看起来比大小写解决方案更好:

mystr="abcd"
if printf '%s' "$mystr" | grep -Eq '^ab'; then
  echo matches
fi

解释:

printf '%s'以防止printf扩展反斜杠转义:Bash printf字面逐字串 grep -q防止匹配回显到stdout:如何使用Bash检查文件是否包含特定字符串 grep -E启用扩展正则表达式,这是^ . grep所需要的

保持简单

word="appel"

if [[ $word = a* ]]
then
  echo "Starts with a"
else
  echo "No match"
fi

我更喜欢已经发布的其他方法,但有些人喜欢使用:

case "$HOST" in 
    user1|node*) 
            echo "yes";;
        *)
            echo "no";;
esac

编辑:

我已经在上面的案例陈述中添加了你的替补

在你编辑过的版本中,括号太多了。它应该是这样的:

if [[ $HOST == user1 || $HOST == node* ]];
if [ [[ $HOST == user1 ]] -o [[ $HOST == node* ]] ];
then
echo yes
fi

不起作用,因为所有的[、[[和test都识别相同的非递归语法。请参阅Bash手册页上的条件表达式部分。

说句题外话,SUSv3说

The KornShell-derived conditional command (double bracket [[]]) was removed from the shell command language description in an early proposal. Objections were raised that the real problem is misuse of the test command ([), and putting it into the shell is the wrong way to fix the problem. Instead, proper documentation and a new shell reserved word (!) are sufficient. Tests that require multiple test operations can be done at the shell level using individual invocations of the test command and shell logicals, rather than using the error-prone -o flag of test.

你需要这样写,但是test不支持:

if [ $HOST == user1 -o $HOST == node* ];
then
echo yes
fi

Test使用=表示字符串相等,更重要的是它不支持模式匹配。

Case / esac对模式匹配有很好的支持:

case $HOST in
user1|node*) echo yes ;;
esac

它还有一个额外的好处,那就是它不依赖于Bash,而且语法是可移植的。从单一Unix规范,Shell命令语言:

case word in
    [(]pattern1) compound-list;;
    [[(]pattern[ | pattern] ... ) compound-list;;] ...
    [[(]pattern[ | pattern] ... ) compound-list]
esac