Register
It is currently Sat Apr 19, 2014 3:25 am

Error in making an interactive script for useradd


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Oct 25, 2012 10:57 am   

Joined: Thu Oct 25, 2012 10:44 am
Posts: 1
I have an assignment for a UNIX class that I'm in to make a script that will make the useradd command interactive. I have the script generating my expected command and the command works when I copy and paste it into the command line. However, when I try to run the command from the script it gives me an error...

ERROR:
Code:
useradd -m -U jaredm4 #An echo of the command that was issued
/usr/sbin/useradd: invalid option -- ' ' #The ERROR
Usage: useradd [options] LOGIN
...rest of usage text...



SCRIPT:
Code:
#!/bin/bash
#Add a new user

FINALCOMMAND="-m"

#import useradd defaults...
. /etc/default/useradd

#Check if running as root
if [ "$(id -u)" != "0" ]; then
   echo "This script must be ran as root"
   exit 1
fi

#Get the new users Name
#echo -n "Please enter the users First and Last Name and press [ENTER]: "
#read Name

#Get the new users username
echo "The username must be 4-20 characters."
echo -n "Please enter the users requested username and press [ENTER]: "
read USERNAME
while [ $(grep -c "^${USERNAME}:" /etc/passwd) -ge 1 ] || [ ${#USERNAME} -le 3 ] || [ ${#USERNAME} -ge 21 ]
do
   echo " "
   echo "Error: Username is in use or invalid.  Please select a different username."
   echo " "
   echo -n "Please enter the users requested username and press [ENTER]: "
   read USERNAME   
done #USERNAME will be valid from this point

#ASK about the default shell now
echo -n "Enter the new shell if you would like one (currently $SHELL) or leave blank for the default and press [ENTER]: "
read tempSHELL
if [ ${#tempSHELL} -ge 1 ]; then
   SHELL="$tempSHELL"
   FINALCOMMAND="$FINALCOMMAND ""-s $SHELL"
fi

#ASK about a different primary group
echo "Would you like to enter a non-default primary user group? Defaults to creating a new group that matches the username"
echo "Enter a new Primary Group or leave blank for the default and press [ENTER]: "
read newPrimaryGroup

if [ ${#newPrimaryGroup} -eq 0 ]; then
   FINALCOMMAND="$FINALCOMMAND -U"
else
   if [ $(grep -c "^${newPrimaryGroup}" /etc/group) -ge 1 ]; then
      FINALCOMMAND="$FINALCOMMAND -g $newPrimaryGroup"
   else
      echo "Invalid group specified reverting to default!"
      FINALCOMMAND="$FINALCOMMAND -U"
   fi
fi
#useradd -m --user-group jaredm2
#ASK about additional groups
echo "Would you like the new user to be a part of any additional groups?  Leave blank if no additional groups are needed or enter additional groups in the format of GROUP1,GROUP2,... (NO SPACES) and press [ENTER]: "
read extraGroups
#remove spaces if the user entered any
extraGroups="${extraGroups//[[:space:]]}"
FINALEXTRAGROUPS=""
IFS=","
for g in $extraGroups
do
   if [ $(grep -c "^${g}" /etc/group) -ge 1 ]; then
      FINALEXTRAGROUPS="$FINALEXTRAGROUPS,$g"
   else
      echo "$g is invalid user will not be added..."
   fi
done
FINALEXTRAGROUPS=$(echo "$FINALEXTRAGROUPS" | tail -c +2)
if [ ${#FINALEXTRAGROUPS} -ge 1 ]; then
   FINALCOMMAND="$FINALCOMMAND -G $FINALEXTRAGROUPS"
fi

#ASK about the home directory
echo "Would you like to enter a new home directory for the user?  Leave blank to use the default of $HOME/$USERNAME or enter your own and press [ENTER]: "
read NEWHOME

if [ ${#NEWHOME} -ge 1 ]; then
   FINALCOMMAND="$FINALCOMMAND -d $NEWHOME"
fi

#ADD the username to the command
FINALCOMMAND=`echo "$FINALCOMMAND $USERNAME" | sed 's/ *$//g' | sed 's/^ *//g'`
echo "useradd $FINALCOMMAND"
#PASSCOMMAND="sudo passwd $USERNAME"
#ADD THE USER
`/usr/sbin/useradd $FINALCOMMAND`

`passwd $USERNAME`

`chfn $USERNAME`


Things I have tried....I'm sure I'll miss some
    I have taken the command that is generated and pasted it to the console and it works perfectly
    I have issued the full path to the useradd to make sure the correct command was being called
    I have done an echo on $PATH from inside the script to make sure that was valid
    beyond that I'm at a loss


Top
 Profile  
 PostPosted: Thu Oct 25, 2012 11:06 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

why are the last three commands inside subshells?

it seems the error message doesn't reflect what's in the script.


Top
 Profile  
 PostPosted: Sun Oct 28, 2012 4:32 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 46
#!/bin/bash
set -x
...


Top
 Profile WWW  
 PostPosted: Wed Nov 06, 2013 3:41 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
The main issue is that the variable FINALCOMMAND should be an array, not a string,
otherwise useradd(1) will only see a string as its arguments instead of multiple tokens.

...and it shouldn't be written in uppercase letters because it's not an internal environment variable.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP