有效的赋值运算符Bash

时间:2013-01-20 12:00:18

标签: linux bash shell command-line sh

在我的bash脚本中,我试图使用equals(=)运算符作为IF语句的输入,可以这样做吗?

echo "Plese enter an operator as shown in the brackets (-)subtract, (*)Multiply, (+)Add, (/)divide, quit(=)?"
read operator
..........

if [[ $operator == '=' ]]; then 
'do something'
fi

2 个答案:

答案 0 :(得分:2)

我不明白为什么你无法做到这一点。

> operator==
> echo $operator
=
> if [[ $operator == '=' ]]; then echo 'ok'; fi;
ok

答案 1 :(得分:1)

试试这个shell.sh

#!/bin/sh
EQUAL="=" #this is our operator
echo "Please enter operator"
read operator

if [ $operator = $EQUAL ]; then
    echo "Entered = "
else
    echo "Not = entered"
fi

执行:

:~$ sh shell.sh
Please enter operator
=
Entered = 
:~$ sh shell.sh
Please enter operator
-
Not = entered
:~$ 

Reference to learn