# 语法1:
if <条件表达式>
then
指令
fi
#语法2:
if <条件表达式>;then
指令
fi
[root@server ~]# vim choice1.sh
#!/bin/bash
read -p "请输入第一个整数: " x
read -p "请输入第二个整数: " y
max=$x
if (($max<$y))
then
max=$y
fi
[root@server ~]# bash choice1.sh
请输入第一个整数:6
请输入第二个整数:3
最大值:6
# 检查当前账户4种方法:
[root@server ~]# whoami
root
[root@server ~]# id -u
0
[root@server ~]# echo $USER
root
[root@server ~]# echo $UID
0
[root@server ~]# vim choice3.sh
#!/bin/bash
if [ "$USER" != "root" ]
then
echo "please switch user root"
fi
[root@server ~]# mv choice3.sh / # 移动脚本到根
[root@server ~]# su redhat # 切换账户身份
[fox@server root]$ cd /
[fox@server /]$ bash choice3.sh
please switch user root
if <条件表达式>
then
指令序列1
else
指令序列2
fi
[root@server ~]# vim choice2.sh
#!/bin/bash
free_mem=$(free -m | grep Mem | tr -s " " | cut -d " " -f 4)
if (($free_mem<100))
then
echo "警告,剩余内存为:$free_mem,低于100MB"
else
echo "剩余内存为:$free_mem,空间足够"
fi
[root@server ~]# bash choice2.sh
剩余内存为:948,空间足够
[root@server ~]# vim choice4.sh
#!/bin/bash
read -p "请输入四位数年份:" year
if [ $(($year%4)) -eq 0 ] && [ $(($year%100)) -ne 0 ] || [ $(($year%400)) -eq 0 ]
then
echo "$year年是润年"
else
echo "$year年是平年"
fi
[root@server ~]# bash choice4.sh
请输入四位数年份:2018
2018年是平年
[root@server ~]# bash choice4.sh
请输入四位数年份:2024
2024年是润年
# 分析
# 1.通过分析服务执行的进程数判断
[root@server ~]# ps -ef | grep 服务名 | grep -v grep | wc -l
# 2.通过查看是否开放端口判断
[root@server ~]# netstat -lntup | grep 端口号 | wc -l
[root@server ~]# vim choice5.sh
#!/bin/bash
num=$(ps -ef | grep sshd | grep -v grep | wc -l)
if (($num>0))
then
echo "sshd is running"
else
echo "sshd is not running"
fi
[root@server ~]# bash choice5.sh
sshd is not running
[root@server ~]# 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 ]
then
echo "主机$ip已运行"
else
echo "主机$ip未运行"
fi
[root@server ~]# bash choice8.sh
请输入测试主机的IP地址:192.168.80.130
主机192.168.80.130未运行
# 上例修改,使用循环测试多台主机,存活的主机显示应用高亮的颜色显示
#!/bin/bash
for ip in 192.168.48.{125..135}
do
ping -c 2 -w 3 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo -e "\e[1;31m主机$ip已运行\e[0m"
else
echo "主机$ip未运行"
fi
done
if 条件表达式1
then
指令序列1
elif 条件表达式2
then
指令序列2
else
指令序列n
fi
[root@server ~]# vim choice6.sh
read -p "请输入百分制成绩: " score
if [ -z $score ]
then
echo "未输入,请重新输入."
elif (($score<0 || $score>100 ))
then
echo "成绩输入有误,请输入0-100间整数成绩"
elif (($score >= 90))
then
echo "成绩优秀"
elif (($score >=80))
then
echo "成绩良好"
elif (($score>=60))
then
echo "成绩及格"
else
echo "补考"
fi
[root@server ~]# bash choice6.sh
请输入百分制成绩:
未输入,请重新输入
[root@server ~]# bash choice6.sh
请输入百分制成绩:100
成绩优秀
[root@server ~]# 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:
[root@server ~]# grep "vendor_id" /proc/cpuinfo
vendor_id : GenuineIntel
vendor_id : GenuineIntel
[root@server ~]# vim choice7.sh
#!/bin/bash
vendor=$(grep "vendor_id" /proc/cpuinfo | uniq | cut -d " " -f2)
if [ $vendor == GenuineIntel ]
then
echo "Inter"
elif [ $vendor == GenuineAMD ] # AuthenticAMD
then
echo "AMD"
else
echo "unknow"
fi
[root@server ~]# bash choice7.sh
Inter
[root@server ~]# vim choice9.sh
#!/bin/bash
read -p "请输入字母、数字、其它字符: " str
if echo $str | grep [a-zA-Z] > /dev/null
then
echo "字母"
elif echo $str | grep [0-9] > /dev/null
then
echo "数字"
else
echo "字符"
fi
[root@server ~]# bash choice9.sh
请输入字母,数字,其他字符:890
数字
[root@server ~]# bash choice9.sh
请输入字母,数字,其他字符:adg
字母
[root@server ~]# bash choice9.sh
请输入字母,数字,其他字符:!@#
字符
case 变量名 in
值1)
指令1
;;
值2)
指令2
;;
值3)
指令3
;;
*)
默认
esac #结束case语句
[root@server ~]# vim choice10.sh
#!/bin/bash
read -p "请输入百分制成绩: " score
case $score in
9[0-9]|100)
echo "成绩优秀"
;;
8[0-9])
echo "成绩良好"
;;
6[0-9]|7[0-9])
echo "成绩及格"
;;
*)
echo "补考"
esac
[root@server ~]# bash choice10.sh
请输入百分制成绩:99
成绩优秀
[root@server ~]# bash choice10.sh
请输入百分制成绩:9
补考