English Snippet 5 - How to Pass Arguments to a Bash-Script
Posted gerryyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了English Snippet 5 - How to Pass Arguments to a Bash-Script相关的知识,希望对你有一定的参考价值。
写在前面
作为Linux开发人员,经常需要和bash打交道,把执行频率高的命令写成bash脚本可以极大地提高工作效率。本文介绍如何将参数传给bash脚本,包括固定参数,非固定参数,以及参数选项的用法。(PS: 微信公总号文章竟然不支持Markdown编辑)
How to Pass Arguments to a Bash-Script
You can write a bash script such that it receives arguments specified when the script is called from the command line. This method is used when a script has to perform a slightly different function depending on the values of input parameters (the arguments).
通常,我们通过不同的参数来执行不同的脚本功能。
For example, you may have a script called "stats.sh" that performs a particular operation on a file, such as counting its words. If you want to be able to use that script on many files, it is best to pass the file name as an argument, so that you can use the same script for all the files to be processed.
For instance, if the name of the file to be processed is "songlist", you would enter the following command line:
``` bash
sh stats.sh songlist
```
Arguments are accessed inside a script using the variables $1, $2, $3, etc., where $1 refers to the first argument, $2 to the second argument, and so on. This is illustrated in the following example:
``` bash
FILE1=$1
wc $FILE1
```
For readability, assign a variable with a descriptive name to the value of the first argument ($1), and then call the word count utility (wc) on this variable ($FILE1).
If you have a variable number of arguments, you can use the "$@" variable, which is an array of all the input parameters. This means you can use a for-loop to iteratively process each one, as illustrated in the following example:
``` bash
for FILE1 in "$@"
do
wc $FILE1
done
```
Here is an example of how to call this script with arguments from the command line:
``` bash
sh stats.sh songlist1 songlist2 songlist3
```
If an argument has spaces, you need to enclose it with single quotes. For example:
``` bash
sh stats.sh 'songlist 1' 'songlist 2' 'songlist 3'
```
如果是固定个数的参数,可以通过$1,$2,等来获取参数,而如果参数个数不固定,可以通过`$@`来遍历获取每个参数。
Frequently a script is written such that the user can pass in arguments in any order using flags. With the flags method, you can also make some of the arguments optional.
Let say you have a script that retrieves information from a database based on specified parameters, such as "username", "date", and "product", and generates a report in a specified "format". Now you want to write your script so that you can pass in these parameters when the script is called. It might look like this:
``` bash
makereport -u jsmith -p notebooks -d 10-20-2011 -f pdf
```
Bash enables this functionality with the "getopts" function. For the above example, you could use getopts as follows:
``` bash
while getopts u:d:p:f: option
do
case "${option}"
in
u) USER=${OPTARG};;
d) DATE=${OPTARG};;
p) PRODUCT=${OPTARG};;
f) FORMAT=$OPTARG;;
esac
done
```
This is a while-loop that uses the "getopts" function and a so-called "optstring", in this case "u:d:p:f:", to iterate through the arguments. The while-loop walks through the optstring, which contains the flags that can be used to pass arguments, and assigns the argument value provided for that flag to the variable "option". The case-statement then assigns the value of the variable "option" to a global variable that can used after all the arguments have been read.
The colons in the optstring mean that values are required for the corresponding flags. In the above example all flags are followed by a colon: "u:d:p:f:".
This means, all flags need a value. If, for example, the "d" and "f" flags were not expected to have a value, the optstring would be "u:dp:f".
如果flag后面带有冒号,那么代表此flag需要带有value。相反,如果没有冒号,则此flag可以不需要value。也就是,如果指定了某个flag需要带有value,但是没有传value,就会报类似`No arg for -u option`这样的错误。
A colon at the beginning of the optstring, for example ":u:d:p:f:", has a completely different meaning. It allows you to handle flags that are not represented in the optstring. In that case the value of the "option" variable is set to "?" and the value of "OPTARG" is set to the unexpected flag. The allows you to display a suitable error message informing the user of the mistake.
如果flag前面带有冒号,则代表允许处理一些不在optstring(例如,u:d:p:f:)里指定的flag。这个功能主要用于提示用户输入了错误的选项。
Arguments that are not preceded by a flag are ignored by getopts. If flags specified in the optstring are not provided when the script is called, nothing happens, unless you specially handle this case in your code.
flag选项可以指定,或者不指定,默认不会报错,除非你显式的检查并报错。
Any arguments not handled by getops can still be captured with the regular $1, $2, etc. variables.
这是通用的方法。
更多关于bash的使用介绍,也可以参考我github上的总结。
https://github.com/gerryyang/wcdj/tree/master/tools/bash
以上是关于English Snippet 5 - How to Pass Arguments to a Bash-Script的主要内容,如果未能解决你的问题,请参考以下文章
How do I improve my English speaking skills in a very short time?
The road to learning English-Grammar
English trip V1 - B 4.How Do You Make a Salad? 请给我一间房? Teacher:Julia Key:imperatives 祈使句
How do I force android WiFi Direct group owner to use 2.4 GHz instead of 5 GHz