云计算—shell编程之 if 语句,单分支,多分支,逻辑与&&和逻辑或 ||
...
#如果提示用户输入密码的信息,然后从标准输入隐式的读取用户的输入,并将读取的内容赋值给变量pass
read -sp "Enther a password: " pass
#如果变量pass的指是xieqc,则显示密码验证通过的信息,然后推出脚本,退出状态码是0
if [ “$pass” == “xieqc” ]
then
echo -e “\nPassword verified.”
exit 0
else
#显示拒绝访问的信息
echo -e “\nAccess denied”
fi
#退出脚本,退出状态码为1
exit 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
加入了else语句,整个世界都不一样了,有了另外一个选择,让我们多了一个选择的余地,不至于太过狼狈。那想不想有更多的选择呢?
嵌套的if/else语句,多级的if…elif…else…fi
来看一个嵌套的语句:
#!/bin/bash
#===================================
#
# FILE:checkcunt.sh
# AUTHOR:Xie_qi_chao
# CREATED:15/03/2019 23:56
#
#===================================
#声明一个整型变量
declare -i count
#显示提示输入一个数值的信息,然后将用户的输入存入变量count中
read -p "Ether an count: " count
if [ $count -eq 100 ];then
echo “Count is 100.”
else
if [ $count -gt 100 ];then
echo “Count is greater than 100.”
else
echo "Count is less than 100."
fi
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
上诉脚本,判断输入的数字是否 大于还是小于100,条件成立,则输出对应的语句。
多级的if…elif…else…fi
我们来看一个 用多级语句写一个 判断数值是正数还是负数的脚本:
#!/bin/bash
#=========================================
#
# FILE:checknumber.sh
# AUTHOR: Xie_qi_chao
# CREATED:16/03/2019 00:23
#
#=========================================
#如果指定的命令行参数个数等于0,则显示必须指定一个参数的提示信息,然后退出脚本,退出状态码为1
if [ $# -eq 0 ] ;then
#显示必须指定一个参数的提示信息
echo "$0 : you must give/supply one integers."
#退出脚本,退出状态码为1
exit 1
fi
#如果指定的参数大于0
if [ $1 -gt 0 ];then
echo “the number is positive.”
#如果指定的参小于0
elif [ $1 -lt 0 ];then
echo “the number is negative.”
#如果指定的参数等于0
elif [ $1 -eq 0 ];then
echo “the number is 0.”
else
echo “Opps! $1 is not number, give number.”
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
逻辑与&&和逻辑或 ||
- 逻辑与——只有当前一个命令执行成功时才执行后一个命令。
- 逻辑或——只有当前一个命令执行失败时才执行后一个命令。
上脚本吧,啥也别说了:
[root@xieqc ~]# ls dir &> /dev/null && echo "sucess" || echo "fail "
fail
- 1
- 2
上诉脚本中,如果dir目录存在,则打印sucess,否则打印fail,可以看出来,&&是和的意思,||是只有前一个条件不成立才会打印。
下一篇整理下云计算的基本概念,和初步认识linux。。。
</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">5</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>
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)