Combine two Bash while loop statements - bash

I am trying to combine 2 different logical statements in a single while loop, but having trouble getting the logic correct so that 2 different checks can be evaluated in the same loop. For example, I have the following 2 logical statements.
Logic 1
Determine if the entered username is blank and if it is ask the user to re-enter a different username.
echo -ne "User Name [uid]$blue:$reset "
read USERNAME
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
while [[ -z "$USERNAME" ]]; do
echo ""
printf "%s\n" "The User Name CAN NOT be blank"
echo ""
echo -ne "User Name [uid]$blue:$reset "
read USERNAME;
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
done
Logic 2
Determine if the read username already exists and if it does ask the user to re-enter a username.
echo -ne "User Name [uid]$blue:$reset "
read USERNAME
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
$(command -v getent) passwd "$USERNAME" &>/dev/null
while [[ $? -eq 0 ]]; do
echo ""
printf "%s\n" "$USERNAME exists in LDAP"
echo ""
echo -ne "User Name [uid]$blue:$reset "
read USERNAME;
USERNAME=$(echo "$USERNAME" | tr "[:upper:]" "[:lower:]")
$(command -v getent) passwd "$USERNAME" &>/dev/null
done
For achieving the described goal I have tried while loops and nested if statements and am just confused at this point. Basically as part of the script I would like these 2 logical statements to be combined when the user is asked to enter a username without the script exiting until a valid value is entered.

Don't use uppercase variable names!
#!/bin/bash
while true; do
echo -ne "User Name [uid]$blue:$reset "
read username
[ -z "$username" ] && echo -e "\nThe User Name CAN NOT be blank\n" && continue
username=$(tr [:upper:] [:lower:] <<< $username)
[ -z $(getent passwd "$username") ] && break || echo -e "\n$username exists in LDAP\n"
done

You could move the conditional check from the while statement to a pair of if statements. In this case, I've also moved the read and related commands to the top of the loop. This means you don't need extra read commands before the loop.
#!/bin/bash
while true; do
echo -ne "User Name [uid]$blue:$reset "
read username;
username=$(echo "$username" | tr "[:upper:]" "[:lower:]")
$(command -v getent) passwd "$username" &>/dev/null
if [[ -z "$username" ]]; then
echo ""
printf "%s\n" "The User Name CAN NOT be blank"
echo ""
continue #Skips the rest of the loop and starts again from the top.
fi
if [[ $? -eq 0 ]]; then
echo ""
printf "%s\n" "$username exists in LDAP"
echo ""
continue #Skips the rest of the loop and starts again from the top.
fi
#If execution reaches this point, both above checks have been passed
break #exit while loop, since we've got a valid username
done
As an aside, it's often recommended to avoid uppercase variable names, in order to avoid conflicts with system environment variables.

Related

My bash script verification code does not work

I am trying to write a program where the user can enter a username and password, then the code should check if the username and password are correct unfortunately whenever it checks no matter if the username/password is correct of not it will echo "Username Verified".
#!/bin/bash
echo "Username"
read username
echo "Password"
read password
sleep 1
correct_u=user
correct_p=pass
if [[ $username -eq $correct_u ]]
then
echo "Username Verified..."
else
echo "Username Incorect..."
fi
sleep 1
if [[ $correct_p -eq $password ]]
then
sleep 1
echo "Password Verified..."
else
echo "Password Incorect..."
fi
I have tired checking that all the variables work
Unless username and correct_u consist solely of digits, [[ $username -eq $correct_u ]] will always evaluate to true, since -eq forces the arguments to be numbers, and if there are no number, the arguments are treated as zero.
To do a string comparision, do
[[ $username == "$correct_u" ]]
Quoting the right-hand side is important here, to avoid that it is interpreted as glob-pattern, since == in general does a wildcard match.
You should use = instead of -eq when comparing strings in bash. = is used for string comparison while -eq is used for integer comparison:
#!/bin/bash
echo "Username"
read username
echo "Password"
read password
sleep 1
correct_u=user
correct_p=pass
if [[ "$username" = "$correct_u" ]]
then
echo "Username Verified..."
else
echo "Username Incorrect..."
fi
sleep 1
if [[ "$correct_p" = "$password" ]]
then
sleep 1
echo "Password Verified..."
else
echo "Password Incorrect..."
fi
Embedding your name & pw in cleartext in the file isn't ideal. The user ID must be able to read it to execute the commands in it; executable permissions won't help if you take away read.
Use that to your advantage. Set the user/group/world permissions appropriately. Then the user and password are entered at login...
You might want to combine methods from here and here, reading the right password securely from a vault file and comparing that to the one you read silently from the user.
But first, as already mentioned - fix your test.
$: [[ "one" -eq "two" ]] && echo same || echo no
same
$: [[ "one" == "two" ]] && echo same || echo no
no
$: [[ "one" == "one" ]] && echo same || echo no
same

how can I loop with an error message?

Latest Version
#!/bin/bash
set -e
shopt -s nocasematch
#vars
redbgbold='\e[1;97;41m'
resetcolor='\e[0m'
RegExFQDN='(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)'
#functions
ask() {
local input
until
read -rp "$1 > " input >&2 || return 1
grep -q -P "$2" <<< "$input"
do
printf "ERROR - "${redbgbold}"\"$input\""${resetcolor}" is not a valid " >&2; sed "s/.*the //" <<< "$1" >&2
done
printf '%s\n' "$input"
}
#code
while [ -z $fqdn ]; do
fqdn=$(ask "Enter the FQDN" $RegExFQDN)
echo "FQDN is $fqdn"
done
The Question
I have a read line, and I want to take what the user entered and see if it matches my regex, if it matches we leave the loop, if it fails it prints an error and we do the loop again until we get a match. It looks redundant to me, and I assume there should be a better way but not sure what that should be.
Original Code
#!/bin/bash
set -e
shopt -s nocasematch
function RegexValidation() {
if [ "$2" = "fqdn" ]; then
if [ `echo $1 | grep -c -P '(?=^.{1,254}$)(^(?>(?!\d+\.)[a-z0-9_\-]{1,63}\.?)+(?:[a-z]{2,})$)'` == "0" ]; then
echo "ERROR - $1 is not a valid FQDN"
unset $!{1}
fi
fi
}
while [ -z $fqdn ]; do
read -e -r -p "Enter the Fully Qualified Domain Name > " fqdn
RegexValidation $fqdn fqdn
done
shopt -u nocasematch
any help is appreciated.
Update #1 - fixed formatting issues.
Update #2 - using that other guy's suggestions with a few additional tweaks
I would do basically the same thing, but split it differently to make it easier to reuse:
#!/bin/bash
set -e
ask() {
local input
until
read -rp "$1 > " input >&2 || return 1
grep -q -P "$2" <<< "$input"
do
echo "Invalid answer. Try again" >&2
done
printf '%s\n' "$input"
}
ask_fqdn() {
ask "$1" '(?=^.{1,254}$)(^(?>(?!\d+\.)[a-z0-9_\-]{1,63}\.?)+(?:[a-z]{2,})$)'
}
fqdn=$(ask_fqdn "Enter first FQDN")
echo "You wrote $fqdn"
fqdn=$(ask_fqdn "Enter second FQDN")
echo "This time it was $fqdn"
number=$(ask "And now a number because why not" '^\d+$')
echo "So $number"
Now you don't have to write a new loop every time you want new information, and you can easily ask for new things without modifying the existing functions.
Have the function return a status, which you can test with if in the loop.
And rather than use test to check the result of grep, just test it directly with if. grep returns a non-zero status if the input doesn't match.
function RegexValidation() {
if [ "$2" = "fqdn" ]; then
if ! echo "$1" | grep -q -P '(?=^.{1,254}$)(^(?>(?!\d+\.)[a-z0-9_\-]{1,63}\.?)+(?:[a-z]{2,})$)'; then
echo "ERROR - $1 is not a valid FQDN"
return 1
fi
return 0
fi
}
while :; do
read -e -r -p "Enter the Fully Qualified Domain Name > " fqdn
if RegexValidation "$fqdn" fqdn
then break
fi
done
Also, remember to quote your variables.

How to add predefined users to a specific group in linux/bash script

I have been trying to implement a code that makes a predefined user created, be put into a specific groups (first 5 in MyMembers, next 5 in MyGroup, and last 5 to MyMinions), but I always got lost in coding it.
So far this is my code in creating predefined user.
#!/bin/bash
#This script adds a user with a hidden password to your #system.
ans=yes
while [[ "$ans" = yes ]] ;
do
if [ $(id -u) -eq 0 ];
then
read -p "Enter username: " username
read -s -p "Enter password: " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ];
then
echo "$username already exists!"
exit 1
else
pass=$(perl -e 'print crypt ($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo -e "\nUser has been added to your system!" || echo "\nFailed to add the user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
echo -e "\nDo you still want to add more users?. \nType yes to continue adding. \nType yes or any key to exit"
read ans
done
exit

Parsing text in BASH

I have a username system. The user picks a username, password and pin number and this is saved into UPP.db. I want to be able to parse this text so that a user can either be edited (Change password) or Deleted (Remove Line). I have coded a solution for both but both return me with an error and jump to the "Else" part of my If statement. I will provide the code for the edit section as the delete is pretty similar and would be redundant providing both.
echo "Please enter the username you wish to edit"
read usernamecheck
usernamesearch=$( grep $usernamecheck\$ UPP.db | cut -d" " -f1 )
if [ $usernamecheck = $usernamesearch ]
then
echo "Please enter $usernamesearch PIN"
read pincheck
pinupdate=$( grep $pincheck\$ UPP.db | cut -d" " -f3 )
if [[ $pin = $pinupdate && $usernamecheck = $usernamesearch ]]
then
echo "The user `grep "$pin"\$ UPP.db | cut -d" " -f1`'s password is about to be changed "
echo "Please enter the new password"
read newpass
passwordcheck=$( grep $pincheck\$ UPP.db | cut -d" " -f2 )
sed -i "s/$passwordcheck/$newpass/g" UPP.db
echo "Password changed"
else
echo "The PIN is incorrect"
fi
else
echo "This username is not reconised"
fi
;;
string equality comparisons will be more precise than regular expressions. Here, I use awk not grep to enable that. You might want to keep the bash man page handy while you read this.
read -p "Please enter the username you wish to edit: " usernamecheck
# extract all the fields at once
read -r db_user db_pass db_pin < <(
awk -v name="$usernamecheck" '$1 == name {print; exit}' UPP.db
)
if [[ -z $db_user ]]; then
echo "This username is not reconised"
else
read -p "Please enter $db_user PIN: " pincheck
if [[ "$db_pin" != "$pincheck" ]]; then
echo "The PIN is incorrect"
else
echo "Changing password for user $db_user:"
while true; do
read -s -p "Please enter the new password: " newpass
echo
read -s -p "Please re-enter the new password: " newpass2
echo
if [[ "$newpass" == "$newpass2" ]]; then
break
fi
echo "Passwords do not match. Try again."
done
ln UPP.db "UPP.db.$(date "+%Y%m%d%H%M%S")" # create a backup copy
temp=$(mktemp)
# update the password
awk -v newpw="$newpass" -v name="$db_user" '
$1 == name {$2 = newpw}
{print}
' UPP.db > "$temp" && mv "$temp" UPP.db
echo "Password changed"
fi
fi
Style tip: in an if-else-end construct, put the smaller block first, then the else-block is close to the condition. Aids readability.
If this plain text file, containing user passwords, is anywhere close to the internet, you're f*cked.

New User creation script error

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
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"
If the man -s8 useradd does not mention --user-group option available to use, then -U will not work. There's still another solution worth trying:
The default behavior (if the -g, -N, and -U options are not specified)
is defined by the USERGROUPS_ENAB variable in /etc/login.defs.
Another way is, you have to chain the useradd command with a groupadd command with the same username supplied as parameters to both the commands.
EDIT:
This must work. First create the group and then create the user and add this new user to the group. Since, you are doing this in a script this should do the job very well.
Do this:
groupadd jaredm2
useradd -m -g jaredm2 jaredm2
Instead of this:
useradd -m --user-group jaredm2
Note that certain other programs which would've been installed in your OS, may have changed your binary or access to it or even created an alias for it. Your which output suggests that it is linked to the useradd binary in bin directory, precisely where it should be.
So I guess:
the binary might have been changed or replaced by a process, by package installers or something else
there's some mismatch between the binary version and the man page version(most likely if you have upgraded your OS improperly, at some point of time)
I think, the only solutions would be using the above pair of commands or changing the useradd binary you are using manually.
The problem is caused by your script you made ​​the use magic quotes `...` to execute your shell commands.
These quotes should only be used if you want to store the return of a command in a variable:
example the instruction below:
FINALCOMMAND=`echo "$FINALCOMMAND $USERNAME" | sed 's/ *$//g' | sed 's/^ *//g'`
Otherwise it is not necessary to use this quote and they can produce bugs like yours.
you can put in your script:
useradd $FINALCOMMAND
passwd $USERNAME
chfn $USERNAME
instead of:
`useradd $FINALCOMMAND`
`passwd $USERNAME`
`chfn $USERNAME`
Judging from your update with debug content, it seems like what you are doing is different from the script you posted.
Anyway, from the debug output, I suspect you're trying to run the adduser command using double quotes
"$FINALCOMMAND"
or maybe enclosed in backticks too, as that seems to be the only situation that triggers the "No such file or directory" error message.
Getting rid of the double quotes should fix the problem.
p/s: Don't use command substitution (``) unless you're actually using the output (e.g. for testing or assigning to variables), or else something like this might happen
$ `echo Just saying hi`
Just: command not found
I fixed the bug in your script, simply remove the line IFS=","
It is a misuse of the environment variable IFS (Internal Field Separator)
The script becomes:
#!/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

Resources