linux Shell学习笔记第三天

2023-12-05 0 624

第三天:条件选择



大 纲



应用实例分析


条件测试


if…else…fi


case…in…esac



实现功能菜单:


执行脚本后


按1,显示当前时间


按2,显示CPU负载


按3,显示剩余内存


按0,退出脚本


按其他字符,提示超出选择范围后退出


linux Shell学习笔记第三天


分析步骤。


#date +%T


uptime awk截取


free –m


条件测试格式


#test –option obj


#[ -option obj ]


返回结果

表达式内容测试结果是真的

表达式内容测试结果是假的


测试的对象分类



执行结果(执行成功或失败)


文件(文件是否存在等)


文本(是否一致)


数字(数值比较)


条件测试的选项



选项
作用

-d
目录

-e
是否存在

-f
是否是普通文件

-s
文件大小是否等于0

-r
是否可读

-w
是否可写

-x
是否可执行


逻辑操作符号


选项


作用

-a


与操作

-o


或操作

!


非操作


实例:


#test –e /etc/passwd –a –e /etc/shadow 中间是a与操作,则都为0才得0


#test –e /etc/passwd –o –e /etc/groups 中间是o或操作,则有一真则真0


字符串操作符



==两个字符串相等


!=两个字符串不相等


-z空字符串


-n非空字符串


实例:


#test –z $LOGNAME


#echo $LOGNAME


#echo $?


linux Shell学习笔记第三天


数值比较操作符


符号


说明

-eq


等于

-ne


不等于

-gt


大于

-lt


小于

-ge


大于等于

-le


小于等于


if…else…fi 条件选择



if控制结构的基本格式:


if条件#判断开始可以是命令,也可以是test语句


then#如果条件为真反值真0则执行


命令1#执行命令1


else#如果条件为假反值假1则执行


命令2#执行命令2


fi#判断结束


实例(if…else…fi)1



inputtest.sh


#!/bin/bash


#input test


echo –n “Enter your name:”


read name


#did the user just hit return


if [ \”${name}\” == \”\” ]


then


echo “You did not enter any information”


else


echo “Your name: ${name}”


fi




linux Shell学习笔记第三天


实例(if…else…fi)2



filecopy.sh


#!/bin/bash


#file copy


if cp /etc/passwd passwd.bak 2>/dev/null2>/dev/null 丢掉错误提示


then


echo “Good Copy!”


else


echo “`basename $0`: error,could not copy”


fi


linux Shell学习笔记第三天


if…else…fi的嵌套 (两层的嵌套)



if 条件1;then


if 条件2;then


命令1


else


命令2


else


if条件3;then


命令3


else


命令4


fi


case…in…esac条件选择 (比较灵活的方式)



case语句多用于较多分支判断


case格式: (多种模式,只匹配和variable相等的模式)


case variable in


模式1)命令1…;;


模式2)命令2…;;


esac


匹配模式


*匹配任意字符


?匹配任意单字符


[]匹配字符范围




case…in.esac实例1



#!/bin/bash


#case select


echo –n “enter a number from 1 to 5:”


read NUM


case $NUM in


1) echo “you select 1″ ;;


2) echo “you select 2″ ;;


3) echo “you select 3″ ;;


4) echo “you select 4″ ;;


5) echo “you select 5″ ;;


*) echo “basename $This is not between 1 and 5″


esac


linux Shell学习笔记第三天


case…in.esac实例2



题目是:学生的考试成绩是0-100分,在85以上的要提示you are the best!,在70-84显示you get a good mark! ,在60-74的显示come on!,60分以下显示You must study hard!


#!/bin/bash


echo –n “please input your mark:”


read mark


case $mark in


100|9[0-9]|8[5-9]) echo “you are the best!”;;100、90-99、85-89


8[0-4]|7[0-9]) echo “you get a good mark!”;;80-84、70-79


7[0-4]|6[0-9]) echo “come on!”;;70-74、60-69


[0-5][0-9]) echo “You must study hard!”;;00-59


esac


解决今天的问题



使用if…else…fi的方式对输入的变量进行判断。


在每个判断的分支上执行相应的语句。


menu.sh



#!/bin/bash



clear



echo “——————–menu—————–”



echo “1) Show Time”



echo “2) CPU load”



echo “3) Memory free”



echo “0) Exit”



echo “——————————————–”



echo -n “Enter you chose [0-3]:”



read NUM





if [ ${NUM} -lt 0 -o ${NUM} -gt 3 ]



then



echo “This is not between 0-3.”



else



if [ \”${NUM}\” == \”1\” ]



then



echo “`date +%T`”



else



if [ \”${NUM}\” == \”2\” ]



then



echo “`uptime | awk -F ‘[,:]‘ ‘{print $7}\’`”



else



if [ \”${NUM}\” == \”3\” ]



then



echo “`free -m | awk ‘$1==”Mem:”{print $4}\’`”



else



exit



fi



fi



fi



fi



linux Shell学习笔记第三天linux Shell学习笔记第三天


linux Shell学习笔记第三天linux Shell学习笔记第三天


本节课回顾:



条件测试的类型



文件测试


文本测试


数值测试


逻辑测试


if…else…fi条件选择结构



case…in…esac



课后测试


1、修改menu.sh 采用非菜单式,参数传递方式来进行选择。 例如 #./menu.sh 1 输出时间


2、使用case方式来实现该菜单选择方式


Sudu答案:(难免会有错误,但是可以实现成功)


1、修改menu.sh后得出


#!/bin/bash


if [ $1 -lt 0 -o $1 -gt 3 ]


then


echo “This is not between 0-3.”


else


if [ \”$1\” == \”1\” ]


then


echo “`date +%T`”


else


if [ \”$1\” == \”2\” ]


then


echo “`uptime | awk -F ‘[,:]‘ ‘{print $7}\’`”


else


if [ \”$1\” == \”3\” ]


then


echo “`free -m | awk ‘$1==”Mem:”{print $4}\’`”


else


exit


fi


fi


fi


fi


linux Shell学习笔记第三天


2、#!/bin/bash


clear


echo “——————–menu—————–”


echo “1) Show Time”


echo “2) CPU load”


echo “3) Memory free”


echo “0) Exit”


echo “——————————————–”


echo -n “Enter you chose [0-3]:”


read NUM


case $NUM in


1) date +%T;;


2) uptime | awk -F ‘[,:]‘ ‘{print $7}\’;;


3) free -m | awk ‘$1==”Mem:”{print $4}\’;;


0) exit ;;


*) echo “This is not between 0-3.” ;;


esac


今天收获还是比较多的。 半个小时的教程看了将近3个小时。


虽然说if…else…fi比较容易理解,但是用case感觉简单很多,呵呵,看个人喜好吧。


每天看来看一节教程就足够了。 看多了头也会晕的。 呵呵。 继续学习吧~

您可能感兴趣的文章:linux shell数组深入学习理解linux shell脚本学习xargs命令使用详解Linux学习之CentOS(一)—-在VMware虚拟机中安装CentOS 7(图文教程)linux Shell学习笔记第一天Linux shell脚本基础学习详细介绍(完整版)linux Shell学习笔记第四天linux Shell学习笔记第五天linux Shell学习笔记最后一节,温故与知新linux Shell学习笔记第二天linux命令学习之10个网络命令和监控命令Linux学习资料下载(电子书籍)linux shell脚本基础知识学习Linux学习基础教程Linux命令学习总结:详解shutdown命令Linux命令学习总结:详解reboot命令Linux学习第一天——ssh登录和软件安装详解个人学习Linux知识总结快速自学Linux命令的4种方法

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 其它脚本 linux Shell学习笔记第三天 https://www.u-9.cn/jiaoben/qita-jiaoben/102092.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务