0

Where could I include man useradd and -g and G i'm not sure where to include these commands above. The current shell script doesn't successly create the user but does run.

#!/usr/bin/env bash

anotherUser() {
   read -p "Add another user? [y/n] yn"
   if [[ $yn = *[yY]* ]]; then
      checkUser
   fi
   exit
}
checkUser() {
while :
   do
      read -p "Enter user: " userName
      read -s -p "Enter password : " userPass
      if id "$userName" >/dev/null; then
         echo "User exists"
         anotherUser
      else
         echo adduser "$userName"
         printf "User %s has been added\n" "$userName"
         exit
      fi
   done
}
checkUser
  • Could someone help me please? – Programmer Mar 23 '16 at 23:56
  • 1
    You've asked for my advice...my advice is that scripts like this are unnecessary and just (badly) duplicate functionality already in the shell. Instead of writing a script that prompts for and takes user input, write a script that takes command line arguments, sanity-checks them, and then runs the appropriate useradd or adduser commands. if another user needs to be added, the script can be run again (using the shell's history editing features). Better yet, the script can be run from another script that calls it multiple times to add multiple users. – cas Mar 24 '16 at 0:14
  • 1
    scripts like this can be useful as a learning exercise, and are often written by people just past the early novice stage of systems administration, trying to automate or simplify tasks that only seem difficult because they haven't yet done them a thousand times. i'd suggest keeping a notebook and adding notes on your experiences with infrequently used (or new to you) commands like useradd. eventually you will remember all the command line options, or at least get into the habit of checking the man page for details you can't quite remember. – cas Mar 24 '16 at 0:18
0

You can use this:

#!/usr/bin/env bash

anotherUser() {
   read -p "Add another user? [y/n]" -n1 yn
   yn=`echo $yesno |tr '[:upper:]' '[:lower:]'`
   if [[ "$yn" = "y" ]]; then
      checkUser
   fi
   exit
}
checkUser() {
while :
   do
      read -p "Enter user: " userName
      read -s -p "Enter password : " userPass
      id -u "$userName" >/dev/null 2>&1
      if [ $? -eq 0 ] ; then
         echo "User exists"
         anotherUser
      else
         echo adduser "$userName"
         printf "User %s has been added\n" "$userName"
         exit
      fi
   done
}
checkUser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.