Cấu trúc Case - shell linux
Cấu trúc case in esac, là cấu trúc rẽ nhánh trong lập trình shell trên Linux. Xem ví dụ sau sẽ rõ hơn:
[code]
#!/bin/bash
while :
do
clear
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -e "\t\t| Main Menu |"
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -e "\t\t| [1] Xem ngay/gio hom nay |"
echo -e "\t\t| [2] Xem lich |"
echo -e "\t\t| [3] Xem tat ca file/folder |"
echo -e "\t\t| [4] Exit/Stop |"
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -n "Enter your choice [1-4]: "
read choice
case $choice in
1) echo "Today is `date` "; read
;;
2) echo "Files in $PWD"; ls -l; read
;;
3) cal ; read
;;
4) exit 0
;;
*) echo "Please choice 1,2,3,4."; read
;;
esac
done
[/code]
[code]
#!/bin/bash
while :
do
clear
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -e "\t\t| Main Menu |"
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -e "\t\t| [1] Xem ngay/gio hom nay |"
echo -e "\t\t| [2] Xem lich |"
echo -e "\t\t| [3] Xem tat ca file/folder |"
echo -e "\t\t| [4] Exit/Stop |"
echo -e "\t\t+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+"
echo -n "Enter your choice [1-4]: "
read choice
case $choice in
1) echo "Today is `date` "; read
;;
2) echo "Files in $PWD"; ls -l; read
;;
3) cal ; read
;;
4) exit 0
;;
*) echo "Please choice 1,2,3,4."; read
;;
esac
done
[/code]
Nhận xét
Đăng nhận xét