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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Andrew Nick
  • 23
  • 1
  • 3
  • possible duplicate of [checking number of arguments bash script](http://stackoverflow.com/questions/18568706/checking-number-of-arguments-bash-script) – Maximin Dec 06 '14 at 18:42

2 Answers2

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 [[ ]]

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Logic.

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