一、特殊位置参数变量

  1、特殊位置参数变量

    在shell中比如:$0、$1、$#,等被称为特殊位置参数变量,当命令行、函数、脚本执行等处传递参数时,就需要使用位置参数变量

    参数说明如下:

  2、示例$1$2...$9${10}${11}...

    1)测试$n(n为1..15)

[root@web1 scripts]# vim test1.sh       #<---创建test1.sh脚本
#!/bin/bash
echo $1                     #<---打印脚本传递的第一个参数的值
[root@web1 scripts]# chmod +x test1.sh   #<---授权
[root@web1 scripts]# ./test1.sh       #<---不加参数输出空白 [root@web1 scripts]# ./test1.sh zxg    #<---加一个参数
zxg
[root@web1 scripts]# ./test1.sh zxg shell  #<---加两个参数,只输出第一个参数
zxg
[root@web1 scripts]# ./test1.sh "zxg shell"  #<---添加双括号,两个都输出因为加双括号会作为一个字符串参数
zxg shell
[root@web1 scripts]#

    2)同时加入$1和$2,并进行测试

[root@web1 scripts]# cat test2.sh               #<---创建test2.sh脚本
#!/bin/bash
echo $ $ [root@web1 scripts]# chmod +x test2.sh          #<---加权限
[root@web1 scripts]# ./test2.sh zxg shell        #<---运行
zxg shell                            
[root@web1 scripts]# ./test2.sh "zxg shell" 2019    #<---对又空格的字符串需要双引号才能正常使用
zxg shell
[root@web1 scripts]#

    3)设置15个位置参数($1~$15),用于接收命令行传递的15个参数

[root@web1 scripts]# echo \${..}                       #<---利用大括号输出15个位置参数,10大技巧
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $
[root@web1 scripts]# echo \${..} >test3.sh
[root@web1 scripts]# cat test3.sh
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $
[root@web1 scripts]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@web1 scripts]# chmod +x test3.sh
[root@web1 scripts]# cat test3.sh
echo $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
[root@web1 scripts]# ./test3.sh {a..z}            #<---运行发现就到9 超过9必须用大括号
a b c d e f g h i a0 a1 a2 a3 a4 a5
[root@web1 scripts]#

[root@web1 scripts]# cat test3.sh
 echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}  #这样就可以正常了
 [root@web1 scripts]# ./test3.sh {a..z}
 a b c d e f g h i j k l m n o
 [root@web1 scripts]#


  3、$0特殊变量的作用示例

    取出执行脚本的名称(包括路径)

    1)、获得脚本名称及路径

[root@web1 scripts]# cat test4.sh
#!/bin/bash
echo $ [root@web1 scripts]# chmod +x test4.sh
[root@web1 scripts]# ./test4.sh
./test4.sh
[root@web1 scripts]#

[root@web1 scripts]# sh /scripts/test4.sh           #<---带路径的话,就会输出路径加名字
 /scripts/test4.sh
 [root@web1 scripts]#

 

    2)单独获取名称或路径的方法

[root@web1 scripts]# dirname /scripts/test4.sh   #<---dirname命令作用时获取脚本路径
/scripts
[root@web1 scripts]# basename /scripts/test4.sh #<---basename命令则时获取脚本名称
test4.sh
[root@web1 scripts]#

    3)利用$0 和(dirname、basename)分别读取脚本名称和脚本路径

[root@web1 scripts]# cat test5.sh
#!/bin/bash
dirname $
basename $ [root@web1 scripts]# chmod +x test5.sh
[root@web1 scripts]# sh /scripts/test5.sh
/scripts
test5.sh
[root@web1 scripts]#

  4、$#特殊变量获取脚本传参个数的示例

    1)通过$#获取脚本传参的个数

[root@web1 scripts]# cat test6.sh
#!/bin/bash
echo $ $ $ $ $ $ $ $ $
echo $#
[root@web1 scripts]# chmod +x test6.sh
[root@web1 scripts]# ./test6.sh {a..z} #<---传入26个参数
a b c d e f g h i               #<---只打印了9个
26                        #<---代表26个参数
[root@web1 scripts]#

    2)根据用户在命令行的传参个数判断用户的输入,不合要求的给予提示并退出

      这是针对$0、$1、$#等多位置参数的综合示例

[root@web1 scripts]# cat test7.sh
#!/bin/bash
[ $# -ne ] && {                #<---如果执行脚本传参的个数不等于2
echo "muse two args"          #<---则给用户提示正确的用法
exit                  #<---由于不满足要求,因此退出脚本,返回值为1
}
echo abc                      #<---满足了参数个数的传参要求,就执行判断后的程序脚本,打印abc
[root@web1 scripts]# chmod +x test7.sh
[root@web1 scripts]# ./test7.sh
muse two args
[root@web1 scripts]# ./test7.sh
abc
[root@web1 scripts]#

      if语句的写法

[root@web1 scripts]# cat test8.sh
#!/bin/bash
if [ $# -ne ]
then
echo "USAGE:/bin/bash $0 arg1 arg2"
exit
fi
echo $ $
[root@web1 scripts]# chmod +x test8.sh
[root@web1 scripts]# ./test8.sh
USAGE:/bin/bash ./test8.sh arg1 arg2
[root@web1 scripts]# ./test8.sh [root@web1 scripts]#

  5、$*和$@特殊变量功能及区别示例

    1)利用set设置位置参数(同命令行脚本的传参)

[root@web1 scripts]# set -- "i am" zxg shell
[root@web1 scripts]# echo $# [root@web1 scripts]# echo $
i am
[root@web1 scripts]# echo $
zxg
[root@web1 scripts]# echo $
shell
[root@web1 scripts]#

    测试$*和$@,注意,此时带双引号

[root@web1 scripts]# echo "$*"                
i am zxg shell
[root@web1 scripts]# echo "$@"
i am zxg shell
[root@web1 scripts]# for i in "$*";do echo $i;done    #<---在又双引号的情况下,"$*",引号里面当作一个参数输出
i am zxg shell
[root@web1 scripts]# for i in "$@";do echo $i;done     #<---在又双引号的情况下,"$#",每个参数独立输出,
i am                                #<---算一个参数
zxg
shell
[root@web1 scripts]# for i ;do echo $i;done        #<---去掉in 变量列表,相当于又引号的in "$@"
i am
zxg
shell
[root@web1 scripts]# for i in $*;do echo $i;done     #<---$*不加双引号,因此会输出所有参数,然后第一个参数"i am"也拆开输出了
i
am
zxg
shell
[root@web1 scripts]# shift                  #<---用shift将位置参数移位(左移)
[root@web1 scripts]# echo $# [root@web1 scripts]# echo $1                 #<---这里就打印原来$2的值
zxg
[root@web1 scripts]# echo $2                 #<---这里就打印原来$3的值
shell
[root@web1 scripts]#

二、shell进程中的特殊状态变量

  1、$?示例

    1)执行命令

[root@web1 scripts]# pwd
/scripts
[root@web1 scripts]# echo $?          #<---返回0表示上一个命令成功 [root@web1 scripts]# ls /root
anaconda-ks.cfg Desktop google-chrome-stable_current_x86_64.rpm original-ks.cfg
bash-test Downloads initial-setup-ks.cfg
[root@web1 scripts]# lss /root
bash: lss: command not found...
Similar command is: 'ls'
[root@web1 scripts]# echo $?          #<---非零表示上一个命令失败 [root@web1 scripts]#

    可以利用$?判断安装、打包、备份等执行是否正确成功

      tar zcf /zxg/zxg.tar.gz /zxg/zxg

      echo $?

    2)通过脚本控制命令及脚本执行后的返回值

[root@web1 scripts]# cat test9.sh
#!/bin/bash
[ $# -ne ] && {                    #<---参数格式不等于2
echo "must be two args"            #<---输出提示
exit 119                          #<---终止程序运行并以制定的119状态值退出程序赋值给$?
}
echo zxg [root@web1 scripts]# chmod +x test9.sh
[root@web1 scripts]# ./test9.sh
zxg
[root@web1 scripts]# echo $? [root@web1 scripts]# ./test9.sh
must be two args
[root@web1 scripts]# echo $? [root@web1 scripts]#

    3)小结

      判断命令、脚本或函数程序是否执行成功

      若在脚本中调用执行exit 数字 把这个数字以函数返回值的形式传给$?变量

      如果是在函数里,则通过return数字把这个数字以函数返回值的形式传给$?

  2、$$特殊变量功能及实践

    1)获取脚本执行的进程号

[root@web1 scripts]# cat test10.sh
#!/bin/bash
echo $$ >/tmpa.pid                                #<---获取$$的值,并重定向
sleep                                      #<---休息300s,模拟守护进程不退出
[root@web1 scripts]# chmod +x test10.sh
[root@web1 scripts]# ps -ef|grep test10|grep -v grep       
[root@web1 scripts]# ./test10.sh &                       #<---后台运行
[]
[root@web1 scripts]# ps -ef|grep test10|grep -v grep            #<---查找进程号
root : pts/ :: /bin/bash ./test10.sh
[root@web1 scripts]# cat /tmpa.pid                       #<---对应$$的值 [root@web1 scripts]#

    2)实现系统中多次执行某个脚本后的进程只有一个-企业级应用,用于执行启动或定时任务

[root@web1 scripts]# cat test11.sh
#!/bin/bash
pidpath=/tmp/a.pid                              #<---定义pid文件
if [ -f "$pidpath" ]                             #<---如果pid文件存在,值=则执行then后面的命令
then
kill `cat $pidpath` >/dev/null >&1          #<---杀掉与前一个金朝赌赢的进程
rm -f $pidpath                       #<---删除pid文件
fi
echo $$ >$pidpath                               #<---将当前$$值记录到pid文件里
sleep
[root@web1 scripts]# chmod +x test11.sh
[root@web1 scripts]# ./test11.sh
^C
[root@web1 scripts]# ./test11.sh &
[]
[root@web1 scripts]# ps -ef |grep test11.sh|grep -v grep
root : pts/ :: /bin/bash ./test11.sh
[root@web1 scripts]# ./test11.sh &
[]
[root@web1 scripts]# ps -ef |grep test11.sh|grep -v grep
root : pts/ :: /bin/bash ./test11.sh
[]- Terminated ./test11.sh
[root@web1 scripts]# ./test11.sh &             #<---无论执行多少次脚本每次都会将上一次运行的杀掉,都只有一个进程
[]
[root@web1 scripts]# ps -ef |grep test11.sh|grep -v grep
root : pts/ :: /bin/bash ./test11.sh
[]- Terminated ./test11.sh
[root@web1 scripts]#

  3、$_特殊变量功能说明及实践

    获得上一条命令的最后一个参数,用的不多

[root@web1 scripts]# systemctl restart httpd zxg
Failed to restart zxg.service: Unit not found.
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@web1 scripts]# echo $_
zxg
[root@web1 scripts]#

  4、$!特殊变量功能说明及实践

    $!的功能类似$$,只不过作用是获取上次执行脚本的pid,了解就可以了

[root@web1 scripts]# ps -ef |grep test11.sh|grep -v grep
[root@web1 scripts]# echo $! [root@web1 scripts]#

转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11128349.html

shell 学习笔记3-shell变量扩展的更多相关文章

  1. Shell学习笔记之shell脚本和python脚本实现批量ping IP测试

    0x00 将IP列表放到txt文件内 先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.1 ...

  2. shell学习笔记1: shell 中的变量与常见符号使用方法

    变量 声明即用 a=2 b="123" 调用 ${varName}或者 $varName echo $b echo ${a} 常见变量 $?:判断上一个语句是否成功 $0:执行脚本 ...

  3. 鸟书shell 学习笔记(一) shell专注于概念和命令

    变量   variableName=value 等号左右不能有空格 变量内容有空格须要用"或者'括起来,可是 v="hello $name" $保持原有功能,单引號则不行 ...

  4. shell学习笔记2: shell中的四则运算符

    shell中的四则运算符 n1,n2 :常量数字 char:运算符号 加,减,乘,除,取余(+,-,*,/,%) $a,$b:变量a,变量b 方法1 数字与符号之间需要有空格 不支持小数 expr n ...

  5. 鸟书shell 学习笔记(二) shell中正則表達式相关

    通配符与正則表達式的差别 通配符是bash原生支持的语法,正則表達式是处理字符串的一种表示方式, 正則表達式须要支持的工具支持才干够 语系设置 : export LANG=C grep alias 设 ...

  6. shell学习笔记汇总

    1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...

  7. SHELL学习笔记三

    SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...

  8. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  9. [转帖][Bash Shell] Shell学习笔记

    [Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html  阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...

  10. shell 学习笔记2-shell-test

    一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...

随机推荐

  1. IDEA一些自动补全方式

    第一种:系统自带:可以CTRL + j 可以查看 psvm 也就是public static void main的首字母. 依次还有在方法体内键入for会有一个fori的提示,选中然后tab键,就会自 ...

  2. Javascript之hoisting变量提升

    javascript不仅仅是一门弱类型语言,还是一门解释型语言.一门编程语言的本质就是这样,优点即是缺点,缺点也往往是优点.JS因为有了变量提升,能够使我们在编程时可以忽略“先声明,再使用”的规则,但 ...

  3. docker 打印带时间的日志

    1, 根据容器日志查看连接情况  docker logs 684  (因为从6.30日开是打印,太慢了.) 2,docker带参数的打印出日志 docker logs 684 --since=&quo ...

  4. PPT插件(islide)

    https://www.islide.cc/features iSlide 主要功能模块   一键优化 将PPT中不规则的字体,段落,色彩,参考线布局,风格样式等一键化全局统一设置,构建专业和规范. ...

  5. RestSharp - Ignore SSL errors

    项目启动时,添加下面代码: 项目启动时,添加 public App() { ServicePointManager.ServerCertificateValidationCallback += (se ...

  6. ES6深入浅出-13 Proxy 与 Reflect-3.Vue 3 将用 Proxy 改写

    如果说想打印出来年龄,但是有没有年龄的这个key值 把创建年龄写在一个按钮上面 通过一个事件来做. 点击创建年龄的按钮,给obj.age设置为18,但是页面的双向绑定并没有显示出来. 因为不响应式,为 ...

  7. Qt Http get

    1.直接建立连接,向网站发送http请求 QNetworkAccessManager *accessManager = new QNetworkAccessManager(this); connect ...

  8. Qt编写自定义控件70-扁平化flatui

    一.前言 对于现在做前端开发人员来说,FlatUI肯定不陌生,最近几年扁平化的设计越来越流行,大概由于现在PC端和移动端的设备的分辨率越来越高,扁平化反而看起来更让人愉悦,而通过渐变色产生的质感色彩反 ...

  9. Python手册 3.7

    Python手册 3.7 下载地址:https://pan.baidu.com/s/1dPzwwP3ehnyLUNWTsB2QJg 关注微信公众号获取提取码: 输入:py99   获取提取码

  10. 单机prometheus vs 集群kube-prometheus+prometheus-operator

    prometheus 组件: node-exporter:9100端口 https://segmentfault.com/a/1190000017959127