linux shell脚本中for循环的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5627179/
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-08-05 03:37:11  来源:igfitidea点击:

syntax of for loop in linux shell scripting

linuxbashshellfor-loopdash-shell

提问by mkab

I have a problem implementing a for loop. I get this error when I execute my script

我在实现 for 循环时遇到问题。执行脚本时出现此错误

test1.sh: 2: Syntax error: Bad for loop variable

test1.sh: 2: 语法错误:循环变量错误

I don't understand this error.

我不明白这个错误。

This is my script

这是我的脚本

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done

can any one tell me syntax for for loop in sh(in ubuntu it links to dash shell) shell in ubuntu?

谁能告诉我 ubuntu 中 sh(在 ubuntu 中它链接到 dash shell)shell 中 for 循环的语法?

采纳答案by Michael Krelin - hacker

You probably run it with sh, not bash. Try bash test1.sh, or ./test1.shif it's executable, but not sh test1.sh.

您可能使用sh,而不是 来运行它bash。尝试bash test1.sh,或者./test1.sh如果它是可执行的,但不是sh test1.sh

回答by David W.

Your shell script (as shown) runs in both Korn shell and Bash. Some thoughts:

您的 shell 脚本(如图所示)在 Korn shell 和 Bash 中运行。一些想法:

  • You might need a space after the shebang (#! /bin/bash and not #!/bin/bash). However, Dennis Ritchie had originally specified the space is optional. Besides, it isn't the error you get with Bourne shell (you get syntax error: '(' unexpectedinstead).
  • Are you on a Windows system? Just a stab in the dark. This doesn't look like a Windows error.
  • Is this Solaris or HP/UX system? They might not be running true versions of Bash, or maybe an older version. However, even the oldest version of Bash recognizes the for ((x;y;z))construct.
  • 您可能需要在 shebang 之后留一个空格(#!/bin/bash 而不是 #!/bin/bash)。然而,丹尼斯·里奇最初指定的空格是可选的。此外,这不是您使用 Bourne shell 遇到的错误(您会得到syntax error: '(' unexpected)。
  • 你在Windows系统上吗?只是在黑暗中刺伤。这看起来不像是 Windows 错误。
  • 这是 Solaris 还是 HP/UX 系统?他们可能没有运行真正的 Bash 版本,或者可能是旧版本。然而,即使是最旧版本的 Bash 也能识别该for ((x;y;z))构造。

Try this:

尝试这个:

#! /bin/bash
set -vx
echo "Random = $RANDOM"   #Test for bash/Kornshell. Will be blank in other shells
echo $BASH_VERSINFO[0] = ${BASH_VERSINFO[0]} #Should only work in BASH
echo $BASH_VERSINFO[1] = ${BASH_VERSINFO[1]}
echo $BASH_VERSINFO[2] = ${BASH_VERSINFO[2]}
echo $BASH_VERSINFO[3] = ${BASH_VERSINFO[3]}
echo $BASH_VERSINFO[4] = ${BASH_VERSINFO[4]}
echo $BASH_VERSINFO[5] = ${BASH_VERSINFO[5]}
for ((c=0, c<=5, c++))
do
    echo "Welcome $c times"
done
  • The set -xvwill display all lines as they are executed.
  • The $RANDOMshould display a value if this is either BASH or Kornshell (your for loop will work in either one).
  • The {$BASH_VERINFO[x]}should only be set if this is truly BASH. These aren't even set even if you run Korn shell after you're in BASH (unlike $SHELL which will still contain bash).
  • set -xv执行时为它们会显示所有行。
  • $RANDOM,如果这是无论是bash或Kornshell(您的循环将在任何一个工作)应显示的值。
  • {$BASH_VERINFO[x]},如果这是真正的BASH只应设置。即使您在 BASH 中运行 Korn shell(与仍包含 的 $SHELL 不同),这些甚至都不会设置bash

If the for loop still gives you trouble, just delete it. Somewhere in this script, we'll find out if you're really executing a bash shell or not.

如果 for 循环仍然给您带来麻烦,只需将其删除即可。在这个脚本的某个地方,我们会发现你是否真的在执行一个 bash shell。

回答by Ken Bloom

A standard POSIX shell only accepts the syntax for varname in list

标准的 POSIX shell 只接受语法 for varname in list

The C-like for-loop syntax for (( expr1; expr2; expr3 ))is a bashism.

类似 C 的 for 循环语法for (( expr1; expr2; expr3 ))是一种 bashism。

You can get similar behavior in the standard POSIX shell using for c in $(seq 1 5)

您可以使用以下命令在标准 POSIX shell 中获得类似的行为 for c in $(seq 1 5)

回答by Ankur Agarwal

What does

有什么作用

ls -l /bin/sh

give on your machine ?

给你的机器?

Make sha symbolic link to bashand then you can do sh ./test1.sh

建立sh一个符号链接bash,然后你可以做sh ./test1.sh