如何将字符串与 Bash 中的多个正确值进行比较? [英] How can I compare a string to multiple correct values in Bash?

查看:40
本文介绍了如何将字符串与 Bash 中的多个正确值进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Bash 脚本:

I have the following piece of Bash script:

function get_cms {
    echo "input cms name"
    read cms
    cms=${cms,,}
    if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then
        get_cms
    fi
}

但是无论我输入什么(正确和不正确的值),它都不会再次调用该函数,因为我只想允许这 3 个输入中的 1 个.

But no matter what I input (correct and incorrect values), it never calls the function again, because I only want to allow 1 of those 3 inputs.

我用 ||[ var != value ] 或 [ var != value1 ] 或 [ var != value1 ] 尝试过,但没有任何效果.

I have tried it with ||, with [ var != value ] or [ var != value1 ] or [ var != value1 ], but nothing works.

有人能指出我正确的方向吗?

Can someone point me in the right direction?

推荐答案

而不是说:

if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then

说:

if [[ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]]; then

您可能还想参考条件构造.

这篇关于如何将字符串与 Bash 中的多个正确值进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆