尝试使用bash验证文本输入

时间:2016-09-22 07:30:34

标签: bash if-statement

我试图验证简单的文本输入。 我需要验证输入是“是”还是“否”。 我正在使用以下代码:

#!/bin/bash
IsSSL=$1
if [$IsSSL = "Yes"] || [$IsSSL = "No"];
then 
        echo "correct input"
else
        echo "incorrect input"
fi

但我一直收到以下错误:

#sh test.sh df
test.sh: line 3: [df: command not found
test.sh: line 3: [df: command not found
incorrect input

知道我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

试试这个;

{{1}}

在bash中,如果条件

,则需要[和]周围的空格

答案 1 :(得分:0)

#!/bin/bash
IsSSL=$1
if [ $IsSSL = "Yes" ] || [ $IsSSL = "No" ] ;
then 
        echo "correct input"
else
        echo "incorrect input"
fi

[周围需要空格。

使用shellcheck这样的内容。

此外,您应该始终引用您的变量,以避免其中包含特殊字符的并发症。所以你的病情应该是 if [ "$IsSSL" = "Yes" ] || [ "$IsSSL" = "No" ];