bash 语法错误:循环变量错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30358065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:00:50  来源:igfitidea点击:

Syntax error: Bad for loop variable

bashsh

提问by Kwiatkowski

I'm trying to write a script that will vol up radio in the background

我正在尝试编写一个脚本,该脚本将在后台播放收音机

#!/bin/sh

for (( i = 80 ; i <= 101; i++ )) 
 do 
  amixer cset numid=1 i$% sleep 60;
done 

But i have problem:

但我有问题:

alarmclock-vol.sh: 3: alarmclock-vol.sh: Syntax error: Bad for loop variable

回答by geirha

The for (( expr ; expr ; expr ))syntax is not available in sh. Switch to bash or ksh93 if you want to use that syntax. Otherwise, the equivalent for sh is:

for (( expr ; expr ; expr ))语法不可用sh。如果要使用该语法,请切换到 bash 或 ksh93。否则, sh 的等价物是:

#!/bin/sh

i=80
while [ "$i" -le 101 ]; do
    amixer cset numid=1 "$i%"
    sleep 60
    i=$(( i + 1 ))
done