I am basically trying to write a simple script to allow for interactive use of "useradd" with select options.
The options I am trying to allow for specific use are to either be defined interactively or set to the next available or default settings.
The main problem I am having is how to get the next available values to occur for value like group id and groups.
I imagine I can set preset values for the home directory and shell but I wanted to find out if there is a not so brute force method other than making constants for these variables.
Here is where I have made it to so far:
#!/bin/bash # Script to add a user to Linux system if [ $(id -u) -eq 0 ]; then read -p "Enter username : " username read -p "Enter password : " password read -p "Enter group id or [default to next available]: " gid read -p "Enter additional groups or [default to next available]: " group read -p "Enter home directory or [default to next available]: " hdir read -p "Enter desired shell or [default to next available]: " difshell egrep "^$username" /etc/passwd >/dev/null if [ $? -eq 0 ]; then echo "$username exists!" exit 1 else pass=$(perl -e 'print crypt($ARGV, "password")' $password) useradd -p$pass -g$gid -G$group -d$hdir -s$difshell $username [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" fi else echo "Only root may add a user to the system" exit 2 fi