0
#!/bin/bash

(...)

if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ] ; then # WORKS!  
echo "Erro  #1: Nao inseriu todos os argumentos necessarios! Tente novamente!"  
echo "Ajuda #1: Lembre-se, ep1.sh [diretoria] [modo] [informação] <nome_do_ficheiro>"

elif [[ "$2" != "contar" || "$2" != "hist" ]] ; then **# THE PROBLEM!!!**  
echo "Erro  #2: O segundo argumento está incorrecto! Tente novamente!"  
echo "Ajuda #2: Use contar ou hist."

elif [[ "$3" != "palavras" || "$3" != "bytes" ]] ; then # CAN'T TEST BECAUSE OF FIRST ELIF  
echo "Erro  #3: O terceiro argumento está incorrecto! Tente novamente!"  
echo "Ajuda #3: Use palavras ou bytes."

(...)

fi

So, my problem is in the first elif, when the elif is False, the program should not enter in the elif, but enters.

I do not see what could be the problem.

Can anyone help me?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Miguel
  • 69
  • 5

1 Answers1

1

Use && not ||.

elif [[ "$2" != "contar" && "$2" != "hist" ]] ; then

The reason for switching the operator is De Morgan's law. If you negate

if [[ $a == "foo" || $b == "bar" || $c == "baz" ]]

you get

# negated
if ! [[ $a == "foo" || $b == "bar" || $c == "baz" ]]

De Morgan's law says you can distribute the ! by switching all the =='s to !='s and changing || to &&.

# `!` distributed per De Morgan's law
if [[ $a != "foo" && $b != "bar" && $c != "baz" ]]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578