在編寫了一些 shell script 並打算只給予特定用戶執行,可以先取得用戶的 user id,或者直接用 whoami 指令檢查使用者名稱,例如:

if [ $(whoami) = "samtang" ]
then
   # 執行程式碼
else
   echo "You cannot run this script."
   exit 0
fi

如果在另一個情況下,不容許特定用戶執行,可以這樣寫:

if [ $(whoami) = "root" ]
then
   echo "You cannot run this script."
   exit 1
fi