0

I have a script where I pass a string variable. Now I would like to check the value of the variable passed like this:

  1. Check if it's not null
  2. Check if is "Test1"
  3. Check if is "Test2"

I wrote this: #!/bin/bash

if [[ "$1" == "" || "$1" != "Test1" || "$1" != "Test2" ]]; then
    echo "ERROR: no argument or bad argument passed. Only Test1 or Test2 is accepted"
    exit 2
else
    echo "ok good value"
    exit 0
fi

but when I try the script (./script.sh Test1 or ./script.sh Test2) I receive always:

ERROR: no argument or bad argument passed. Only Test1 or Test2 is accepted

1
  • 2
    if argument is null OR argument is NOT Test1 OR argument is NOT Test2 is always going to be true. Commented Oct 29, 2021 at 14:57

3 Answers 3

2

thanks for your support, but it's wrong, because accept all values not null.

I solved using this:

if [[ "$1" != "Test1" ]] && [[ "$1" != "Test2" ]]; then
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of if [[ ..., try case which is more portable:

case "$1" in 
    Test[12]) echo "ok good value"
              exit ;;
    *)        echo "ERROR: no argument or bad argument passed. Only Test1 or Test2 is accepted";
              exit 2 ;;
esac

Note: if a program has many users vague error messages of the form "No X or else no Y" can sometimes waste man-years of collective time, because half the users will be testing for the wrong problem. So if the code had a lot of users, separate error messages would be better:

case "$1" in 
    Test[12]) echo "ok good value"
              exit ;;
    "")       echo "ERROR: no argument passed. Only Test1 or Test2 is accepted";
              exit 2 ;;
    *)        echo "ERROR: Bad argument, only Test1 or Test2 is accepted";
              exit 2 ;;
esac

4 Comments

exit 0 would still be preferable. exit by itself uses the exit status of echo, which should be 0 but could be non-zero under some circumstances.
@chepner, The omission was intentional. It's not obvious what circumstances might occur where plain old echo would fail in some way where it wouldn't be better to know that it failed. For example: echo foo > /; echo $? prints a "1", and that is useful -- especially for a novice...
If you're going to flag an error with writing to standard output, you should be consistent. If the exit status is 2, was the problem with the arguments, or did that echo fail for some reason?
@chepner, Agreed that we don't want ambiguous error codes. But your last example assumes it's even possible for a plain echo "string" to return an exit status of 2. If there's a good example of that, please elaborate.
0

Try replacing

if [[ "$1" == "" || "$1" != "Test1" || "$1" != "Test2" ]]; then

with

if [ -z "$1" ] || [ "$1" == "Test1" ] || [ "$1" == "Test2" ]]; then

1 Comment

This allows an empty argument.

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.