Last active
August 5, 2022 08:44
-
-
Save Benjamin1021523/1a5daa903c95b18dc5514122fd1cb7ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function checkRoot { | |
if [ "$EUID" -ne 0 ] | |
then echo 請使用root帳號,或sudo指令執行 | |
return 1 | |
fi | |
return 0 | |
} | |
checkRoot || exit 1 | |
read -p "請輸入欲建立的使用者名稱: " USER_NAME | |
# 確認帳號可建立 | |
grep -i "^$USER_NAME:" /etc/passwd > /dev/null 2>&1; | |
if [ $? -eq 0 ]; then | |
echo "帳號已存在" | |
exit 1 | |
fi | |
read -p "是否要設為sudo使用者? (y/n)" BE_SUDOER | |
WHEEL= | |
if [ $BE_SUDOER == 'y' ] | |
then | |
WHEEL='-G wheel' | |
fi | |
useradd -s /bin/bash $WHEEL $USER_NAME && echo "建立使用者成功" | |
read -p -s "請輸入使用者密碼: " PASSWD | |
echo "$PASSWD" | passwd --stdin $USER_NAME | |
if [ $BE_SUDOER == 'y' ] | |
then | |
echo "測試sudo權限" | |
TEST_FILE=test.`date "+%Y-%m-%d_%H:%M:%S"` | |
su $USER_NAME -c "echo \"$PASSWD\" | sudo -S touch $TEST_FILE" > /dev/null | |
ls $TEST_FILE > /dev/null 2>&1 && echo "測試成功" || echo "測試失敗" | |
rm $TEST_FILE 2> /dev/null | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment