linux - Foreach 循环不会运行

标签 linux shell

作业是修改此脚本以将 exec 作为参数,但首先我希望能够运行脚本以尝试弄清楚如何修改它

tcsh $ cat foreach_1
#!/bin/tcsh
# routine to zero-fill argv to 20 arguments
#

set buffer = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
set count = 1
#
if ($#argv > 20) goto toomany
#
foreach argument ($argv[*])
set buffer[$count] = $argument
@ count++
end
# REPLACE command ON THE NEXT LINE WITH
# THE PROGRAM YOU WANT TO CALL.
exec command $buffer[*]
#
toomany:
echo "Too many arguments given."
echo "Usage: foreach_1 [up to 20 arguments]"
exit 1

但是我在尝试运行它时遇到了这个错误:

./foreach_1: line 5: syntax error near unexpected token `('
./foreach_1: line 5: `set buffer = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)'

我没有任何额外的引号,那为什么会这样呢?

最佳答案

在许多 shell 中(我相信 tcsh 被算在 bourne 兼容程序中),您必须放置表达式的左侧,=,并且右侧全部直接彼此相邻。

# shorten the ` = ` to `=` below:
set buffer=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
set count=1

if ($#argv > 20) goto toomany
#
foreach argument ($argv[*])
set buffer[$count] = $argument
@ count++
end
# REPLACE command ON THE NEXT LINE WITH
# THE PROGRAM YOU WANT TO CALL.
exec command $buffer[*]
#
toomany:
echo "Too many arguments given."
echo "Usage: foreach_1 [up to 20 arguments]"
exit 1

关于linux - Foreach 循环不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204244/

相关文章:

perl - 在 csh 中重定向 stderr 时出现问题

linux - 了解 ldd 输出 - 符号链接(symbolic link)

linux - 关闭所有正在运行的虚拟机并再次启动自动启动机器

linux - 无法运行使用 'mkbundle' 命令构建的 bundle

linux - 如何显示启动时运行的脚本的终端?

shell - 如何查找 PWD 中引用 PWD 之外的文件的所有符号链接(symbolic link)? UNIX

linux - Linux 下的curl命令报错

linux - PKG_CHECK_MODULES 中意外标记 `LIBCONFIGXX,' 附近出现语法错误

python - 在不同的 shell 窗口中启动 2 个并行进程

c - 制作我自己的 shell - C - chdir()