单分支if语句
if 判断条件;then statement1 statement2 fi
双分支if语句
if 判断条件;then statement1 statement2 …… else statement3 statement4 …… fi
多分支if语句
if 判断条件1;then statement1 statement2 …… elif 判断条件2;then statement3 statement4 …… elif 判断条件3;then statement5 statement6 …… else statement7 statement8 …… fi
示例1:
[[email protected] Scripts]# cat ./root.sh #!/bin/bash read -p "Please input an Username in /etc/passwd file: " Local_USER Local_ID=$(id -u $Local_USER) if [ "$Local_ID" -eq 0 ];then echo "The User $Local_USER is admin" else echo "The User $Local_USER is common user." fi [[email protected] Scripts]# ./root.sh Please input an Username in /etc/passwd file: frame The User frame is common user. [[email protected] Scripts]# ./root.sh Please input an Username in /etc/passwd file: root The User root is admin [[email protected] Scripts]#
shell编程(四)--- 条件判断之if判断
时间: 2024-10-12 17:33:07