瀏覽單個文章
ansible
Basic Member
 

加入日期: Jan 2021
文章: 23
用shell寫一個script來建帳戶

大家好,學校作業要求製作一個建user帳號的shell script,東拼西湊的,我寫得很心虛,希望大家幫我看看如何改進,謝謝

(建組名,建帳戶名,設密碼,提供改user id 與 group id的機會)

#!/bin/bash

if [ `whoami` = root ]; then
echo "Please provide a group name:"
else
echo "Permission denied."
fi

read g
groupadd $g
echo "Group name '$g' has been created successfully."

echo "Please provide a user name:"
read u
useradd $u
echo "User account '$u' has been created successfully."

read -p "Please enter the password for '$u' : " PASSWD
echo "$PASSWD" | passwd --stdin $u &> /dev/null

echo "Do you want to change group ID for '$g' (y/n)?"
read gyn
echo
if [ $gyn == y ]
then
echo "Please enter the group ID for '$g':"
read gid
groupmod $g -g $gid
echo
echo "The group ID $gid is with the group '$g' now."
elif [ $gyn == n ]
then
echo "Ok, nothing to change"
fi

echo "Do you want to change user ID for '$u' (y/n)?"
read uyn
echo
if [ $uyn == y ]
then
echo "Please enter the user ID for '$u':"
read uid
usermod $u -u $uid
echo
echo "The user ID $uid is with the user '$u' now."
elif [ $uyn == n ]
then
echo "Ok, nothing to change"
fi
     
      
舊 2021-09-18, 11:03 PM #1
回應時引用此文章
ansible現在在線上