gpt4 book ai didi

linux - Foreach 循环不会运行

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:08 25 4
gpt4 key购买 nike

作业是修改此脚本以将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com