網絡技術日誌
關於 PHP, Linux, Open Source 及個人生活記載的網誌。
-
編寫只給特定用戶執行的 shell script
Posted on March 17th, 2009 No comments在編寫了一些 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
Leave a reply
