Foreach循环将无法运行

问题描述:

作业就是修改这个脚本采取EXEC作为参数,但首先我希望能够运行脚本来揣摩如何修改它Foreach循环将无法运行

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)' 

我没有任何额外的引号,为什么会发生这种情况?

在许多炮弹(我认为是tcsh伯恩的兼容机之间计算),你必须地方表达的左侧,在=,和右侧的所有直接紧邻另一个。

# 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 
+0

tcsh肯定不是** bourne shell兼容...... – user1934428