bash 将参数传递给 Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19602698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:25:04  来源:igfitidea点击:

Passing Arguments to Shell Script

bashshellscripting-language

提问by Nik

Very simple question may be but not able to get this working....essentially i have a XML file which is internally allows me to pass few arguments to a external program in my case i chose shell script due to unavoidable reasons.

非常简单的问题可能是但无法使其正常工作....本质上我有一个 XML 文件,它在内部允许我将一些参数传递给外部程序,在我的情况下,由于不可避免的原因我选择了 shell 脚本。

Now it is working for the most part but i hit issue, essentially i am passing 12 arguments from XML to shell script -- passing and consuming arguments in shell worked fine till 9th argument when i hit 10th 11th 12th argument they are concatenating ARG1 with 0 for 10th argument, ARG1 with 1 for 11th argument and Arg1 with 2 for 12th argument.

现在它大部分都在工作,但我遇到了问题,基本上我将 12 个参数从 XML 传递到 shell 脚本——在 shell 中传递和使用参数工作正常,直到第 9 个参数,当我点击第 10 个第 11 个第 12 个参数时,它们将 ARG1 与 0 连接对于第 10 个参数,ARG1 用 1 表示第 11 个参数,Arg1 用 2 表示第 12 个参数。

ARG1=    # Name    
ARG2=    # Text     
ARG3=    # Model    
ARG4=    # Network Address    
ARG5=    # Type    
ARG6=    # Landscape    
ARG7=    # Cause    
ARG8=    # Troubleshooter    
ARG9=    # Originiating Event    
ARGX=   # Status    
ARGY="" # Customer    
ARGZ="" # Category

so essentially value of ARG10 is ARG1 and 0 for ex lets say you pass ARG1 as "text" and ARG10 as "New" from XML file to shell script but when ARG10 is echoed out in shell script it echoes - "text0" instead of new.
I am very sure this is a silly thing i am doing need some help understanding where i am wrong in coding it.

所以本质上 ARG10 的值是 ARG1 和 0 对于 ex 假设你将 ARG1 作为“文本”和 ARG10 作为“新”从 XML 文件传递​​到 shell 脚本但是当 ARG10 在 shell 脚本中回显时它回显 - “text0”而不是 new .
我很确定这是一件愚蠢的事情,我正在做的事情需要一些帮助来理解我编码错误的地方。

回答by Mark Reed

You need to use curly braces when you have more than one digit.

当您有一个以上的数字时,您需要使用花括号。

ARGX=

etc. (Note that you don't need quotation marks around a parameter expansion that is the entire right side of an assignment statement.) Without the braces, $10is interpreted as $1followed by a literal 0.

等(请注意,作为赋值语句整个右侧的参数扩展不需要引号。)如果没有大括号,$10则解释为$1后跟文字0.

You can also use braces for the single-digit arguments (as well as any other shell parameter names), just for consistency. But with double-digit argument numbers, they're a necessity.

您还可以对一位数的参数(以及任何其他 shell 参数名称)使用大括号,只是为了保持一致。但是对于两位数的参数编号,它们是必需的。

Once you get to the point that you have that many parameters to something, though, it might be worth looking into some sort of explicit naming scheme. Perhaps you could use option syntax, with something like func -name "$name" -text "$text", etc. Then you could do something like this (assuming bash >= 4):

但是,一旦你知道你有这么多参数来做某事,那么研究某种显式命名方案可能是值得的。也许你可以使用选项语法,比如func -name "$name" -text "$text",等等。然后你可以做这样的事情(假设 bash >= 4):

declare -A args=()
while (( $# )); do
  case "" in  -*) key="${1#-}";;
                 *) args[$key]="";;
  esac
  shift
done

# now the name is ${args[name]}, text is ${args[text]}, etc.

回答by Kevin

Once you have recorded an argument, you can shiftit off the list of arguments:

一旦你记录了一个论点,你就可以shift把它从论点列表中删除:

ARG1=; shift #Name
ARG2=; shift # Text
ARG3=; shift # Model
...