#!/bin/bashwhile [ x$username = "x" ]; doread -p “Please enter the username you wish to create : ” usernameif id -u $username >/dev/null 2>&1; thenecho “User already exists”username=””fidonewhile [ x$group = "x" ]; doread -p “Please enter the primary group. If group not exist, it will be created : ” groupif id -g $group >/dev/null 2>&1; thenecho “Group exist”elsegroupadd $groupfidoneread -p “Please enter bash [/bin/bash] : ” bashif [ x"$bash" = "x" ]; thenbash=”/bin/bash”firead -p “Please enter homedir [/home/$username] : ” homedirif [ x"$homedir" = "x" ]; thenhomedir=”/home/$username”firead -p “Please confirm [y/n]” confirmif [ "$confirm" = "y" ]; thenuseradd -g $group -s $bash -d $homedir -m $usernamefi
Sample Result
sudo ./linux_user.shPlease enter the username you wish to create : testPlease enter the primary group. If group not exist, it will be created : testPlease enter bash [/bin/bash] :Please enter homedir [/home/test] :Please confirm [y/n]y22:12:58 [test@Desktop] :~ id testuid=1003(test) gid=1003(test) groups=1003(test)
Learn Above Script Line by Line
#Write to console ask to enter group and save input to group variable
read -p “Please enter the primary group. If group not exist, it will be created : ” group
#check if group already exist
if id -g $group >/dev/null 2>&1; then
#just warn that group already exist
echo “Group exist”
else
#if group not exist – create one more
groupadd $group
fi
#end of while loop
done
#ask to enter preferred bash
read -p “Please enter bash [/bin/bash] : ” bash
#check if no input
if [ x"$bash" = "x" ]; then
#if no input, use default bash
bash=”/bin/bash”
fi
#ask to enter preferred homedir
read -p “Please enter homedir [/home/$username] : ” homedir
#check if no input
if [ x"$homedir" = "x" ]; then
#if no input , use default homedir
homedir=”/home/$username”
fi
#ask to confirm all inputs
read -p “Please confirm [y/n]” confirm
#if input y
if [ "$confirm" = "y" ]; then
#command to add user with all entered info
useradd -g $group -s $bash -d $homedir -m $username
fi
No comments:
Post a Comment