shell脚本 while循环 if条件 case语句学习

条件域名的学习,以及实战中的的应用;

1、cat <

2、if else条件语句&while 循环的应用;

3、case in语句的用法;

4、echo -e 特殊颜色的用法;

#/bin/bash
#
#20.1.10.16:00

DELAY=3

while [ "$DELAY" != "0" ];do       #while条件语句
clear

cat << EOF                        #eof打印特殊功能
-----------------------
Pls choice menu:
    1:Show information for disk.
    2:Show disk service condition.
    3:Show users home used.
    0:Save and exit.
EOF

read -p "Pls choice your answer:" CHOICE

    if [[ $CHOICE =~ ^[0-3]$ ]];then      #这个是重点,需要定义菜单选项,遇到范围类的可以使用“=~”==“约等于”
    									#if else 条件
        case $CHOICE in                  #case语句的用法参考
            1)
                echo -e -n "\033[31m${HOSTNAME} disk information\033[0m:"
                echo `uptime`
                sleep $DELAY;;
            2)
                echo -e "\033[31mShow disk used:\033[0m"
                df -Th
                sleep $DELAY;;
            3)
                echo -e "\033[31mUsers home used:\033[0m"
                if [[ $(id -u) -eq 0 ]];then
                    echo "Root home used:"
                    echo `du -sh /root`
                else
                    echo "`id |awk '{print $2}' |cut -d"(" -f2 |cut -d")" -f1` home used:"
                    echo `du -sh /home/${HOSTNAME}/`
                fi
                sleep $DELAY
                ;;
            0)
                echo -e "\033[32mSave and Exit.\033[0m"
                exit 0;;
            esac
        else
            echo -e "\033[1;41mEnter Error.\033[0m"         #echo 特殊颜色的用法
        sleep 1
    fi
done

 

你可能感兴趣的:(shell)