【问题标题】:Storing ksh input array to variable and passing to another script将 ksh 输入数组存储到变量并传递给另一个脚本
【发布时间】:2010-07-26 06:00:17
【问题描述】:

我必须修改现有的 ksh 脚本,该脚本使用 'shift' 查看命令行参数,因此清空 $@,但现在想在之后将原始参数传递给第二个脚本。

在主线情况下,我可以通过将 $@ 对应到一个变量并将其传递给第二个脚本来做到这一点,但我无法让它为引用的命令行参数工作。

如果我有一个名为“打印机”的脚本,如下所示:

#!/bin/ksh

INPUT=$@  
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 的分配中以及在将其传递给打印机时),但无法弄清楚。有人可以帮忙吗?

【问题讨论】:

    标签: ksh command-line-arguments


    【解决方案1】:

    好的,经过大量的反复试验,我想我已经找到了解决方案。

    像这样分配 $INPUT:

    set -A INPUT "$@"
    

    然后像这样传递它:

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

    产生我想要的输出。

    因此,整个第一个脚本是:

    #!/bin/ksh
    
    set -A INPUT "$@"  
    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
    

    如果有人想解释我尝试过的其他事情的问题,请做,因为我仍然感兴趣!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多