1

I need to write a bash script which will

  1. check if it is run as root user
  2. ask for the user name
  3. check if user exists
  4. add new user with password
  5. ask for the group name
  6. check if this group exists
  7. add the user to the group

I wrote code like this

function create_user() {
if (( $EUID == 0)); then
echo "Only for root users."
exit
fi
echo "User name: "
read userschool
for x in $(awk -F":" '{ print }' /etc/passwd ); do
if [ $x == userschool ] ; then
echo "This user exists"
sleep $delay_time && exit
fi 
done
sudo useradd -p $(openssl passwd -1 $PASS) $userschool
echo "User has been added"
sleep $delay_time
echo "Name of group"
read school
for x in $(awk -F":" '{ print }' /etc/group ); do
if [ $x == school ] ; then
echo "Group doesn't exists"
else
echo "$userschool has been added to group $school"
sleep $delay_time
break 2
fi
done
}

It almost works but if the user exists the script also adds him one more time and I don't know why. I want to communicate that the user exists and stop.

Stephen Kitt
  • 434,908
  • 1
    As an aside, I'd strongly recommend you learn to indent your code so that blocks (loops) stand out. It makes writing (and debugging) so much easier – Chris Davies May 29 '20 at 16:32

3 Answers3

1

You're comparing $x with literal userschool not as variable. And you should double quote your variable calls to prevent globbing and word splitting. It should be :

if [ "$x" == "$userschool" ]; then
...
fi

if [ "$x" == "$school" ]; then
...
fi

And you don't have to iterate the whole /etc/passwd and make comparison line by line. Instead, just do:

awk -F: -v user=$userschool '$1 == user {print $1}' /etc/passwd

Where -v is a variable assignment in awk.

If it's already exist, then it will output given username

# Since it's bash, you can do this instead
read -p "Username: " userschool
exists=$(awk -F: -v user=$userschool '$1 == user {print $1}' /etc/passwd)

if [[ "$exists" ]]; then
  echo "User exists."
  sleep $delay_time
  exit 1
fi 

# create user
...
annahri
  • 2,075
0

The main problems are the

awk -F":" '{ print }' /etc/passwd

which prints the whole line. It must be

awk -F":" '{ print $1; }' /etc/passwd

and that you forgot the $ in $x == userschool. That must be "$x" = "$userschool", of course.

Furthermore it doesn't make sense to call exit with

sleep $delay_time && exit

because you want the exit to be executed in any case. But it is not if there are problems with sleep (e.g. if you forgot to set delay_time or gave it an illegal value). So that should be

E_USER_EXISTS=3
if [ "$x" = "$userschool" ] ; then
    echo "This user exists"
    sleep "$delay_time"
    exit "$E_USER_EXISTS"
fi

Nonetheless, this is a terrible approach. I will suggest a better one later.

Hauke Laging
  • 90,279
  • It's not solve my problem unfortunately. There is still communicate that user exists and later that has been added. – Lauro Mike May 29 '20 at 17:04
  • @LauroMike There are a lot of problems in your code. I fixed one/two more fatal errors. – Hauke Laging May 30 '20 at 04:46
  • cold you tell me where is these fatal errors? – Lauro Mike May 31 '20 at 13:21
  • @LauroMike As it says in my answer: (1) You forgot the $ in $x == userschool. (2) You executed exit conditionally i.e. depending on the other script code (which you haven't shown to us) or the environment the exit would not be executed. – Hauke Laging Jun 02 '20 at 00:29
0

By default, useradd command throws error if user exists already...you don't have to recheck it...

function create_user() {
if [[ $EUID -ne 0 ]]; then
 echo "This script must be run as root" 
 exit 1
fi
echo -n "User name: "
read userschool
sudo useradd -p $(openssl passwd -1 $PASS) $userschool && echo "User has been added"
sleep $delay_time
echo "Name of group"
read school
sudo groupadd $school &>>/dev/null && echo "Group created $school"
sudo usermod -aG $school $userschool
}
Akhil
  • 1,290
  • It's working very good but I still have problem with exists user because If there will be user which exists, there is communication about this and next I can add him to group one more time. – Lauro Mike May 31 '20 at 12:17