红联Linux门户
Linux帮助

linux下shell的if语句如何写?

发布时间:2009-08-04 16:03:04来源:红联作者:dolphin1030
在linux的bash环境下 执行如下脚本test
#!/bin/bash
echo "Please input Quantity:"
read Quantity
if $Quantity -eq 100
then
echo "The box is full."
else
echo "The box is not full."
fi
在我执行之后
Please input Quantity:
100
报错如下:
./test: line 4: 100: command not found
The box is not full.

请各位大虾们帮帮忙,我刚开始学习,不知道自己错在哪里,希望大家帮我指点下 谢谢
文章评论

共有 13 条评论

  1. lanlanlangzi 于 2011-11-02 15:28:02发表:

    学习中

  2. 白白 于 2011-10-25 12:56:09发表:

    学习了!!!

  3. zhoutingting 于 2011-10-02 17:49:38发表:

    read -p "please input quantity: " quantity
    quantity=${quantity:-0}
    if ((quantity == 100))
    then
    echo "box is full"
    else
    echo "box is not full"
    fi

  4. zhoutingting 于 2011-10-02 17:49:32发表:

    read -p "please input quantity: " quantity
    quantity=${quantity:-0}
    if ((quantity == 100))
    then
    echo "box is full"
    else
    echo "box is not full"
    fi

  5. zhoutingting 于 2011-10-02 17:49:01发表:

    #!/bin/bash
    echo "Please input Quantity:"
    read Quantity
    if [ $Quantity -eq 100 ]
    then
    echo "The box is full."
    else
    echo "The box is not full."
    fi

  6. wokanhaoziji 于 2011-09-28 11:07:15发表:

    那个 吧 for 与 if 结合 的 语句写一个 看看 需要 谢谢 :0wmjh(1

  7. 死皮赖狗 于 2009-08-05 17:03:55发表:

    学习了……

  8. dolphin1030 于 2009-08-04 23:06:11发表:

    太谢谢了,我也注意的自己错在哪里啦

  9. zhou_arron 于 2009-08-04 22:33:52发表:

    有了(( )),可以忘记数值判断-eq -lt -gt -le -ge了,一律== != > < >= <=
    var=${var:-0}是一种预防性写法,当未输入的时候,置一个缺省值,上面的例子,恰好quantity未初始化的时候是0。

  10. zhou_arron 于 2009-08-04 22:30:18发表:

    -eq没有错,你没有使用[ ]形成一个判断式。
    [ ]有一些麻烦的问题,bash从3.0开始支持[[ ]]和(( )),分别用于字符串和数值判断,方便了很多,建议使用。

  11. Silenthunter 于 2009-08-04 21:50:15发表:

    #!/bin/bash
    echo "Please input Quantity:"
    read Quantity
    if [ $Quantity -eq 100 ] # 注意[ ] 之间的空格
    then
    echo "The box is full."
    else
    echo "The box is not full."
    fi

  12. dolphin1030 于 2009-08-04 21:29:15发表:

    你好: zhou_arron
    我将你写的那个程序执行了,是可以的
    不过我就不明白为什么不能用 -eq这个运算符,还请你帮我解答

    好像没有quantity=${quantity:-0} 程序一样成功

  13. zhou_arron 于 2009-08-04 20:21:00发表:

    read -p "please input quantity: " quantity
    quantity=${quantity:-0}
    if ((quantity == 100))
    then
    echo "box is full"
    else
    echo "box is not full"
    fi