0

I recently created a different thread with an issue concerning a for loop in a bash script I was writing for my GCSE coursework. I have another issue with the same bash script (however it has evolved a fair bit since last time).

Here is the code:

#!/bin/bash
# A script that creates users.

uerror='^[0-9]+$'

echo "This is a script to create new users on this system."
echo "How many users do you want to add? (in integer numbers)"
read am
echo " "

if [[ $am =~ $uerror ]] ; then

    echo "ERROR: Please use integer numbers." 
    echo "Please re-enter the amount."
    read am ;

else 

    echo " "


    for i in $(seq "$am")
    do
        echo "Enter a username below:"
        read usern

        sudo useradd $usern

        sudo passwd $usern

        echo " "
        echo "User $i '$usern' added."

        echo " "
        echo "What group do you want to add $usern to?"
        read group
        sudo usermod $usern -aG $group

        echo "$usern added to $group"
        echo " "    
        echo "-------------------"
        echo " "
done
fi

The issue is in the if statement. It's purpose is to stop users entering anything other than an integer number. But for some reason, I don't seem to be able to capture the input from the read am part. Instead the script skips straight onto the for loop where the $(seq "$am") obviously will have issues comprehending an input that is not a number.

The output from this error is as follows.

seq: invalid floating point argument

However, I don't think this is relevant because as far as I can tell, the issue is with the if / else statement.

If anyone could point me in the right direction of what I need to do to fix this, I would be greatly appreciative.

I'd also like to iterate that I am still learning how to write bash scripts (and not in a particularly organised manner) so I've probably made a very simple mistake. Apologies for that.

Thanks, Callum.

EDIT: I mistyped an echo message, I've now changed that so it actually makes sense.

7
  • What's the semi-colon doing after your if? Commented Apr 29, 2015 at 18:08
  • @Almo Being correct (assuming you mean the one between ]] and then). Commented Apr 29, 2015 at 18:09
  • Just checking. That was the first thing that jumped out at me as looking weird. Commented Apr 29, 2015 at 18:09
  • The return of the [[ test is going to be true when the value matches that regex and false when it doesn't. You have your logic backwards. Commented Apr 29, 2015 at 18:09
  • 2
    Also you seem to be confused about what am is for. You ask for a number but the error talks about usernames and then asks for a username (which then isn't used anywhere). Commented Apr 29, 2015 at 18:10

2 Answers 2

1

If you want to read in a number and make sure it is a number use a while loop:

while read -p 'type a number:' n ; do
    # Exit the loop if the input is a number
    [[ "$n" =~ ^[0-9]+$ ]] && break
    echo "This was not a number! Don't trick me!"
done

# Now can use `seq`
seq "$n"

The if statement in your example would do the completely the wrong thing. It checks if the input is a number and in that case asks for the input again and exits the script. If you don't type a number, it uses the (wrong) input in the else branch.

Sign up to request clarification or add additional context in comments.

1 Comment

This works brilliantly! Thank you. I think I'd gone mad because now I look back on it, the backwards-ness is so obvious!
0

Replace your whole file with this:

#!/bin/bash
# A script that creates users.

uerror='^[0-9]+$'

echo "This is a script to create new users on this system."
echo "How many users do you want to add? (in integer numbers)"
read am
echo " "
while true; do
if [[ $am =~ $uerror ]] ; then

  break;  

else 
echo "Must be integer" 
echo "Please re-enter: "
read am ;
fi
done
    for i in $(seq "$am")
    do
        echo "Enter a username below:"
        read usern

        sudo useradd $usern

        sudo passwd $usern

        echo " "
        echo "User $i '$usern' added."

        echo " "
        echo "What group do you want to add $usern to?"
        read group
        sudo usermod $usern -aG $group

        echo "$usern added to $group"
        echo " "    
        echo "-------------------"
        echo " "
done

3 Comments

No, that's not right at all. == uses pattern matching not regex matching. Also this type of answer is not at all helpful as it doesn't explain anything and contains a large amount of entirely unrelated content.
@EtanReisner, fixed that. And if you look into OP's original code, you will find that it's all messed up. He is checking for integer and calling it username. That code makes no sense at all. I think a refined complete code was necessary
Yes, I commented about the username/count issue on the post. But that wasn't the question here. And more broadly you didn't mention that issue (or the logic issue) in your answer. You just dumped a bunch of code on the OP. That's not helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.