简体   繁体   English

在ksh Shell脚本中将临时变量纳入参数后解析

[英]Parsing arguments after taking into a temp variable in ksh shell scripting

I have a shell script as below: 我有一个如下的shell脚本:

$ cat check.sh  

    echo "$@"  
    for i in "$@"; do  
        echo "$i"  
    done   

If I run the script with command line args, it prints as below: 如果我使用命令行args运行脚本,则其输出如下:

$ ./check.sh arg1 arg2 "This is a message" arg4  

arg1 arg2 This is a message arg4  
arg1  
arg2
This is a message  
arg4  

All is well till now.. -- the number of arguments shown are 4 到目前为止一切都很好..-显示的参数数量为4

If I take $@ into an variable and do the same thing on it, it will behave as below: 如果我将$@放入变量并对其执行相同的操作,它将表现如下:

$ cat check.sh  

    VARGS="$@"  
    echo "$VARGS"  
    for i in $VARGS; do  
        echo "$i"  
    done  


$ ./check.sh arg1 arg2 "This is a message" arg4

arg1 arg2 This is a message arg4   
arg1  
arg2  
This  
is  
a  
message  
arg4  

Here the number of arguments are 7. 这里的参数数是7。
The reason that I have taken the arguments in a temp variable is to remove some unwanted args from it and pass it to another application/process. 我将参数用作temp变量的原因是要从其中删除一些不需要的args并将其传递给另一个应用程序/进程。

Can someone let me know how to get the same behavior in this scenario as if we are using "$@" 有人可以让我知道如何在这种情况下获得与使用“ $ @”相同的行为吗?

Thanks for your help in advance. 感谢您的帮助。

I'm using ksh93 for this, but it ought to work in ksh88 as well as it also has arrays: 我为此使用ksh93 ,但是它应该可以在ksh88中工作,并且还具有数组:

set -A VARGS "$@"

IFS=

for i in ${VARGS[@]}; do
    echo "$i"
done

Setting IFS= is necessary, otherwise the "This is a message" string would be chopped up on spaces. 设置IFS=是必需的,否则“ This is a message”字符串将在空格处被切掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM