To start, I have already looked at the previous comments in the others listed here but unfortunately, none of the help provided has solved my issue.
I am working in CentOS 7 as my environment and I am coding some error handling into my add user script.
#! /bin/bash
echo -n "Enter username: "
read -r username
/bin/egrep -i "^${username}:" /etc/passwd
if [ $? -eq 0 ]
echo "User $username already exists. Please check the username and try again."
elif [ $? eq 1 ]
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup
else
echo "Error encountered."
fi
When I go to test it, I get the following error message:
./testscript-error.sh line 7: syntax error near unexpected token 'elif'
./testscript-error.sh line 7: elif [ $? eq 1 ]
I've tried:
elif [ $? eq 1 ]**;**
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup**;**
I've also tried:
elif [ $? eq 1 ]
then
echo "User $username does not exist. Please proceed with account creation."
then
adduser "$username"
echo -n "Enter password: "
read -r -s password
echo $username:$password | chpasswd
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup
Also with the same result. Not sure what I'm missing and could use another pair of eyes on it.
Here you go... I hope you will understand better the syntax and usage:
#!/bin/bash
while true; do
echo -n "Enter username: "
read -r username
/bin/egrep -i "^${username}:" /etc/passwd
if [ $? -eq 0 ]; then
echo "User $username already exists. Please check the username and try again."
else
echo "User $username does not exist. Proceed with account creation."
break
fi
done
adduser "$username"
if [ $? -gt 0 ]; then
echo "Error encountered."
exit 1
fi
echo -n "Enter password: "
read -r -s password
echo "$username:$password" | chpasswd
if [ $? -gt 0 ]; then
echo "Error encountered."
exit 1
fi
touch /etc/sudoers.d/sugroup
chmod 0440 /etc/sudoers.d/sugroup
echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/sugroup
if [ $? -gt 0 ]; then
echo "Error encountered."
exit 1
fi
Here is the finished working code.
#!/bin/bash
#========================================================================================================
# This script allows for account creation on a server |
# It also performs error handling to ensure that the user doesn't currently exist on the system. |
# Also provides feedback from the input to verify the entries are correct. |
#========================================================================================================
while true; do
echo -n "Enter username: "
read -r username
/bin/egrep -i "^${username}:" /etc/passwd
if [ $? -eq 0 ]; then
echo "User $username already exists. Please check the username and try again."
else
echo "User $username does not exist. Proceed with account creation."
break
fi
done
adduser "$username"
if [ $? -gt 0 ]; then
echo "Error encountered."
exit 1
fi
echo -n "Enter password: "
read -r -s password
echo "$username:$password" | chpasswd
echo "Password was succesfully set for $username."
if [ $? -gt 0 ]; then
echo "Error encountered. There was a problem with your entry. Please re-run the script and try again."
exit 1
fi
usermod -a -G wheel "$username"
echo "User was succesfully added to the group wheel."
if [ $? -gt 0 ]; then
echo "Error encountered."
exit 1
fi
echo "Successfully added $username to the system."
Related
Please help me. I wanted to write a script to add users. It was supposed to work in such a way that after creating one user, it asks whether to create another. I also wanted to display that the user was added or not added. The script I have works, but there is one problem. If I create a user by entering the correct password, I get a message that "user has been added". If I enter the wrong username, I get a message that "Failed to add a user "$username" !". However, when I create a user and enter two different passwords, I get an error that the passwords do not match. I abort adding the user and then the script says "user has been added". How do I fix this error, so that the script serves - if i cancel adding user it will show "error". Thanks for any tips.
#!/bin/bash
count=0
while [ $count -le 10 ]
do
while true; do
read -p "Do you want to add a user ? (Y/n) " yn
case $yn in
[yY] ) read -p "Enter a username without sudo privileges : " username
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "User $username exists!"
else
adduser --gecos GECOS "$username"
usermod -aG sudo "$username"
[ $? -eq 0 ] && echo "user has been added." || echo "Failed to add a user "$username" !"
fi
break;;
[nN] ) echo -----------------------------------------------;
count=$((10));
break;;
* ) echo invalid response;;
esac
done
count=$((count+1))
done
correct display of the script's operation
Update.
When I add the user correctly, I get the information -
user has been added.
when I enter the wrong name, I get the information -
Failed to add a user "$username" !
when I enter the wrong passwords and cancel adding a user, I get:
user has been added.
I tried to remove adding to the group, but the result is as above.
#!/bin/bash
while true; do
read -p "Do you want to add a user ? (Y/n) " yn
case $yn in
[yY] ) read -p "Enter a username without sudo privileges : " username
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "User $username exists!"
else
adduser --gecos GECOS "$username"
[ $? -eq 0 ] && echo "user has been added." || echo "Failed to add a user "$username" !"
fi
break;;
[nN] ) echo -----------------------------------------------;
break;;
* ) echo invalid response;;
esac
done
I tried - adduser --gecos GECOS "$username" && usermod -aG sudo "$username" but when I cancel adding user it shows "user has been added.". I also removed usermod -aG sudo "$username" , and when I canceling adding a user it shows "user has been added." .
I'm trying to make a bash script that creates users in Ubuntu. If the user exist, then it should asks to put in a different username that does not exist.
The same I would like for creating groups. I hope you guys can help me!
if [ $(id -u) -eq 0 ]; then
read -p "Please enter a username: " username
# Check if user exist or not
egrep "^$username" /etc/passwd >/dev/null
while [ $? -eq 0 ]; do
read -p "User $username already exist! Please enter a different username: " username
exit 1
done
groupadd -f "${group}"
useradd -m -g "${group}" "${username}"
[ $? -eq 0 ] && echo "User added to system!" || echo "Could not create user!"
else
echo "Only root may add a user to the system."
exit 2
fi
Like this:
if ((UID!=0)); then
echo >&2 "Only root may add a user to the system."
exit 2
fi
read -p "Please enter a username: " username
# Check if user exist or not
if id "$username" &>/dev/null; then
echo >&2 "User $username already exist"
exit 1
fi
groupadd -f "$group"
if useradd -m -g "$group" "$username"; then
echo "User added to system!"
else
echo >&2 "Could not create user!"
exit 1
fi
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
In order to simplicate the process of creating a new user in my company's NIS server, I wrote the following scripts:
#!/bin/bash
# This script will simplicate NIS user management.
# You will not be able to change password or delete users peeradmin and root through this script.
# Written by Itai Ganot 2014.
# Variables
USER=$1
GREP="/bin/grep"
PASSWDFILE="/etc/passwd"
YPPASSWD="/usr/bin/yppasswd"
USERDEL="/usr/sbin/userdel"
USERADD="/usr/sbin/useradd"
PASSWD="/usr/bin/passwd"
YPCAT="/usr/bin/ypcat passwd.byname"
# Functions
function usage {
echo -e "Usage: $0 <username to manage>"
}
function updatenis {
echo -e "\e[36m #===# Uptdating NIS database... \e[0m"
cd /var/yp && make
}
# Script
if [ -z "$USER" ]; then
usage
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo -e "Run as root!"
exit 1
fi
"$GREP" -q "$USER" "$PASSWDFILE"
if [ "$?" = "0" ]; then
echo -e "\e[36m #===# User already exists \e[0m"
echo -e "\e[36m #===# How would you like to continue? \e[0m"
USERID=$(id -u $USER)
select CHOICE in 'Change user password' 'Remove user' 'View user' 'Exit'; do
case $CHOICE in
"Change user password")
if [[ "$USER" = "peeradmin" || "$USER" = "root" ]]; then # Defense against changing root or peeradmin password
echo -e "\e[36m #===# User $USER should never be edited! \e[0m"
exit 1
fi
echo -e "\e[36m #===# Provide root password for NIS server... \e[0m"
"$YPPASSWD" "$USER"
break
;;
"Remove user")
if [[ "$USER" = "peeradmin" || "$USER" = "root" ]]; then # Defense against deletion of user root or peeradmin.
echo -e "\e[36m #===# User $USER should never be edited! \e[0m"
exit 1
fi
read -r -p "Remove home directory and mail? [y/n] " ANSWER1
if [[ "$ANSWER1" = [Yy] ]]; then
"$USERDEL" -r "$USER"
updatenis
echo -e "\e[36m #===# User $USER has been deleted along with the user's home folder and mail \e[0m"
break
else
"$USERDEL" "$USER"
echo -e "\e[36m #===# User $USER has been deleted \e[0m"
updatenis
break
fi
;;
"View user")
echo -e "\e[36m #===# Displaying user $USER \e[0m"
$YPCAT | $GREP "$USER"
break
;;
"Exit")
echo -e "\e[36m #===# Exiting, No changes done. \e[0m"
exit 0
;;
esac
done
else
read -r -p "User doesn't exist, would you like to add it? [y/n] " ANSWER2
if [[ "$ANSWER2" = [Yy] ]]; then
echo -e "\e[36m #===# Collecting required information... \e[0m"
sleep 2
LASTUID=$(tail -n 1 $PASSWDFILE | awk -F: '{print $3}')
NEXTUID=$(( LASTUID + 1 ))
$USERADD -g users $USER -u $NEXTUID
echo -e "\e[36m #===# Set password for the new user \e[0m"
$PASSWD $USER
updatenis
read -r -p "Would you like to test the creation of the user? [y/n] " ANSWER3
if [[ "$ANSWER3" = [Yy] ]]; then
$YPCAT | $GREP "$USER"
if [ "$?" = "0" ]; then
echo -e "\e[36m #===# User $USER created successfully! \e[0m"
fi
fi
elif [[ "$ANSWER2" = [Nn] ]]; then
echo -e "\e[36m #===# Exiting, no changes done. \e[0m"
exit 0
fi
fi
I want to make the script public so I'll be able to share it with some communities, however I'm having a hard time to get some specific thing done.
I want to define a new variable called: PROTECTEDUSERS and assign one or more usernames to it.
Example:
PROTECTEDUSERS="root peeradmin"
Then, In the relevant line:
if [[ "$USER" = "peeradmin" || "$USER" = "root" ]];
I want the line to include the newly created variable.
I've tried this:
if [[ "*$USER*" =~ "$PROTECTEDUSERS" ]]; then...
But it doesn't work, is that even a possible thing to do? I believe it is just don't know how to do it, please assist.
Thanks in advance
if [[ "$PROTECTEDUSERS" =~ $USER ]]; then
Update to not match substings like "admin":
if [[ "$PROTECTEDUSERS" =~ (^| )$USER($| ) ]]; then
You are using glob in reverse direction, use this condition with == in bash:
if [[ "$PROTECTEDUSERS" == *"$USER"* ]]; then...
glob pattern is only supported on RHS of comparison.
I'm getting the following error:
./adduser.sh: line 21: syntax error near unexpected token `else'
./adduser.sh: line 21: ` else'
I have been stuck here for an hour and I just can not figure it out.
#!/bin/bash
#==========================================================================================================================================
# Script Name: adduser.sh
# By: Tim mayo
# Date: 3/2013
# Purpose: Add a user to the Linux system
# Command line: adduser.sh
#
#==========================================================================================================================================
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Root user can only add a user to the Linux system"
exit 2
fi
The else keyword isn't associated with any if statement; you ended the if statement with the fi keyword on line 20. Perhaps you are missing an if just before the two read statements?
Another 'run as root only' example:
#!/bin/bash
if [ $UID -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 exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Root user can only add a user to the Linux system"
exit 2
fi