如果/同时无法执行 Bash 脚本

Ano*_*ous 3 bash shell-script variable

我想如果有人指出我脚本中的错误。我从中学习的来源是如此错误,这就是为什么它让我感到困惑。

本脚本的用途:它将计算从用户输入的任何数字到数字 1 的数字

#!/bin/bash

echo -n Enter a number

read number

if (($number > 0))  ; then

index = $number

while [ $index => 1 ]  ; do

echo $index

((index--))



break
done
fi    
Run Code Online (Sandbox Code Playgroud)

它给出的错误:索引:找不到命令

Sun*_*eep 7

  • index = $number不能在=变量赋值中使用空格.. 使用index=$number((index = number))
  • [ $index => 1 ]我想你想检查是否index大于或等于 1,使用[ $index -ge 1 ]((index >= 1))
  • 为什么break使用该语句?它用于退出循环
  • if并不需要声明
  • 您还可以使用read -p选项为用户添加消息

把它们放在一起:

#!/bin/bash

read -p 'Enter a number: ' number

while ((number >= 1)) ; do
    echo $number
    ((number--))
done
Run Code Online (Sandbox Code Playgroud)