Shell编程-03-Shell中的特殊变量和扩展变量
特殊变量? ? 在Shell中的特殊变量主要分别两种位置参数变量、状态变量两种。 位置参数变量? ? Shell中的位置参数变量主要是指$0、$1、$#等,主要用于从命令行、函数或脚本执行等地方传递参数。详细说明如下所示:
位置参数变量示例1、示例一: [[email?protected] Test]# cat para.sh #!/bin/bash echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} echo ‘$0 is:‘ $0 echo ‘$1 is:‘ $1 echo ‘$12 is:‘ ${12} echo ‘$# is:‘ $# echo ‘$* is:‘ $* echo ‘"$*"is:‘ "$*" echo ‘[email?protected] is:‘ [email?protected] echo ‘"[email?protected]"is:‘ "[email?protected]" # 输出结果 [[email?protected]calhost Test]# bash ~/Test/para.sh {a..z} a b c d e f g h i j k l m n o $0 is: /root/Test/para.sh $1 is: a $12 is: l $# is: 26 $* is: 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 "$*"is: 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 [email?protected] is: 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 "[email?protected]"is: 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
2、示例二: [[email?protected] Test]# cat testposition.sh #!/bin/bash echo ‘$# $1 $2 $3 $* [email?protected]‘ echo $# $1 $2 $3 $* [email?protected] echo "************" echo ‘$*‘ for tmp in $* do echo $tmp done echo "************" echo "@@@@@@@@@@@@" echo ‘[email?protected]‘ for temp in [email?protected] do echo $temp done echo "@@@@@@@@@@@@" echo ‘"*"*"*"*"*"*‘ echo ‘$*‘ for i in "$*" do echo $i done echo ‘"*"*"*"*"*"*‘ echo ‘"@"@"@"@"@"@‘ echo ‘[email?protected]‘ for j in "[email?protected]" do echo $j done echo ‘"@"@"@"@"@"@‘ [[email?protected] Test]# bash testposition.sh "Hello Jack" Welcome "to Shanghai" $# $1 $2 $3 $* [email?protected] 3 Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai ************ $* # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开 Hello Jack Welcome to Shanghai ************ @@@@@@@@@@@@ [email?protected] # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开 Hello Jack Welcome to Shanghai @@@@@@@@@@@@ "*"*"*"*"*"* $* # 添加双引号后,传入的参数全部当一个参数进行输出 Hello Jack Welcome to Shanghai "*"*"*"*"*"* "@"@"@"@"@"@ [email?protected] # 添加双引号后,传入的参数全部当独立的参数进行输出 Hello Jack Welcome to Shanghai "@"@"@"@"@"@ 状态变量
在日常使场景中,$?主要用法如下所示:
状态变量示例1、$?示例: [[email?protected] Test]# ll /etc/profile -rw-r--r--. 1 root root 1819 4月 11 2018 /etc/profile [[email?protected] Test]# echo $? 0 [[email?protected] Test]# ll /etc/profild ls: 无法访问/etc/profild: 没有那个文件或目录 [[email?protected] Test]# echo $? 2 2、$$示例: [[email?protected] Test]# cat testPID.sh #!/bin/bash echo $$ > /tmp/test.pid sleep 300 [[email?protected] Test]# bash testPID.sh & # 将当前脚本调用到后台执行 [1] 1671 [[email?protected] Test]# ps -ef | grep testPID | grep -v grep root 1671 23706 0 16:37 pts/0 00:00:00 bash testPID.sh # 查询PID 3、$!示例: [[email?protected] Test]# bash testPID.sh & [1] 24078 [[email?protected] Test]# echo $! 24078 # 打印上一次在后台执行的进程号 [[email?protected] Test]# ps -ef | grep testPID | grep -v grep root 24078 23706 0 16:42 pts/0 00:00:00 bash testPID.sh 4、$_示例: [[email?protected] Test]# bash para.sh {a..z} a b c d e f g h i j k l m n o $0 is: para.sh $1 is: a $12 is: l $# is: 26 $* is: 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 "$*"is: 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 [email?protected] is: 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 "[email?protected]"is: 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 [[email?protected] Test]# echo $_ z # 打印最后一个传入的参数值 Bash 内置变量? ? 常用的内部命令有echo、eval、exec、export、read、shift、exit。 echo? ? 主要用于打印信息,其命令格式如下所示: echo [options] args 常用参数如下所示:
echo常用转义字符如下:
eval? ? 当Shell程序运行到eval语句时,将读入参数args,并将它们组合成一个新的命令而后执行。其命令格式如下所示: eval args exec? ? exec主要用于在不创建新的子进程的情况下,转而执行指定的命令,当指定命令执行完后,则终止该进程。其命令格式如下所示: exec args
read? ? 从标准输入读取变量或字符串等信息并传递给其他变量,其命令格式如下所示 read args shift? ? 对传入的位置参数依次向左移动一个位置,并使用位置参数$#减1,直至0为止。其命令格式如下所示: shift postition args
exit? ? 常用于退出Shell,在日常使用过程中可使用exit num来自定义返回状态数。 Bash 内置变量示例1、echo [[email?protected] Test]# echo "Test";echo "Dao" Test Dao [[email?protected] Test]# echo -n "Test";echo "Dao" TestDao [[email?protected] Test]# echo -e "TesttNamen Dao" Test Name Dao 2、eval [[email?protected] Test]# cat eval.sh #!/bin/bash echo "No eval" echo $$# echo "Add eval" eval echo $$# [[email?protected] Test]# bash eval.sh a b No eval $2 # 未添加evel时,$#为2,则输出$2 Add eval b # 添加evel后,则重新对传入的参数进行解析,则输出传入的第2个参数 3、exec [[email?protected] Test]# exec ls eval.sh para.sh ping.sh testPID.sh testposition.sh [[email?protected] ~]$ # 在执行exec后则终止当前Shell进程,因此从root用户退出到普通用户 # 与read一起读取文件 [[email?protected] ~]# seq 5 > /tmp/rand.log [[email?protected] ~]# cat /tmp/rand.log 1 2 3 4 5 [[email?protected] Test]# cat exec.sh #!/bin/bash exec < /tmp/rand.log while read line do echo $line done echo "Completed" [[email?protected] Test]# bash exec.sh 1 2 3 4 5 Completed 4、read 5、shift [[email?protected] Test]# cat shift.sh #!/bin/bash echo $1 $2 $3 $4 $5 until [ -z $1 ] do echo [email?protected] shift 1 done [[email?protected] Test]# bash shift.sh {1..5} 1 2 3 4 5 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 变量扩展变量扩展说明? ? Shell中变量扩展说明如下所示:
其中${var:-word}、${var:=word}、${var:?word}、${var:+word}中的冒号也可以省略,则将变量为空或未赋值修改为未赋值,去掉了为空的检测, 即运算符仅检测变量是否未赋值 变量扩展示例[[email?protected] init.d]# var="This is test string" [[email?protected] init.d]# echo $var This is test string [[email?protected] init.d]# echo ${var} This is test string [[email?protected] init.d]# echo ${#var} # 统计字符长度 19 [[email?protected] init.d]# echo ${var:5} # 从第5个位置开始截取字符 is test string [[email?protected] init.d]# echo ${var:5:2} # 从第5个位置开始截取2个字符 is [[email?protected] init.d]# echo ${var#This} # 从开头删除最短匹配的字符 is is test string [[email?protected] init.d]# echo ${var##This} # 从开头删除最长匹配的字符 is is test string [[email?protected] init.d]# echo ${var%g} # 从结尾删除最短匹配的字符 is This is test strin [[email?protected] init.d]# echo ${var%%g} # 从结尾删除最长匹配的字符 is This is test strin [[email?protected] init.d]# echo ${var/is/newis} # 替换第一个匹配的字符 Thnewis is test string [[email?protected] init.d]# echo ${var//is/newis} # 替换所有匹配到的字符 Thnewis newis test string [[email?protected] init.d]# echo $centos # 变量未定义 [[email?protected] init.d]# echo ${centos:-UNDEFINE} # 变量为空,返回UNDEFINE UNDEFINE [[email?protected] init.d]# centos="CentOS" [[email?protected] init.d]# echo ${centos:-UNDEFINE} # 变量已经定义,返回变量本身的值 CentOS [[email?protected] init.d]# unset centos # 取消变量值 [[email?protected] init.d]# echo $centos [[email?protected] init.d]# result=${centos:=UNDEFINE} [[email?protected] init.d]# echo $result UNDEFINE [[email?protected] init.d]# echo $centos # 变量值为空,则将UNDEFINE赋值给centos UNDEFINE [[email?protected] init.d]# unset centos [[email?protected] init.d]# echo ${centos:?can not find variable centos} -bash: centos: can not find variable centos # 变量值为空,输出自定义错误信息 [[email?protected] init.d]# centos="IS DEFINED" [[email?protected] init.d]# echo ${centos:?can not find variable centos} IS DEFINED #变量值已定义,则输出变量值 [[email?protected] init.d]# unset centos [[email?protected] init.d]# echo ${centos:+do nothing} # 变量值为空,什么都不操作输出 [[email?protected] init.d]# centos="do" [[email?protected] init.d]# echo ${centos:+do nothing} # 变量已赋值,则输出自定义的消息 do nothing 本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |