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
Related
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
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
im trying to create a simple bash script to create and delete a user on ubunutu , here is my script
sudo nano createuser.sh
#!/bin/bash
choice=2
# Main Display
echo "Enter Number to select an option"
echo
echo "1) Add User"
echo "2) Delete User"
echo
while [ $choice -eq 2 ]; do
read choice
if [ $choice -eq 1] ;
then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd $user_name -m -p $user_passwd
cat /etc/passwd
else if [$choise -eq 2] ; then
cat /etc/passwd
echo -e "Enter Password"
read del_passwd
echo -e "User to be deleted:"
read del_user
sudo userdel -r $del_user
cat /etc/passwd
echo
fi
im not sure if there s a typo on my script ,or something else .
whenever i execute the script i get this message
Enter Number to select an option
Add User
Delete User
./createuser.sh: line 31: syntax error: unexpected end of file
thank you in advance for your help !!
Errors:
wrong if/else/fi sequence, what you have is basically this w few errors
if [ ]
then
# something
else
if [ ]
then
# something else
fi
# fi should be here ti close outer if []
In bash you have if then/elif/else closed by fi So something like this
if []
then
# something
elif []
then
# something else happened
else
# something else than elif happened
fi
; after if [], it only goes there if if and than are in the same line, like so
if [] ; then
# something
elif []
# something else happened
else
# something else than elif happened
fi
space inside test brackets []
if [ a -eq 5 ]
# ^ ^
# +-------+----< notice space here
In bash while sequence goes as following while [ ] do done. like following
while [ i -le 55 ]
do
# do something
done
Suggestions
use -s for reading in password in bash to hide it while typing.
Conclusion, with all the fixes above here is working script:
#!/bin/bash
choice=2
# Main Display
echo "Enter Number to select an option"
echo
echo "1) Add User"
echo "2) Delete User"
echo
while [ $choice -eq 2 ]
do
read choice
if [ $choice -eq 1 ]
then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd $user_name -m -p $user_passwd
cat /etc/passwd
elif [ $choise -eq 2 ]
then
cat /etc/passwd
echo -e "Enter Password"
read del_passwd
echo -e "User to be deleted:"
read del_user
sudo userdel -r $del_user
cat /etc/passwd
echo
else
echo "Wrong option you have 1 or 2"
fi
done
yes guys ,thank you so much for your help
i just fixed it
here is my working script now
#!/bin/bash
choice=2
# Main display echo "Enter number to select an option" echo echo "1) Add User" echo "2) Delete User" echo
while [[ "$choice" -eq 2 ]]; do
read choice
if [[ "$choice" -eq 1 ]] ; then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd "$user_name" -m -p "$user_passwd"
cat /etc/passwd else
if [[ "$choice" -eq 2 ]] ; then
cat /etc/passwd
echo
echo -e "User to be deleted:"
read del_user
sudo userdel -r "$del_user"
cat /etc/passwd
echo
choice=2
fi
fi
done
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."
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
username=$1
freq=$2
checkuser()
{
if who grep "$1"
then
sleep 60
fi
}
if [ -n "$1" ]
then
echo "Enter username"
read username
checkuser
echo -e "$1 is logged on \a"
echo -e "$1 logged in at `date`">>LOG
checkuser
else
echo "User is not logged on"
fi
I need to integrate a second argument into my code which allows for the user to specify after what time should the script check to see who is logged in. I have it set to 60 seconds currently and this needs to be the default frequency. I tried to use another function but to no avail. I thought of something like this...
if [ "$2" -ne 0 ]
then
freq=$2
else
freq=60
Thanks William for that was very helpful!! I changed the code a bit and came up with this. I now need to add a 3rd argument "X" which when selected just sends a message to the LOGFILE and not to the screen. I made an attempt but not doing as intended.
username=$1
freq=${2:-10}
X=$3
checkuser()
{
whoami|grep "$1";
}
while checkuser "$username"
do
echo -e "$1 is logged on \a"
echo "$1 logged in at `date`">>LOGFILE
sleep $freq
exit 0
done
echo "User is not logged in"
if [ "$3" -ne 1 ]
then
echo "$1 logged in at `date`"LOGFILE
fi
username=$1
freq=${2:-60} # Set a default frequency
checkuser(){ who | grep -q "$1"; }
while ! checkuser "$username"; do
echo "User is not logged on"
sleep $freq
done
echo "$1 is logged on"
Also note that you can simplify the setting of username:
username=${1:-$( echo "Enter username: "; read u; echo $u; )}
echo 'Enter id'
read id
res=`who | grep "$id" | wc -l`
if [ $res -eq 0 ]
then
echo 'user is not logged in'
else
echo 'user is logged in'
fi
A simple shell script that takes the username as input and determines whether the user is currently logged in.
if [[ $# -eq 0 ]]; then
echo "Usage: " $0 "username"
exit 1
fi
result="$(who | grep $1 | wc -l)"
if [[ $result -gt 0 ]]; then
echo "$1 is currently logged in"
else
echo "$1 is not logged in"
fi
exit 0