0

i want to let the user Input the selection, then use if elif statement to differentiate the input.

code:

read in

if $in = "1"
then 
Type="Employee1"
elif $in = "2"
then
Type="Employee2"
elif $in = "3"
then
Type="Employee3"
else
echo "Wrong input"

Error:

./Pay.sh: line 127: 1: command not found
./Pay.sh: line 130: 1: command not found
./Pay.sh: line 133: 1: command not found
Wrong input

please advise thank you

3
  • What is the question? Commented Nov 1, 2013 at 15:38
  • Have you at least read a few things about bash before asking your question? you know, basic things like tests? Commented Nov 1, 2013 at 15:39
  • Read about the select command. Commented Nov 1, 2013 at 15:45

5 Answers 5

3

Square brackets are required.

if [[ $in = "1" ]]; then
    Type="Employee1"
elif [[ $in = "2" ]]; then
    Type="Employee2"
elif [[ $in = "3" ]]; then
    Type="Employee3"
else
    echo "Wrong input"
fi

For this particular chain of ifs you might alternatively use a case statement.

case $in in
    1) Type='Employee1';;
    2) Type='Employee2';;
    3) Type='Employee3';;
    *) echo 'Wrong input' >&2;;
esac
Sign up to request clarification or add additional context in comments.

3 Comments

Double square brackets are a more recent shell addition and are not universally portable.
The question is tagged bash, so [[ is better than [.
Which doesn't change the fact that noting the possible portability issues is a good thing. I didn't suggest he not use them or vote him down for doing so or anything like that.
2

In my opinion it's poor style to use a shell keyword such as in as a variable name.

read inp

case $inp in
  1) Type="Employee1";;
  2) Type="Employee2";;
  3) Type="Employee3";;
  *) echo "Wrong input";;
esac

You may also consider the select construct. Widely disliked for its rigid output, it's still useful for getting very specific input:

select Type in Employee{1,2,3}; do
  # Whatever you want to do with $Type
done

9 Comments

@gniourf_gniourf can, yes, should no.
What's wrong with $in? It has a dollar sign in front, it can't be confused for a keyword.
why shouldn't he? in bash, keywords and variables don't play in the same ball park.
@JohnKugelman Shell keywords don't always have a $ in front of them. The $ is an indicator for expansion, not naming. (And even in expansion, the dollar sign is not required to be used in arithmetic expression context.)
Who downvoted this answer??? it's a neat answer (apart from the first sentence that's just a matter of opinion).
|
2

This is the kind of cases for which case was built:

#!/bin/bash

read in

case "$in" in
        1)
           Type="Employee1"
           ;;
        2)
           Type="Employee2"
           ;;
        3)
           Type="Employee3"
           ;;
        *)
           echo "wrong input"
           ;;
esac

echo "type is $Type"

As a side note, with case you can match many conditions all together. For example, for $in being 1 or 5:

case "$in" in
        1|5)
           Type="Employee1"
           ;;

6 Comments

case "$in" in [1-3]), perhaps?
What do you mean, @kojiro?
I'm suggesting a different pattern for your latter case statement.
Well, 1|5 stands for: case $in=1 or case $in=5, so it would be a different situation. Thanks anyway, [1-4]` would work if we wanted to check if the value belongs to the 1-4 range.
Yes, except case $in in 1|5)… won't actually match what OP is doing.
|
0

A totally different approach, if your goal is to set a variable depending on user's input is to use an associative array. The benefit is that if your choices get many more, the large part of the code will be for defining this array (that can eventually be easily automated), and not in the conditional part:

declare -A ass=() # that's a cool variable name
ass[1]="Employee 1"
ass[2]="Employee 2"
ass[3]="Employee 3"

read in # I have no problems with a variable named 'in' ;)

if [[ -z ${ass[$in]} ]]; then
    echo "Wrong answer you banana!"
else
    Type=${ass[$in]}
fi

Comments

0

I'd use the user input as part of Type:

read inp

if ( [ $inp -ge 1 ] && [ $inp -le 3 ] )
then
    Type="Employee$inp"
else
    echo "wrong input"
fi

Comments

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.