1
#!/bin/bash

echo "Type in your username in lowercase letters"
read user
#sudo adduser $user

echo "Are you a student or teacher?"
read group

if (("$group"=="teacher"));
then
        #sudo usermod -aG teachers
        echo "teacher"

elif (("$group"=="student"));
then
        #sudo usermod -aG students
        echo "students"
else
        echo "Sorry this group doesn't exist"
fi

I'm trying to make a shell script that allows me to create a user and then automatically add it into a group that they want to be in. The input is either a student or a teacher although i want to include these statements above but i can't seem to make it work because it just goes to to the 'if' statement and ignores if i use the input student.


Could you please help me with this problem?


0

IMO case is a better match for this task than if/elif/else/fi. For example:

case "$group" in 
    teacher)  echo teacher ; sudo usermod -aG teachers ;;
    student)  echo student ; sudo usermod -aG students ;;
    *) echo "Sorry, this group doesn't exist" ; exit 1 ;;
esac

Note that wildcards can be used:

    te*)  echo teacher ; sudo usermod -aG teachers ;;
    st*)  echo student ; sudo usermod -aG students ;;

that will match any group entered beginning with te or st.

BTW, instead of running sudo multiple times in the script, you're better off writing the script so that it first gets the input from the user, then validates/sanitises it, and only then runs sudo once to perform the required actions. If necessary, write a second script that does ONLY the adduser and usermod stuff based with the args passed on the command line, and only allow that script to be run by sudo.

For example:

#! /bin/sh

read -p "Type in your username in lowercase letters: " user
grep -q "^$user:" /etc/passwd && echo "Sorry, that user already exists" && exit 1

read -p "Are you a student or teacher? " group

[[ "$group" ~ student|teacher ]] || echo "Sorry, no such group" && exit 1

sudo useradd "$user" -G "$group"

but it's hard to see the point of this script when you can just do it all on the command line with sudo useradd user -G group without wasting time with prompts and answering questions.

2

try

if [ "$group" = teacher ] 

mind

  • space arround [ and ]
  • = (no need for ==)
  • no semicolon

instead of

if (("$group"=="teacher"));
1

In bash, this:

if (("$group"=="teacher"));

is a numeric test of the variables. The variables are evaluated as numbers, and if they contain only text, they evaluate to 0.

The correct test for text in bash is:

if [[ $group == teacher ]];

no need for quotes in this case (not always) and you can use == or = (which are equivalent inside [[).

For POSIX shells you need to use:

if [ "$group" = "teacher" ];

You do need to use simple [, quote the variable, and use the =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.