原文地址
[1−LinuxBashShellScriptingTutorialWiki(cyberciti.biz)](https://bash.cyberciti.biz/guide/1 - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)](https://bash.cyberciti.biz/guide/1−LinuxBashShellScriptingTutorialWiki(cyberciti.biz)](https://bash.cyberciti.biz/guide/1)
案例介绍
**1∗∗ isthefirstcommand−lineargumentpassedtotheshellscript.Also,knowasPositionalparameters.Forexample,1** is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, 1∗∗isthefirstcommand−lineargumentpassedtotheshellscript.Also,knowasPositionalparameters.Forexample,0, 1,1, 1,3, $4 and so on. If you run ./script.sh filename1 dir1, then。
**1∗∗是传递给shell脚本的∗∗第一个命令行参数∗∗。另外,也被称为∗∗位置参数∗∗。例如,1**是传递给shell脚本的**第一个命令行参数**。另外,也被称为**位置参数**。例如,1∗∗是传递给shell脚本的∗∗第一个命令行参数∗∗。另外,也被称为∗∗位置参数∗∗。例如,0、1、3、4等等。
比如如果你运行./script.sh filename1 dir1
,那么:
- $0 is the name of the script itself (script.sh)
- $1 is the first argument (filename1)
- $2 is the second argument (dir1)
- $9 is the ninth argument
- 10isthetenthargumentandmustbeenclosedinbracketsafter{10} is the tenth argument and must be enclosed in brackets after 10isthetenthargumentandmustbeenclosedinbracketsafter9.
- ${11} is the eleventh argument.
$0
代表了脚本名称本身,比如这里的script.sh
就是$0的值。$1
代表了跟在脚本后面的第一个参数,$1 = filename1
$2
代表跟在脚本后面的第二个参数,$2 = dir1
。$9
对应的到$9
代表之后的第九个参数${10}
是第10个参数,必须在$9之后用括号括起来。${11}
是第11个参数。
What does $1
mean in Bash? $1
在Bash脚本的含义
Create a shell script named demo-args.sh
as follows:
最快的理解方式是实际在Linux上创建一个测试文件,这里我们命名为 demo-args.sh
,通过vim
新建一个文件,脚本的内容如下:
xander@xander:~$ vim demo-arges.sh
文件当中添加内容如下:
#!/bin/bash script="$0" first="$1" second="$2" tenth="${10}" echo "The script name : $script" echo "The first argument : $first" echo "The second argument : $second" echo "The tenth and eleventh argument : $tenth and ${11}"
xander@xander:~$ ll total 44 drwxr-x--- 4 xander xander 4096 Feb 3 13:12 ./ drwxr-xr-x 3 root root 4096 Jan 8 03:12 ../ .... -rw-rw-r-- 1 xander xander 225 Feb 3 13:12 demo-arges.sh ....
因为新建的文件不具备x
(可执行)权限,使用命令chmod +x demo-arges.sh
为新建的脚本文件新增可执行权限。
xander@xander:~$ chmod +x demo-arges.sh
之后运行脚本,我们传入字母作为参数:
xander@xander:~$ ./demo-arges.sh foo bar one two a b c d e f z f z h The script name : ./demo-arges.sh The first argument : foo The second argument : bar The tenth and eleventh argument : f and z
从结果来看可以验证之前介绍的方法。
1inbashfunctions1 in bash functions 1inbashfunctions1 在函数含义
Create a new script called func-args.sh;
创建一个名为func-args.sh
的新脚本。
xander@xander:~$ vim func-args.sh
添加内容如下:
#!/bin/bash die(){ local m="$1" # the first arg local e=$2 # the second arg echo "$m" exit $e } # if not enough args displayed, display an error and die [ $# -eq 0 ] && die "Usage: $0 filename" 1 # Rest of script goes here echo "We can start working the script..."
$# -eq 0
这个判断逻辑是如果传参为0的情况。
同样需要添加可执行权限:
xander@xander:~$ chmod +x func-args.sh
我们不传入任何参数,直接执行脚本,则会进入到如果显示的args不够多,则显示错误并结束
这一步。注意这里的$0
并不是脚本的名称。
xander@xander:~$ ./func-args.sh Usage: ./func-args.sh filename
我们在脚本中传入参数,结果正确执行:
xander@xander:~$ ./func-args.sh /etc/hosts We can start working the script...
其他例子
fingerprints() { local file="$1" while read l; do [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l done < $file }