2

I want to test the number of arguments passed to a Linux shell script. If the number of arguments is not 2 or 4, it should print something. Unfortunately it does not work. Can anyone explain what I am doing wrong?

#!/bin/bash
if [[ $# -ne 2 ]] || [[ $# -ne 4 ]];
then
    echo "here";
fi
1

2 Answers 2

4

You should replace logical OR by logical AND, so :

#!/bin/bash

if [[ $# -ne 2 && $# -ne 4 ]]; then
   echo "here"
fi

In arithmetic form:

#!/bin/bash

if (($# != 2 && $# != 4)); then
   echo "here"
fi

As you can see, no need to use 2 [[ ]]

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

Comments

1

Logic.

if [[ $# -ne 2 ]] && [[ $# -ne 4 ]]; then
  echo "here"
fi

2 Comments

It's -ne (not equal)
Also, the code posted by OP also has a problem: a value is always not 2 or not 4

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.