關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 編寫只給特定用戶執行的 shell script

    Posted on March 17th, 2009 Sam Tang 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

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word