前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >$加数字在Shell中的含义

$加数字在Shell中的含义

作者头像
阿东
发布2023-02-27 09:35:24
1.6K0
发布2023-02-27 09:35:24
举报
文章被收录于专栏:公众号:懒时小窝

原文地址

$1 - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)

案例介绍

$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, $0, $1, $3, $4 and so on. If you run ./script.sh filename1 dir1, then。

$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
  • ${10} is the tenth argument and must be enclosed in brackets after $9.
  • ${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新建一个文件,脚本的内容如下:

代码语言:shell
复制
xander@xander:~$ vim demo-arges.sh

文件当中添加内容如下:

代码语言:shell
复制
#!/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}"
代码语言:shell
复制
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为新建的脚本文件新增可执行权限。

代码语言:shell
复制
xander@xander:~$ chmod +x demo-arges.sh 

之后运行脚本,我们传入字母作为参数:

代码语言:shell
复制
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

从结果来看可以验证之前介绍的方法。

$1 in bash functions $1 在函数含义

Create a new script called func-args.sh;

创建一个名为func-args.sh的新脚本。

代码语言:shell
复制
xander@xander:~$ vim func-args.sh

添加内容如下:

代码语言:shell
复制
#!/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的情况。

同样需要添加可执行权限:

代码语言:shell
复制
xander@xander:~$ chmod +x func-args.sh

我们不传入任何参数,直接执行脚本,则会进入到如果显示的args不够多,则显示错误并结束这一步。注意这里的$0并不是脚本的名称。

代码语言:shell
复制
xander@xander:~$ ./func-args.sh 
Usage: ./func-args.sh filename

我们在脚本中传入参数,结果正确执行:

代码语言:shell
复制
xander@xander:~$ ./func-args.sh /etc/hosts
We can start working the script...

其他例子

代码语言:shell
复制
fingerprints() {
         local file="$1"
         while read l; do
                 [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l
         done < $file
}

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原文地址
  • 案例介绍
  • What does $1 mean in Bash? $1 在Bash脚本的含义
  • $1 in bash functions $1 在函数含义
  • 其他例子
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档