简体   繁体   中英

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

I am trying to run shell script for two datasets. 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. 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 . This parameter is applicable only for AMPIL dataset not for 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?

bash script.sh AMPIL exec_seq
bash script.sh MDM

Example: Make parameter 5 optional.

You can process an optional parameter ie $5 by fetching its contents by, for instance,

${5:-cockroach}

If parameter 5 is absent or empty , its value is assumed to be 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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