0

I am trying to make an if statement that checks to see if a variable is not equal to multiple different values.

Here's my code:

for subID in {1..100}
    if [ "${subID}" != 2 8 34 56 89 92 ]
        echo "Yes"
    else
        echo "No"
    fi
done

I get an error saying that there are too many arguments. Is there a way to code for this completely within the if statement, or should I just set the values I want to compare as a separate variable using a for loop?

1 Answer 1

3

Use a case statement

for subID in {1..100}; do
    case $subID in 
        2|8|34|56|89|92) echo "Yes" ;;
        *)               echo "No" ;;
    esac
done

or bash extended globbing

shopt -s extglob
for subID in {1..100}; do
    if [[ $subID == @(2|8|34|56|89|92) ]]; then
        echo Yes
    else
        echo No
    fi
done
Sign up to request clarification or add additional context in comments.

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.