常见的if
条件语句分为三种:单分支、双分支、多分支;
# if与then分隔
if <条件表达式>
then #当条件满足表达式时,执行指令
指令
fi
# if与then相连
if <条件表达式>;then
指令
fi
[root@localhost shell]# cat freeMem.sh
#!/bin/bash
freeMem=`free -m | grep -w Mem | tr -s " " | cut -d " " -f 4`
#反引号内容为执行指令,执行结果赋值给新对象freeMem
if [ $freeMem -le 100 ]
then
echo "内存已满,请及时清理!"
#当freeMem小于100时,执行echo语句
else
echo "内存充足!"
#当freeMem大于或等于100时,执行echo语句
fi
#提取内存大小其他简便方法(推荐使用sed与awk命令为shell编程语句提取数值)
[root@localhost ~]# free -m | awk '/Mem:/ {print $4}'
1614
[root@localhost shell]# cat a3.sh
#!/bin/bash
if [ $USER == "root" ];then
echo "is root!"
else
echo "not is root!"
fi
if <条件表达式>
then #当条件满足表达式时,执行指令1
指令1
else #当条件不满足表达式时,执行指令2
指令2
fi
[root@localhost shell]# cat a6.sh
#!/bin/bash
vendor=`cat /proc/cpuinfo | grep vendor_id | uniq | tr -s " " | cut -d " " -f 2`
#uniq 去除重复出现的行
#tr -s 字符串去重,只保留第一个重复字符
if [[ $vendor=="Genuinelnmtel" ]];then
echo "is intel!"
elif [[ $vendor=="AuthenticAMD" ]];then
echo "is AMD!"
else
echo "is other!"
fi
if <条件表达式1>
then #当条件满足表达式1时,执行指令1
指令1
elif <条件表达式2>
then #当条件满足表达式2时,执行指令2
指令2
else #当条件不满足表达式1与2时,执行指令3
指令3
fi
[root@localhost shell]# cat test.sh
#!/bin/bash
read -p "Please enter your score(0-100):" grade
#输入一个数值,范围区间为0-100之内,将其赋值给定义新对象grade
if [ $grade -ge 85 ]
then
echo "$grade,优"
#若grade的值大于85,则返回优
elif [ $grade -ge 70 ]
then
echo "$grade,良"
#若grade的值介于85和70之间,则返回良
elif [ $grade -ge 60 ]
then
echo "$grade,中"
#若grade的值介于70和60之间,则返回中
else
echo "$grade,差"
#其他情况,返回差
fi
复合指令:一串命令的集合,( )
和{ }
都是对一串命令进行执行;
( )
和 { }
都是把一串命令放在括号内,用;
间隔;
( )
和 { }
某个命令的重定向只影响该命令,但是括号外的重定向则影响括号李的所有命令;
( )
只是对一串命令重新开一个子shell进行执行,{ }
对一串命令在当前shell执行;
( )
最后一个命令可以不用 ;
,{ }
最后一个命令要用;
结束;
( )
第一个命令和左边括号不需要有空格,{ }
第一个命令必须和左边括号有空格;
exit
语句的基本作用是终止shell程序的执行,此外exit
可以附带一个可选参数,用来指定程序退出时的状态码;
exit [status]
其中,status
表示退出状态,该参数是一个整数值,取值范围为0-255
,;
[root@localhost shell]# cat vendor.sh
#!/bin/bash
vendor=`cat /proc/cpuinfo | grep vendor_id | uniq | tr -s " " | cut -d " " -f 2`
if [[ $vendor=="Genuinelnmtel" ]];then
echo "is intel!"
elif [[ $vendor=="AuthenticAMD" ]];then
echo "is AMD!"
else
echo "is other!"
fi
exit 6
#指定退出状态为6,若不为6代表脚本执行失败,以此可以判断脚本执行错误位置
[root@localhost shell]# echo $?
6
case 变量名 in
值1)
指令1
#当变量选择值1时,执行指令1
;;
值2)
指令2
#当变量选择值2时,执行指令2
;;
值3)
指令3
#当变量选择值3时,执行指令3
;;
*)
默认指令
#当变量选择值为其他时,执行默认指令
esac
[root@localhost shell]# cat ping_ip.sh
#!/bin/bash
read -p "Please enter youer ip.address(1-2):" ip_addr
#输入一个数值,范围区间为1-2之内,将其赋值给定义新对象ip_addr
ping -c 2 10.81.20.$ip_addr &> /dev/null
#将变量ip_addr的值调用,并执行命令,结果指空(指空:当前终端无任何命令执行结果的回显信息)
if [ $? -eq 0 ];then
echo "is ok!"
#若命令执行成功,则代表可以ping通
else
echo "is down!"
#若命令执行失败,则代表不能ping通
fi
[root@localhost shell]# cat username.sh
#!/bin/bash
read -p "Please enter your username:" username
grep -w $username /etc/passwd &> /dev/null
if [ $? -eq 0 ];then
echo "$username is live!"
else
echo "$username is none!"
fi
[root@localhost shell]# cat kernel.sh
#!/bin/bash
aaa=`uname -r | cut -d "." -f 1`
bbb=`uname -r | cut -d "." -f 2`
if [ $aaa -eq 4 ];then
echo "主版本为4"
else
echo "主版本不为4"
fi
if [ $bbb -ge 10 ];then
echo "次版本大于10"
else
echo "次版本小于10"
fi
yum
源支持)[root@localhost shell]# cat service.sh
#!/bin/bash
read -p "Please enter your check service:" Service
Service_num=`rpm -qa $Service | wc -l`
if [ $Service_num -eq 0 ];then
echo "This Service is not install" && yum install -y $Service
exit 2
else
echo "This Service is install" && rpm -qa $Service | cut -d "-" -f 2
exit 3
fi
[root@localhost shell]# cat service_2.sh
#!/bin/bash
read -p "Please enter your check service:" Service
while true
do
Service_num=`ps -ef | grep $Service | wc -l`
if [ $Service_num -gt 1 ];then
echo "The service is running " &> /dev/null
else
echo "The service is not running" && systemctl start #$Service
break
fi
done
[root@localhost shell]# cat a1.sh
#!/bin/bash
ping -c 1 $1 &> /dev/null
if [ $? -eq 0 ];then
echo "this ip is ok!"
else
echo "this ip is not ok!"
fi
[root@localhost shell]# sh a1.sh 192.168.1.1
this ip is ok!
[root@localhost shell]# cat input_num.sh
#!/bin/bash
while true
do
read -p "Please enter your info:" info_file
case "$info_file" in
[0-9])
echo "is number!" && break
;;
*)
echo "is not number"
esac
done
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- zrrp.cn 版权所有 赣ICP备2024042808号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务