【问题标题】:why this simple while loop not working in bash script?为什么这个简单的 while 循环在 bash 脚本中不起作用?
【发布时间】:2020-06-11 13:45:40
【问题描述】:
#! /bin/bash
read -p "enter i:" I
while [ $I -lt 4 ]
do
echo $I
I=$[$I+1]
done

【问题讨论】:

  • “不工作”是什么意思?您收到错误/语法消息吗?它产生什么输出?你期待什么输出?
  • 请看editing help
  • “我的终端没有输出”就是我所说的“它不工作”的意思。
  • 您学习 Bash shell 的资料太旧或没有更新。 Bash 算术作为括号表达式自 1992 年以来已被弃用。(见这篇文章:stackoverflow.com/a/40048865/7939871

标签: linux bash unix


【解决方案1】:

在现代

read -p 'enter a positive integer < 4: >>> ' int

while ((int < 4 )); do
        echo "$int"
        ((int++))
done

((...))

是一个算术命令,如果表达式非零,则返回退出状态 0,如果表达式为零,则返回 1。如果需要副作用(分配),也用作“让”的同义词。见http://mywiki.wooledge.org/ArithmeticExpression

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2016-07-23
    相关资源
    最近更新 更多