我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$nn 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推……

实例

以下实例我们向脚本传递三个参数,并分别输出,其中 $0 为执行的文件名:

#!/bin/bash
# author:hong
echo "current file name is $0"
echo "first param name is $1"
echo "first param name is $2"
echo "first param name is $3"

为脚本设置可执行权限,并执行脚本,输出结果如下所示:

[root@localhost shelltest]# chmod +x test.sh
[root@localhost shelltest]# ./test.sh p1 p2 p3
./test.sh: line : uthor:hong: command not found
current file name is ./test.sh
first param name is p1
first param name is p2
first param name is p3

处理参数的特殊字符

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

#!/bin/bash
# author:hong
# echo "current file name is $0"
# echo "first param name is $1"
# echo "first param name is $2"
# echo "first param name is $3"
echo "all the param num is $#"
echo "all the param print by a string is $*"
echo "running process id num is $$"
echo "last id num is $!"
echo "all the param print by quotation marks is $@"
echo "show the shell opition: $-"
echo "show the last commond exit state: $?"

执行脚本结果

[root@localhost shelltest]# ./test.sh p1 p2 p3
all the param num is
all the param print by a string is p1 p2 p3
running process id num is
last id num is
all the param print by quotation marks is p1 p2 p3
show the shell opition: hB
show the last commond exit state:

$* 与 $@ 区别

  • 相同点:都是引用所有参数。
  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)。

示例

#!/bin/bash
# author:hong echo "-- \$* 演示 ---"
for i in "$*"; do
echo $i
done echo "-- \$@ 演示 ---"
for i in "$@"; do
echo $i
done

结果

[root@localhost shelltest]# chmod +x test1.sh
[root@localhost shelltest]# ./test1.sh p11 p22 p33
-- $* 演示 ---
p11 p22 p33
-- $@ 演示 ---
p11
p22
p33

Shell脚本编程(三):shell参数传递的更多相关文章

  1. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  2. linux中的shell脚本编程---初识shell

    Shell是用户与Linux或Unix内核通信的工具,shell编程指的并不是编写这个工具,而是指利用现有的shell工具进行编程,写出来的程序是轻量级的脚本,我们叫做shell脚本. Shell的语 ...

  3. Shell脚本编程具体解释

    第12章 Shell脚本编程   l  Shell命令行的执行 l  编写.改动权限和运行Shell程序的步骤 l  在Shell程序中使用參数和变量 l  表达式比較.循环结构语句和条件结构语句 l ...

  4. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  5. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  6. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  7. shell脚本编程及bash特性

    bash特性及bash脚本编程初步 终端,附着在终端的接口程序; GUI: KDE,GNome,Xfce CLI: /etc/shells bash的特性: 命令行展开: ~,{} 命令别名: ali ...

  8. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  9. 【Linux】Shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  10. Shell脚本编程总结及速查手册

    Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...

随机推荐

  1. metasploit支持利用的CVE

    因为需要添加许多漏洞的流量检测,所以需要模拟很多漏洞的利用过程,简单来说,就是抓漏洞利用过程的流量. 一个脚本对metasploit中的module中包含的cve字段进行提取,而后去重,得出metas ...

  2. PostgreSQL 在Ubuntu下如何修改postgres默认密码

    Step1: 切换用户为postgres sudo su postgres Step2: 用postgres连接postgreSQL psql -U postgres Step3: 修改postgre ...

  3. String,StringBuffer与StringBuilder的区别|线程安全与线程不安全

    https://www.cnblogs.com/xingzc/p/6277581.html

  4. gcc 各种参数

    1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...

  5. [OC] Delegate的使用

    建立两个页面 A 和 B,我们假设他们的文件名为ControllerA,ControllerB 由A页面,点击跳转到B页面.在B页面中,进行一些操作,并得到一个值,并将这个值传回给A页面,并在A页面上 ...

  6. IE和DOM事件流

    * ie采用冒泡型事件 Netscape使用捕获型事件 dom使用先冒泡后捕获事件 冒泡型事件模型: button->div->body (IE事件流) 捕获型事件模型: body-> ...

  7. Codeforces.1088D.Ehab and another another xor problem(交互 思路)

    题目链接 边颓边写了半上午A掉啦233(本来就是被无数人过掉的好吗→_→) 首先可以\(Query\)一次得到\(a,b\)的大小关系(\(c=d=0\)). 然后发现我们是可以逐位比较出\(a,b\ ...

  8. [JOI2017/2018]美術展

    [JOI2017/2018]美術展 题目大意: 有\(n(n\le5\times10^5)\)个物品,每个物品有两个属性:尺寸\(A_i\)和收益\(B_i\).从中选取一个子集,总收益为\(\sum ...

  9. 使用纯CSS制作展开合并立方体特效

    显示效果 源码 <html> <head> <meta http-equiv="Content-Type" content="text/ht ...

  10. yii2过滤器(filter)

    一.VerbFilter VerbFilter检查请求动作的HTTP请求方式是否允许执行, 如果不允许,会抛出HTTP 异常 use yii\filters\VerbFilter; public fu ...