2010-07-25 60 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

相关问题