继续再来两个内部变量 $TMOUT $UID
内部变量之$TMOUT
先来看一下概念:
作为内部变量read的默认超时秒数。read我会在接下来的博客中详细提到。
首先此变量需要时一个非零的值。想想也是对的,超时秒数是零的话肯定是不行的。
废话少说,来一个脚本解释一下:
#vim Timeout.sh
#!/bin/bash
#==================================================
#
# FILE:Timeout.sh
# AUTHOR:Xie_qc
# CREATEED:14/03/2019
#===================================================
set -o nounset
#此命令可检测变量是否存在,如果不存在,bash报错并停止
TMOUT=3
echo “Are you sure (Y/N)”
read input
if [ “$input” == “Y” ];then
echo “Contime…”
else
echo “Exit!”
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
我们来测试一下效果:
[root@xieqc shell]# sh Timeout.sh
Are you sure (Y/N)
Y
Contime..
[root@xieqc shell]# sh Timeout.sh
Are you sure (Y/N)
Exit!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
在上面的栗子中,如果等你三秒后还没有输入,则脚本推出!
下一个$UID
当前用户的账号标识码
可以理解为用户账号的身份证号码
需要注意的是,UID是一个只读变量,不能进行修改
不说废话,上脚本:
vim uaroot.sh
#!/bin/bash
#==============================================
# FIEL:uaroot.sh
# AUTHOR:Xie_qc
# CREADTED: 14/03/2019
#
#+=============================================
root_uid=0
if [ “
U
I
D
"
−
e
q
"
UID" -eq "
UID"−eq"root_uid” ];then
echo “you are root”
else
echo “you are just an ordinary user.”
fi
exit 0
//以此来判断用户 是不是root
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
内部变量告一段落,我们继续看一看位置参数
位置参数
bash中的位置参数,除了0以外的数字来进行表示
来来看一下使用的格式: 使用set命令进行赋值,用$[N] 来进行调取 0-9可以用$N来调取
举个栗子:
[root@xieqc shell]# set 1 2 3 four five six 7 8 9 ten
[root@xieqc shell]# echo "$1 $2 $3 $4 $5 $6 $6 $7 $8 $9 $[10]"
1 2 3 four five six six 7 8 9 10
- 1
- 2
- 3
上面的栗子告诉我们栗子真的很好吃。。。
10以上的数字必须用[]中括号括起来
注意:位置变量不能赋值语句来赋值
只能通过set或者shift来设置和取消
在脚本中可以 读取的位置变量在脚本名字的后面,用空格分隔,从1开始的位置变量
上脚本:
#!/bin/bash
#====================================
#
# FILE:show_positional_param.sh
# AUTHOR: Xie_qc
# CREATED: 14/03/2019 19:45
#
#======================================
echo “Argument 1 : $1”
echo “Argument 2 : $2”
echo “Argument 3 : $3”
echo “Argument 4 : $4”
echo “Argument 5 : $5”
[root@xieqc shell]# sh show_positional_parm.sh one two three four five
Argument 1 : one
Argument 2 : two
Argument 3 : three
Argument 4 : four
Argument 5 : five
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
从结果可以看到使用方式。继续学习下一个:
特殊参数:* @ # ? - $ ! 0 _
看到这些符号就头疼,别着急,我们一个一个来
特殊参数 *
- 显示所有位置参数
#set one two three
#echo "$*"
one two three
特殊参数 ?
- 最近一个执行的命令的退出状态
#touch newfile
#echo $?
0
#ls new file
newfile
#echo $?
0
#rm -rf newfile
#echo $?
0
#ls newfile
ls:newfile: No such file or directory
#echo $?
2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
可以看出,如果命令执行正确,则返回值为0,错误就是非0值
特殊参数 -
- 扩展为当前的选项标志
特殊参数 $
- 扩展为当前shell的进程号
#echo $$
28072
- 1
- 2
特殊参数!
- 扩展为最近执行的后台命令的进程号
[root@xieqc shell]# sleep 10 &
[1] 13410
[root@xieqc shell]# echo $!
13410
//栗子很清楚了不是么。。。
- 1
- 2
- 3
- 4
- 5
特殊参数0
- 扩展为shell脚本的名称
- 也就是 $0 是代表脚本本身的名字
特殊参数@
- 相当于一下子调用所有位置变量
特殊参数#
- 统计位置变量的个数,显示结果为有多少个位置参数
好了,木有了…
上面有几个就不举栗子了,感兴趣的童鞋可以自己去操作一下,记得用$符号进行调用哦
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">2</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>