Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:15:09+00:00 2026-06-13T18:15:09+00:00

I have the script listed below that I can’t seem to get the issue

  • 0

I have the script listed below that I can’t seem to get the issue worked out of. I’m trying to make an interactive login script for a UNIX class that I’m in. I’m basically building out a command to pass into useradd. The command that I make when passed into the command line (while adding sudo) works as expected, but when I try to run it from my script (which is generating the text that I copy/paste into the command line) it gives me some errors… At this point I’m at a loss for what to try next to resolve the issue.

ERROR:

useradd -m --user-group jaredm2 #command that is attempting to run...
useradd: invalid option -- ' '
Usage: useradd [options] LOGIN
....rest of useradd error text....

SCRIPT:

#!/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 --user-group"
else
    if [ $(grep -c "^${newPrimaryGroup}" /etc/group) -ge 1 ]; then
        FINALCOMMAND="$FINALCOMMAND -g $newPrimaryGroup"
    else
        echo "Invalid group specified reverting to default!"
        FINALCOMMAND="$FINALCOMMAND --user-group"
    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
`useradd $FINALCOMMAND`

`passwd $USERNAME`

`chfn $USERNAME`

UPDATE: ADDED DEBUG CONTENT

+ '[' 0 -ge 1 ']'
++ sed 's/^ *//g'
++ sed 's/ *$//g'
++ echo '/usr/sbin/useradd -m -U JaredM'
+ FINALCOMMAND='/usr/sbin/useradd -m -U JaredM'
++ '/usr/sbin/useradd -m -U JaredM'
./addnewuser.sh: line 89: /usr/sbin/useradd -m -U JaredM: No such file or directory
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:15:11+00:00Added an answer on June 13, 2026 at 6:15 pm

    By using arrays you could prevent IFS related errors in the command-line and it’s a lot cleaner. This one’s already tested. I made some clean-ups to the code as well.

    #!/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 -n "Enter a new Primary Group or leave blank for the default and press [ENTER]: "
    read newPrimaryGroup
    
    if [ ${#newPrimaryGroup} -eq 0 ]; then
        FINALCOMMAND=("${FINALCOMMAND[@]}" "--user-group")
    else
        if [ $(grep -c "^${newPrimaryGroup}" /etc/group) -ge 1 ]; then
            FINALCOMMAND=("${FINALCOMMAND[@]}" "-g" "$newPrimaryGroup")
        else
            echo "Invalid group specified reverting to default!"
            FINALCOMMAND=("${FINALCOMMAND[@]}" "--user-group")
        fi
    fi
    
    #ASK about additional groups
    echo -n "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=, read -a TEMP <<< "$extraGroups"
    for g in "${TEMP[@]}"; 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
    
    if [ ${#FINALEXTRAGROUPS[@]} -ge 1 ]; then
        FINALCOMMAND=("${FINALCOMMAND[@]}" "-G" "${FINALEXTRAGROUPS:1}")
    fi
    
    
    #ASK about the home directory
    echo -n "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=("${FINALCOMMAND[@]}" "$USERNAME")
    
    #PASSCOMMAND="sudo passwd $USERNAME"
    
    #ADD THE USER
    echo "useradd ${FINALCOMMAND[@]}"
    
    useradd "${FINALCOMMAND[@]}"
    
    passwd "$USERNAME"
    
    chfn "$USERNAME"
    

    Note: In newer versions of bash you could just use += to append a value to an array e.g. ARRAY+=(“value”)

    Also by my additional preferences I would improve the code further this way, but that’s not the best of it yet:

    #!/bin/bash
    
    shopt -s extglob
    
    # Check if running as root.
    if [[ "$(id -u)" != 0 ]]; then
        echo "This script must be ran as root."
        exit 1
    fi
    
    # Initialize useradd command variable.
    USERADDCOMMAND=("useradd" "-m")
    
    # Import useradd defaults.
    . /etc/default/useradd
    
    # Get the new user's 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."
    while :; do
        read -p "Please enter the user's requested username and press [ENTER]: " USERNAME
        [[ ${#USERNAME} -ge 4 && ${#USERNAME} -le 20 && $USERNAME == +([[:alpha:]])*([[:alnum:]_-]) ]] || {
            echo "Error: Username is invalid. Please enter a different username."
            continue
        }
        [[ $(grep -c "^${USERNAME}:" /etc/passwd) -ge 1 ]] && {
            echo "Error: Username is in use. Please enter a different username."
            continue
        }
        break
    done
    
    # Ask about the default shell.
    read -p "Enter the new shell if you would like one (currently $SHELL) or leave blank for the default and press [ENTER]: " SHELL_
    if [[ ${#SHELL_} -ge 1 ]]; then
        USERADDCOMMAND=("${USERADDCOMMAND[@]}" "-s" "$SHELL_")
    else
        # We use this if we really are to use SHELL specified in $SHELL but it still needs further workarounds like checking if $SHELL is valid. Those could be easily done but it depends if this one's really necessary.
        #USERADDCOMMAND=("${USERADDCOMMAND[@]}" "-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 -n "Enter a new Primary Group or leave blank for the default and press [ENTER]: "
    read NEWPRIMARYGROUP
    
    if [[ ${#NEWPRIMARYGROUP} -eq 0 ]]; then
        USERADDCOMMAND=("${USERADDCOMMAND[@]}" "--user-group")
    else
        if [[ $(grep -c "^${NEWPRIMARYGROUP}" /etc/group) -ge 1 ]]; then
            USERADDCOMMAND=("${USERADDCOMMAND[@]}" "-g" "$NEWPRIMARYGROUP")
        else
            echo "Invalid group specified reverting to default!"
            USERADDCOMMAND=("${USERADDCOMMAND[@]}" "--user-group")
        fi
    fi
    
    # Ask about additional groups.
    echo -n "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=, read -a TEMP <<< "$EXTRAGROUPS"
    for G in "${TEMP[@]}"; do
        if [[ $(grep -c "^${g}" /etc/group) -ge 1 ]]; then
            FINALEXTRAGROUPS="$FINALEXTRAGROUPS,$G"
        else
            echo "$G is an invalid user and will not be added."
        fi
    done
    if [[ ${#FINALEXTRAGROUPS[@]} -ge 1 ]]; then
        USERADDCOMMAND=("${USERADDCOMMAND[@]}" "-G" "${FINALEXTRAGROUPS:1}")
    fi
    
    # Ask about the home directory
    read -p "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]: " NEWHOME
    if [[ ${#NEWHOME} -ge 1 ]]; then
        USERADDCOMMAND=("${USERADDCOMMAND[@]}" "-d" "$NEWHOME")
    fi
    
    # Add the username to the command
    USERADDCOMMAND=("${USERADDCOMMAND[@]}" "$USERNAME")
    
    # Add THE USER
    echo "> ${USERADDCOMMAND[*]}"
    "${USERADDCOMMAND[@]}"
    
    echo "> passwd $USERNAME"
    passwd "$USERNAME"
    
    echo "> chfn $USERNAME"
    chfn "$USERNAME"  # -f "$NAME"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have programmed a proxy.php script (listed below), which would fetch an image specified
I have the following snippet below from my script that's using a WebRequest to
I have a unix shell script which test ftp ports of multiple hosts listed
I have script that reads remote file content and writes it to local server.
I have script which allows to display Bing search results. I can call for
I need to have script.sh , that would create files f1.txt and f2.txt with
I have this script that run to fix my menu bar to the browser
So at the moment I'm trying to delete files listed in the directory that
I have a script that reads files from a folder and catalogs the contents
I have this script that displays a max of 5 images for each row,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.