$加数字在Shell中的含义

简介: $加数字在Shell中的含义

原文地址

[1−LinuxBashShellScriptingTutorialWiki(cyberciti.biz)](https://bash.cyberciti.biz/guide/1 - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)](https://bash.cyberciti.biz/guide/1LinuxBashShellScriptingTutorialWiki(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, 1isthefirstcommandlineargumentpassedtotheshellscript.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}"

image.png


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的情况。

image.png

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


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
}



相关文章
|
9月前
|
Shell 分布式数据库
shell脚本中if判断‘-a‘ - ‘-z‘含义
shell脚本中if判断‘-a‘ - ‘-z‘含义
|
2月前
|
Shell 数据安全/隐私保护
Shell判断输入是否有效IP和字母
Shell判断输入是否有效IP和字母
Shell判断输入是否有效IP和字母
|
11月前
|
Shell PHP
浅谈无数字字母shell
浅谈无数字字母shell
94 0
|
11月前
|
Shell
Shell 预定义变量 ($0、$$、$?、$#、$* 含义)
Shell 预定义变量 ($0、$$、$?、$#、$* 含义)
353 0
|
弹性计算 Shell Linux
3天玩转shell--9.shell中的数字及时间运算
本文将通过shell代码示例,简单通俗的讲解shell。通过执行代码和运行结果反向掌握shell编程方法。准备一台低配的阿里云ECS Linux环境,跟着教程走起,本文比较适合shell小白。
|
Shell
SHELL中变量字串中包含$时怎么办?
SHELL中变量字串中包含$时怎么办?
103 0
|
Shell
SHELL判断文件是否包含某个字串
SHELL判断文件是否包含某个字串
162 0
|
Shell
shell脚本中常见的一些特殊符号和作用详解
在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我们编写的代码更加简洁和高效,这里给大家汇总下: 1、{} 大括号:用法一:通配符扩展eg: ls my_{finger,toe}s这条命令相当于如下命令的组合:ls my_fingers my_toeseg: m...
1893 0

热门文章

最新文章