shell腳本 while循環 if條件 case語句學習

條件域名的學習,以及實戰中的的應用;

1、cat <<EOF 特殊打印功能;

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

 

發佈了24 篇原創文章 · 獲贊 10 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章