2010-07-25 64 views
2

我必須修改現有的ksh腳本,它使用'shift'查看命令行參數,並清空$ @,但現在要將原始參數傳遞給之後的第二個劇本。將ksh輸入數組存儲到變量並傳遞給另一個腳本

在主線情況下,我可以通過將$ @複製到一個變量並將其傳遞給第二個腳本來完成此操作,但是我無法讓它適用於引用的命令行參數。

如果我有一個叫做 '打印機' 像下面的腳本:

#!/bin/ksh 

[email protected] 
echo "Printing args" 
until [[ $# -eq 0 ]];do 
    echo $1 
    shift 
done 

./printer2 $INPUT 

和PRINTER2象下面這樣:

#!/bin/ksh 

echo "Printing second args" 
until [[ $# -eq 0 ]];do 
    echo $1 
    shift 
done 

我想的

./printer first second "third forth" 

輸出爲:

Printing args 
first 
second 
third forth 
Printing second args 
first 
second 
third forth 

我試過各種各樣的變量組合(在$ INPUT的賦值和將它傳遞給printer2時),但無法弄清楚。誰能幫忙?

回答

4

好吧我想我已經找到了解決方案,經過了很多反覆試驗。

分配$ INPUT這樣的:

set -A INPUT "[email protected]" 

,然後傳遞給它這樣的:

./printer2 "${INPUT[@]}" 

產生輸出我之後。

整個第一腳本因此:

#!/bin/ksh 

set -A INPUT "[email protected]" 
echo "Printing args" 
until [[ $# -eq 0 ]];do 
    echo $1 
    shift 
done 

./printer2 "${INPUT[@]}" 

./printer first second "third fourth" 

輸出:

Printing args 
first 
second 
third fourth 
Printing second args 
first 
second 
third fourth 

如果有人想用其他的事情我試圖解釋這一問題,請做,因爲我仍然感興趣!

+0

與我的興趣有關。感謝您的幫助! – Katerberg 2011-01-21 21:20:24

+0

請參閱http://unix.stackexchange.com/questions/41357/what-is-the-most-correct-way-to-pass-an-array-to-a-function進行推理。 – 2012-08-31 13:57:10

相關問題