猛砸,检查变量 [英] Bash, checking variable

查看:97
本文介绍了猛砸,检查变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想运行一个脚本,如果用户输入,然后输出,但如果用户输入 N N 然后输出 NO ,这是我迄今为止:

Ok, so I'm trying to run a script where if the user enters y or Y, then the output is YES but if the user inputs n or N then the output is NO, this is what I have so far:

read character
if (( ("$character") == "y" )) || (( ("$character" == "Y") )); then
echo "YES"
else
echo "NO"
fi

当我运行这个code,我得到成功,但输入 N 只是导致反正。

When I run this code I get YES successfully but inputting n just results in YES anyway.

我不知道我做错了任何我想吃的输入。

I have no idea what I'm doing wrong and any I'd love some input.

在此先感谢!

推荐答案

((...))用于数学计算。您可以使用 [...] 在BASH是这样的:

((...)) is used for math calculations. You can use [[...]] in BASH like this:

read character
if [[ "$character" == [yY] ]]; then
   echo "YES"
else
   echo "NO"
fi

此外,我已经联合2个条件为一个使用 [YY] glob模式。

这篇关于猛砸,检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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