case 語法結構(字串比較)
case 變數 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
無匹配后命令序列
esac
簡單的模式匹配
邀請用戶輸入待洗掉用戶名,
if寫法:
詢問用戶,確定要繼續洗掉嗎 yes/no: " y
#!/bin/bash
#1請輸入洗掉的用戶名:
read -p "please input a username : " user
#2輸出用戶ID
id $user &> /dev/null
#4判斷用戶是否存在
if [ $? -ne 0 ];then
echo "no such user: $user"
exit 1
fi
#3請用戶確認是否洗掉
read -p "are you sure?[y/n]: " action
if [ "$action" = "y" -o "$action" = "Y" ] ;then
userdel -r $user
echo "$user is deleted!"
else
echo "thank you"
fi
case寫法
#!/bin/bash
read -p "請輸入選擇" action
case "$action" in
Y|y|YES|yes)
userdel -r $user
echo "$user is deleted!"
;;
*)
echo "thank you"
;;
esac
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260530.html
標籤:其他
上一篇:點在矩形內編程問題
