2017-07-20 98 views
0

作業就是修改這個腳本採取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)' 

我沒有任何額外的引號,爲什麼會發生這種情況?

回答

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