程序使用bash脚本编写的,内部内置了一些日志,全部采用echo
命令实现。
test1 test2
此外usernames.txt这个文件名可以在脚本最上方的config中进行修改.
bash scriptName.sh -create
就可以创建在usernames.txt中存储的账号。bash scriptName.sh -delete
就可以删除在usernames.txt中存储的账号.其中-create
和-delete
可以在脚本最上方的config中进行修改.
# config userNameFile="usernames.txt" logStorageFile="log.txt" create="-create" delete="-delete" # function code addUser() { userName=$1 if id -u $userName >/dev/null 2>&1; then data="Attempted to add an existing user: $userName" echo $data echo $data >> $logStorageFile else password=$(openssl rand -base64 6) useradd -m $userName echo "$userName:$password" | chpasswd echo "added a new user: $userName" data="userName: $userName password: $password" echo $data >> $logStorageFile fi } deleteUser() { userName=$1 if id -u $userName >/dev/null 2>&1; then echo "deleting $userName" userdel -r $userName data="deleted a user: $userName" echo $data echo $data >> $logStorageFile else echo "user $userName does not exist!" data="Attempted to delete a user that did not exist: $userName" echo $data >> $logStorageFile fi } help() { echo " 1. Check whether file $userNameFile is correctly configured. Example: testUser1 testUser2 2. Check that the options are correctly selected. Append '-create' behind the script name to add the user stored in file $userNameFile; Append '-delete' behind the script name to delete the user stored in file $userNameFile. " } # main code if [ ! -f $userNameFile ]; then help exit 1 fi userNames=$(cat $userNameFile) op="${1}" echo $(date) >> $logStorageFile if [[ $op == $create || $op == $delete ]] then echo "operate: $op" >> $logStorageFile for userName in $userNames do if [[ $op == $create ]] then addUser $userName else deleteUser $userName fi done else echo $op help fi
# config userNameFile="usernames.txt" logStorageFile="log.txt" create="-create" delete="-delete" # function code addUser() { userName=$1 if id -u $userName >/dev/null 2>&1; then data="Attempted to add an existing user: $userName" echo $data echo $data >> $logStorageFile else password=$(openssl rand -base64 6) useradd -m $userName echo "$userName:$password" | chpasswd echo "added a new user: $userName" data="userName: $userName password: $password" echo $data >> $logStorageFile fi } deleteUser() { userName=$1 if id -u $userName >/dev/null 2>&1; then echo "deleting $userName" userdel -r $userName data="deleted a user: $userName" echo $data echo $data >> $logStorageFile else echo "user $userName does not exist!" data="Attempted to delete a user that did not exist: $userName" echo $data >> $logStorageFile fi } help() { echo " 1. Check whether file $userNameFile is correctly configured. Example: testUser1 testUser2 2. Check that the options are correctly selected. Append '-create' behind the script name to add the user stored in file $userNameFile; Append '-delete' behind the script name to delete the user stored in file $userNameFile. " } # main code if [ ! -f $userNameFile ]; then help exit 1 fi userNames=$(cat $userNameFile) op="${1}" echo $(date) >> $logStorageFile if [[ $op == $create || $op == $delete ]] then echo "operate: $op" >> $logStorageFile for userName in $userNames do if [[ $op == $create ]] then addUser $userName else deleteUser $userName fi done else echo $op help fi