哪些网站做推广效果好,南京 外贸网站建设,自己做公司网站,plc培训机构哪家最好华子目录 if语句单分支案例 双分支案例 多分支 case多条件判断格式执行过程示例 if语句
单分支
# 语法1#xff1a;
if 条件表达式
then指令
fi#语法2#xff1a;
if 条件表达式;then指令
fi案例
编写脚本choice1.sh,利用单分支结构实现输入2个整数#… 华子目录 if语句单分支案例 双分支案例 多分支 case多条件判断格式执行过程示例 if语句
单分支
# 语法1
if 条件表达式
then指令
fi#语法2
if 条件表达式;then指令
fi案例
编写脚本choice1.sh,利用单分支结构实现输入2个整数判断最大值后输出$符相当于取值符号
[rootserver ~]# vim choice1.sh
#!/bin/bash
read -p 请输入第一个整数: x
read -p 请输入第二个整数: y
max$x
if (($max$y))
then max$y
fi [rootserver ~]# bash choice1.sh
请输入第一个整数6
请输入第二个整数3
最大值6编写脚本choice3.sh 判断当前脚本的执行者身份若不是root账户执行则输出提示并退出
# 检查当前账户4种方法
[rootserver ~]# whoami
root
[rootserver ~]# id -u
0
[rootserver ~]# echo $USER
root
[rootserver ~]# echo $UID
0[rootserver ~]# vim choice3.sh
#!/bin/bash
if [ $USER ! root ]
thenecho please switch user root
fi[rootserver ~]# mv choice3.sh / # 移动脚本到根
[rootserver ~]# su redhat # 切换账户身份
[foxserver root]$ cd /
[foxserver /]$ bash choice3.sh
please switch user root双分支
if 条件表达式
then指令序列1
else指令序列2
fi案例
面试题编写脚本choice2.sh使用双分支判断当前系统的剩余内存大小若低于100M则发送消息进行告警
[rootserver ~]# vim choice2.sh
#!/bin/bash
free_mem$(free -m | grep Mem | tr -s | cut -d -f 4)
if (($free_mem100))
thenecho 警告剩余内存为$free_mem,低于100MB
elseecho 剩余内存为$free_mem,空间足够
fi[rootserver ~]# bash choice2.sh
剩余内存为948,空间足够编写脚本choice4.sh 实现闰年的判断闰年能被4整除但不能被100整除或者能被400整除的年份
[rootserver ~]# vim choice4.sh
#!/bin/bash
read -p 请输入四位数年份 year
if [ $(($year%4)) -eq 0 ] [ $(($year%100)) -ne 0 ] || [ $(($year%400)) -eq 0 ]
thenecho $year年是润年
elseecho $year年是平年
fi[rootserver ~]# bash choice4.sh
请输入四位数年份2018
2018年是平年
[rootserver ~]# bash choice4.sh
请输入四位数年份2024
2024年是润年编写脚本choice5.sh判断sshd是否运行
# 分析
# 1.通过分析服务执行的进程数判断
[rootserver ~]# ps -ef | grep 服务名 | grep -v grep | wc -l
# 2.通过查看是否开放端口判断
[rootserver ~]# netstat -lntup | grep 端口号 | wc -l[rootserver ~]# vim choice5.sh
#!/bin/bash
num$(ps -ef | grep sshd | grep -v grep | wc -l)
if (($num0))
thenecho sshd is running
elseecho sshd is not running
fi[rootserver ~]# bash choice5.sh
sshd is not running编写脚本choice8.sh检测主机是否存活
[rootserver ~]# vim choice8.sh
#!/bin/bash
read -p 请输入测试主机的IP地址 ip
ping -c 2 -w 3 $ip /dev/null
# -c 2 表示发出2个数据包-w 3表示等待3秒结束注意不能等待1秒结束有可能第二个包未返回就结束会报错
if [ $? -eq 0 ]
thenecho 主机$ip已运行
elseecho 主机$ip未运行
fi[rootserver ~]# bash choice8.sh
请输入测试主机的IP地址192.168.80.130
主机192.168.80.130未运行# 上例修改使用循环测试多台主机存活的主机显示应用高亮的颜色显示
#!/bin/bashfor ip in 192.168.48.{125..135}
doping -c 2 -w 3 $ip /dev/nullif [ $? -eq 0 ]thenecho -e \e[1;31m主机$ip已运行\e[0melseecho 主机$ip未运行fi
done 多分支
if 条件表达式1
then指令序列1
elif 条件表达式2
then指令序列2
else指令序列n
fi编写脚本choice6.sh 输入百分制成绩判断后输出等级成绩
[rootserver ~]# vim choice6.sh
read -p 请输入百分制成绩: score
if [ -z $score ]
thenecho 未输入,请重新输入.
elif (($score0 || $score100 ))
thenecho 成绩输入有误请输入0-100间整数成绩
elif (($score 90))
thenecho 成绩优秀
elif (($score 80))
thenecho 成绩良好
elif (($score60))
thenecho 成绩及格
elseecho 补考
fi[rootserver ~]# bash choice6.sh
请输入百分制成绩
未输入请重新输入
[rootserver ~]# bash choice6.sh
请输入百分制成绩100
成绩优秀编写脚本choice7.sh判断主机的cpu品牌
[rootserver ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 186
model name : 13th Gen Intel(R) Core(TM) i7-13620H
stepping : 2
microcode : 0x410e
cpu MHz : 2918.399
cache size : 24576 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 32
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni arat umip pku ospke gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities
bugs : spectre_v1 spectre_v2 spec_store_bypass swapgs itlb_multihit eibrs_pbrsb
bogomips : 5836.79
clflush size : 64
cache_alignment : 64
address sizes : 45 bits physical, 48 bits virtual
power management:processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 186
model name : 13th Gen Intel(R) Core(TM) i7-13620H
stepping : 2
microcode : 0x410e
cpu MHz : 2918.399
cache size : 24576 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 32
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni arat umip pku ospke gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities
bugs : spectre_v1 spectre_v2 spec_store_bypass swapgs itlb_multihit eibrs_pbrsb
bogomips : 5836.79
clflush size : 64
cache_alignment : 64
address sizes : 45 bits physical, 48 bits virtual
power management:[rootserver ~]# grep vendor_id /proc/cpuinfo
vendor_id : GenuineIntel
vendor_id : GenuineIntel[rootserver ~]# vim choice7.sh
#!/bin/bash
vendor$(grep vendor_id /proc/cpuinfo | uniq | cut -d -f2)
if [ $vendor GenuineIntel ]
thenecho Inter
elif [ $vendor GenuineAMD ] # AuthenticAMD
thenecho AMD
elseecho unknow
fi[rootserver ~]# bash choice7.sh
Inter编写脚本choice9.sh根据用户的输入内容判断是数字、字母、其它字符
[rootserver ~]# vim choice9.sh
#!/bin/bash
read -p 请输入字母、数字、其它字符: str
if echo $str | grep [a-zA-Z] /dev/null
thenecho 字母
elif echo $str | grep [0-9] /dev/null
thenecho 数字
elseecho 字符
fi[rootserver ~]# bash choice9.sh
请输入字母数字其他字符890
数字
[rootserver ~]# bash choice9.sh
请输入字母数字其他字符adg
字母
[rootserver ~]# bash choice9.sh
请输入字母数字其他字符!#
字符case多条件判断
格式
case 变量名 in值1)指令1;;值2)指令2;;值3)指令3 ;;*默认
esac #结束case语句执行过程
case语句会将该变量的值与 括号中的值相比较 如果与某个值相等则执行对应语句。当遇到“;;”符号时就跳出case语句执行esac语句后面的语句。如果没有与任何一个值相匹配则执行*)后面的一组语句
示例
编写脚本choice10.sh对上例7的百分制成绩判断等级成绩进行改写
[rootserver ~]# vim choice10.sh
#!/bin/bash
read -p 请输入百分制成绩: score
case $score in9[0-9]|100)echo 成绩优秀;;8[0-9])echo 成绩良好;;6[0-9]|7[0-9])echo 成绩及格;;*)echo 补考
esac[rootserver ~]# bash choice10.sh
请输入百分制成绩99
成绩优秀
[rootserver ~]# bash choice10.sh
请输入百分制成绩9
补考