特殊变量
在Shell中的特殊变量主要分别两种位置参数变量、状态变量两种。
位置参数变量
Shell中的位置参数变量主要是指0、0、1、$#等,主要用于从命令行、函数或脚本执行等地方传递参数。详细说明如下所示:
当"∗"和"∗"和"@"都添加双引号时,两者的功能有所区别;如不加,则功能相同,无区别。
位置参数变量示例
1、示例一:
[root@localhost Test]# cat para.sh#!/bin/bashecho $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}echo '$0 is:' $0echo '$1 is:' $1echo '$12 is:' ${12}echo '$# is:' $#echo '$* is:' $*echo '"$*"is:' "$*"echo '$@ is:' $@echo '"$@"is:' "$@"# 输出结果[root@localhost 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$@ 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
1、传入的参数个数多于脚本定义的参数时,则多出的参数会忽略
2、传入的参数中如使用双引号,则会当作一个参数值进行传递
3、位置参数如大于9,需要使用${}进行传递
2、示例二:
[root@localhost Test]# cat testposition.sh #!/bin/bashecho '$# $1 $2 $3 $* $@'echo $# $1 $2 $3 $* $@echo "************"echo '$*'for tmp in $* do echo $tmp doneecho "************"echo "@@@@@@@@@@@@"echo '$@'for temp in $@ do echo $temp doneecho "@@@@@@@@@@@@"echo '"*"*"*"*"*"*'echo '$*'for i in "$*" do echo $i doneecho '"*"*"*"*"*"*'echo '"@"@"@"@"@"@'echo '$@'for j in "$@" do echo $j doneecho '"@"@"@"@"@"@'[root@localhost Test]# bash testposition.sh "Hello Jack" Welcome "to Shanghai"$# $1 $2 $3 $* $@3 Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai Hello Jack Welcome to Shanghai************$* # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开HelloJackWelcometoShanghai************@@@@@@@@@@@@$@ # 未加双引号,所以会输出全部参数,则第一个和第三个参数会拆开HelloJackWelcometoShanghai@@@@@@@@@@@@"*"*"*"*"*"*$* # 添加双引号后,传入的参数全部当一个参数进行输出Hello Jack Welcome to Shanghai"*"*"*"*"*"*"@"@"@"@"@"@$@ # 添加双引号后,传入的参数全部当独立的参数进行输出Hello JackWelcometo Shanghai"@"@"@"@"@"@
状态变量
以上四个状态变量,仅$?常用,其他三个了解即可。
在日常使场景中,$?主要用法如下所示:
1、判断命令和脚本是否执行成功
2、如脚本中调用exit 数字,则会返回该数字给$?
3、如在函数中,则可以通过return 数字将该数字返回给$?
状态变量示例
1、$?示例:
[root@localhost Test]# ll /etc/profile-rw-r--r--. 1 root root 1819 4月 11 2018 /etc/profile[root@localhost Test]# echo $?0[root@localhost Test]# ll /etc/profildls: 无法访问/etc/profild: 没有那个文件或目录[root@localhost Test]# echo $?2
2、$$示例:
[root@localhost Test]# cat testPID.sh#!/bin/bashecho $$ > /tmp/test.pidsleep 300[root@localhost Test]# bash testPID.sh & # 将当前脚本调用到后台执行[1] 1671[root@localhost Test]# ps -ef | grep testPID | grep -v greproot 1671 23706 0 16:37 pts/0 00:00:00 bash testPID.sh # 查询PID
3、!示例:!示例:!功能类似于$$,只不过是获取上一次执行脚本的PID
[root@localhost Test]# bash testPID.sh &[1] 24078[root@localhost Test]# echo $!24078 # 打印上一次在后台执行的进程号[root@localhost Test]# ps -ef | grep testPID | grep -v greproot 24078 23706 0 16:42 pts/0 00:00:00 bash testPID.sh
4、$_示例:
[root@localhost 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$@ 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[root@localhost Test]# echo $_z # 打印最后一个传入的参数值
Bash 内置变量
常用的内部命令有echo、eval、exec、export、read、shift、exit。
echo
主要用于打印信息,其命令格式如下所示:
echo [options] args
常用参数如下所示:
参数选项 | 说明 |
---|---|
-n | 不换行输出内容 |
-e | 解析转义字符 |
echo常用转义字符如下:
转义字符 | 说明 |
---|---|
/n | 换行 |
/r | 回车 |
/t | Tab |
/b | 退格 |
/v | 纵向制表符 |
eval
当Shell程序运行到eval语句时,将读入参数args,并将它们组合成一个新的命令而后执行。其命令格式如下所示:
eval args
exec
exec主要用于在不创建新的子进程的情况下,转而执行指定的命令,当指定命令执行完后,则终止该进程。其命令格式如下所示:
exec args
当使用exec打开文件后,read命令每次都会将文件指针移动到下一行进行读取,直至结束。因此常用来处理文件内容。
read
从标准输入读取变量或字符串等信息并传递给其他变量,其命令格式如下所示
read args
shift
对传入的位置参数依次向左移动一个位置,并使用位置参数$#减1,直至0为止。其命令格式如下所示:
shift postition args
shift如果不带参数,则默认左移1位
如传入的参数为112 3,如执行一次shift,则之前的3,如执行一次shift,则之前的3变成2,2,2变成1,1,1消失。
exit
常用于退出Shell,在日常使用过程中可使用exit num来自定义返回状态数。
Bash 内置变量示例
1、echo
[root@localhost Test]# echo "Test";echo "Dao"TestDao[root@localhost Test]# echo -n "Test";echo "Dao"TestDao[root@localhost Test]# echo -e "Test/tName/n Dao"Test Name Dao
2、eval
[root@localhost Test]# cat eval.sh#!/bin/bashecho "No eval"echo /$$#echo "Add eval"eval echo /$$#[root@localhost Test]# bash eval.sh a bNo eval$2 # 未添加evel时,$#为2,则输出$2Add evalb # 添加evel后,则重新对传入的参数进行解析,则输出传入的第2个参数
3、exec
[root@localhost Test]# exec lseval.sh para.sh ping.sh testPID.sh testposition.sh[admin@localhost ~]$ # 在执行exec后则终止当前Shell进程,因此从root用户退出到普通用户# 与read一起读取文件[root@localhost ~]# seq 5 > /tmp/rand.log[root@localhost ~]# cat /tmp/rand.log 12345[root@localhost Test]# cat exec.sh#!/bin/bashexec < /tmp/rand.logwhile read line do echo $line doneecho "Completed"[root@localhost Test]# bash exec.sh12345Completed
4、read
可以参考公众号中read命令一文
5、shift
[root@localhost Test]# cat shift.sh#!/bin/bashecho $1 $2 $3 $4 $5until [ -z $1 ]do echo $@ shift 1done[root@localhost Test]# bash shift.sh {1..5}1 2 3 4 51 2 3 4 52 3 4 53 4 54 55
变量扩展
变量扩展说明
Shell中变量扩展说明如下所示:
其中${var:-word}、${var:=word}、${var:?word}、${var:+word}中的冒号也可以省略,则将变量为空或未赋值修改为未赋值,去掉了为空的检测, 即运算符仅检测变量是否未赋值
变量扩展示例
[root@localhost init.d]# var="This is test string"[root@localhost init.d]# echo $varThis is test string[root@localhost init.d]# echo ${var}This is test string[root@localhost init.d]# echo ${#var} # 统计字符长度19[root@localhost init.d]# echo ${var:5} # 从第5个位置开始截取字符is test string[root@localhost init.d]# echo ${var:5:2} # 从第5个位置开始截取2个字符is[root@localhost init.d]# echo ${var#This} # 从开头删除最短匹配的字符 isis test string[root@localhost init.d]# echo ${var##This} # 从开头删除最长匹配的字符 isis test string[root@localhost init.d]# echo ${var%g} # 从结尾删除最短匹配的字符 isThis is test strin[root@localhost init.d]# echo ${var%%g} # 从结尾删除最长匹配的字符 isThis is test strin[root@localhost init.d]# echo ${var/is/newis} # 替换第一个匹配的字符Thnewis is test string[root@localhost init.d]# echo ${var//is/newis} # 替换所有匹配到的字符Thnewis newis test string[root@localhost init.d]# echo $centos # 变量未定义[root@localhost init.d]# echo ${centos:-UNDEFINE} # 变量为空,返回UNDEFINEUNDEFINE[root@localhost init.d]# centos="CentOS"[root@localhost init.d]# echo ${centos:-UNDEFINE} # 变量已经定义,返回变量本身的值CentOS[root@localhost init.d]# unset centos # 取消变量值[root@localhost init.d]# echo $centos[root@localhost init.d]# result=${centos:=UNDEFINE}[root@localhost init.d]# echo $resultUNDEFINE[root@localhost init.d]# echo $centos # 变量值为空,则将UNDEFINE赋值给centosUNDEFINE[root@localhost init.d]# unset centos[root@localhost init.d]# echo ${centos:?can not find variable centos}-bash: centos: can not find variable centos # 变量值为空,输出自定义错误信息[root@localhost init.d]# centos="IS DEFINED"[root@localhost init.d]# echo ${centos:?can not find variable centos}IS DEFINED #变量值已定义,则输出变量值[root@localhost init.d]# unset centos[root@localhost init.d]# echo ${centos:+do nothing} # 变量值为空,什么都不操作输出[root@localhost init.d]# centos="do"[root@localhost init.d]# echo ${centos:+do nothing} # 变量已赋值,则输出自定义的消息do nothing
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对武林网的支持。
新闻热点
疑难解答