Linux shell 脚本中 for 循环的语法

发布于 2024-10-31 09:39:57 字数 303 浏览 7 评论 0原文

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

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

我不明白这个错误。

这是我的脚本

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情旳空白 2024-11-07 09:39:57

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

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

星星的軌跡 2024-11-07 09:39:57

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

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

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

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

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

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

∞琼窗梦回ˉ 2024-11-07 09:39:57

你的机器上有什么作用

ls -l /bin/sh

sh 设为 bash 的符号链接,然后就可以执行 sh ./test1.sh

What does

ls -l /bin/sh

give on your machine ?

Make sh a symbolic link to bash and then you can do sh ./test1.sh

﹏雨一样淡蓝的深情 2024-11-07 09:39:57

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

  • 您可能需要在 shebang 之后有一个空格(#! /bin/bash 而不是 #!/bin/bash)。然而,丹尼斯·里奇最初指定该空间是可选。此外,这不是您使用 Bourne shell 时遇到的错误(您会遇到语法错误:'('意外)。
  • 您使用的是 Windows 系统吗?只需刺一下这看起来不像是 Windows 错误。
  • 这是 Solaris 或 HP/UX 系统吗?它们可能不是运行真正版本的 Bash,或者可能是旧版本。但是,即使是最旧版本的 Bash 也可以识别 <。 code>for ((x;y;z)) 构造

#! /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
  • set -xv 将显示执行时的所有行。
  • > 如果是 BASH 或 Kornshell,则应显示一个值(您的 for 循环将在其中之一中工作)。
  • 仅当这是真正的 BASH 时才应设置 {$BASH_VERINFO[x]}。即使您在 BASH 中运行 Korn shell 也无需设置(与仍包含 bash 的 $SHELL 不同),

如果 for 循环仍然给您带来麻烦,只需将其删除即可。脚本,我们将了解您是否真的在执行 bash shell。

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

  • 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: '(' unexpected instead).
  • 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.

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 -xv will display all lines as they are executed.
  • The $RANDOM should 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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文