简体   繁体   English

是否可以在 shell 脚本中传递可选参数?

[英]Is it possible to pass an optional argument in a shell script?

I am trying to run shell script for two datasets.我正在尝试为两个数据集运行 shell 脚本。 I am passing dataset name as parameter.我将数据集名称作为参数传递。

But one dataset has the parameter exec_seq and the other doesn't, but the script is the same for both the datasets.但是一个数据集有参数exec_seq而另一个没有,但是两个数据集的脚本是相同的。 Is there any way to pass an optional argument?有什么方法可以传递可选参数吗?

Example:例子:

We have two datasets:我们有两个数据集:

AMPIL
MDM

We are running the script using the command below:我们正在使用以下命令运行脚本:

bash script.sh AMPIL
bash script.sh MDM

But now I have to pass one more parameter, exec_seq .但现在我必须再传递一个参数exec_seq This parameter is applicable only for AMPIL dataset not for MDM.此参数仅适用于 AMPIL 数据集,不适用于 MDM。 How can we check if no parameter is passed, meaning it should run for MDM, and if any parameter is passed then it should run for AMPIL?我们如何检查是否没有传递参数,这意味着它应该为 MDM 运行,如果传递了任何参数,那么它应该为 AMPIL 运行?

bash script.sh AMPIL exec_seq
bash script.sh MDM

Example: Make parameter 5 optional.示例:将参数 5 设为可选。

You can process an optional parameter ie $5 by fetching its contents by, for instance,您可以通过获取其内容来处理可选参数,即$5 ,例如,

${5:-cockroach}

If parameter 5 is absent or empty , its value is assumed to be cockroach .如果参数 5不存在或为空,则假定其值为cockroach

If the user wants to explicitly specify parameter 4 and 6, and leave parameter 5 as the default, he passes the empty string as 5th parameter.如果用户想明确指定参数 4 和 6,并将参数 5 保留为默认值,则将空字符串作为第 5 个参数传递。

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

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