【发布时间】:2019-01-28 18:05:24
【问题描述】:
我有一个脚本“/tmp/SampleScript.sh”,内容如下:
echo "First arg: $1"
echo "Second arg: $2"
如果我按以下方式运行此脚本:
[oracle@xxxxx tmp]$ ./SampleScript.sh FirstParamPassed SecondParamPassed
Output Is:
First arg: FirstParamPassed
Second arg: SecondParamPassed
但如果我将其运行为:
[oracle@xxxxx tmp]$ ./SampleScript.sh SecondParamPassed FirstParamPassed
Output Is:
First arg: SecondParamPassed
Second arg: FirstParamPassed
我想要这样的输出:
echo "First arg: $FirstParamPassed"
echo "Second arg: $FirstParamPassed"
[oracle@xxxxx tmp]$ ./SampleScript.sh SecondParamPassed=2 FirstParamPassed=1
First arg: 1
Second arg: 2
如何在 REHL shell 脚本中使用这种类型的命名变量。 我已经完成了这个答案Is there a way to avoid positional arguments in bash?,但无法理解如何在我的情况下实施。
【问题讨论】:
-
这是对类似 SO 问题的另一个更简单的答案,可能比您提到的 SO 问题更容易适应:stackoverflow.com/questions/5499472/…