0

I am trying to get this bash program to work but I can't seem to figure it out. I am writing this script to take a list of names and print out each names group number. I am very new to bash, so I have no idea what to do with the variable GROUPNUM in my for statement for this program to work. For example, if the users name they enter is Melissa, the program should output "Melissa, you are in group 20." I think my problem lies within my variable GROUPNUM in my for loop. Any help would be awesome, thank you.

#!/bin/bash
echo "Please enter your first name: "
read NAME
for GROUPNUM in $(NAME)
do
case $NAME in
[a-H]*) echo "$NAME, you are in group 10"
;;
[i-M]*) echo "$NAME, you are in group 20"
;;
[n-Q]*) echo "$NAME, you are in group number 30"
;;
[r-Z]*) echo "$NAME, you are in group number 40"
;;
*) echo "Please enter valid input!"
;;
esac
done
echo "Goodbye!"
5
  • start with using meaningful indentation. Commented Mar 2, 2018 at 8:29
  • 1
    The expression $(NAME) tries to execute the command NAME. Read the manual page and a few tutorials. Commented Mar 2, 2018 at 8:31
  • Besides that, you don't need a loop at all. Just check the first letter (and be consistent with upper and lower case!) in a case. Commented Mar 2, 2018 at 8:33
  • 1
    What errors are found when you run your script through ShellCheck (like you should do each time before posting here...) Commented Mar 2, 2018 at 9:08
  • Are you perhaps looking for for name in Mark Melissa Grant Jim; do ...? Commented Mar 2, 2018 at 9:35

2 Answers 2

1

You have several things that are questionable. Guessing what you are trying to do, this is probably close:

#!/bin/bash

# This gives an infinite loop
while :
do
    echo "Please enter your first name: "
    # Changed NAME to name, uppercase can clash with shell variables
    read name

    # This breaks out of the infinite loop when the user hits <RETURN>
    [[ -z $name ]] && break

    # Note that I have changed the patterns to what I think you mean
    case $name in
        [a-hA-H]*) echo "$name, you are in group 10"
        ;;
        [i-mI-M]*) echo "$name, you are in group 20"
        ;;
        [n-qN-Q]*) echo "$name, you are in group number 30"
        ;;
        [r-zR-Z]*) echo "$name, you are in group number 40"
        ;;
        *) echo "Please enter valid input!"
        ;;
    esac
done
echo "Goodbye!"
Sign up to request clarification or add additional context in comments.

3 Comments

This works great, thank you for posting this edit to my code. Is there a way to add a for...in loop to this or...? If you don't think its necessary then disregard my question.
A loop requires multiple values and you are only reading one. We can't really guess what you want to loop over or why you think this would be useful, necessary, or desirable.
I really can't see a reason to use a for loop. It is used to iterate (get one item at a time) over something (a list), and there is nothing to iterate over so far as I can see.
0

Remove the lines, starting with

for
do

done

Then you will hit the next bug. But don't use loops without reason.

4 Comments

For this program I need a for...in loop, to process each name I need a case statement inside a for... in loop. Are you saying the for loop is unnecessary? Edit: I usually code in c++ and I just realized how stupid it is that I put a case statement inside a for loop, its one or the other. I admit, this is homework, and my teacher is requiring me to use a for...in loop AND case statements.. what would you recommend?
But you only read one name. Start with a single case. If you got it right, you may add a loop. You can comment it out, and later in. Do you know functions?
Putting a for-loop around a case statement isn't stupid. Well, the identifier name 'groupnum' might. You want to detect the groupnum only later in the case statement. Well, and there is no second name to be read after entering the for loop.
Thanks for the tips

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.