【问题标题】:Valid assignment operators Bash有效的赋值运算符 Bash
【发布时间】:2013-01-03 15:51:01
【问题描述】:

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

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

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

【问题讨论】:

  • 你试过了吗?成功了吗?

标签: linux bash shell command-line sh


【解决方案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

【讨论】:

    【解决方案2】:

    我不明白你为什么不能这样做。

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

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 2014-11-08
      • 2011-08-02
      • 2019-10-06
      • 2011-11-16
      • 2020-01-30
      • 1970-01-01
      • 2013-11-26
      • 2013-11-30
      相关资源
      最近更新 更多