Cómo hacer una operación lógica o en Shell Scripting


Estoy tratando de hacer una comprobación de condición simple, pero no parece funcionar.

Si $# es igual a 0 o es mayor que 1 entonces di hola.

He probado la siguiente sintaxis sin éxito:

if [ "$#" == 0 -o "$#" > 1 ] ; then
 echo "hello"
fi

if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
 echo "hello"
fi
Author: fedorqui, 2010-11-06

7 answers

Esto debería funcionar:

#!/bin/bash

if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then
    echo "hello"
fi

No estoy seguro de si esto es diferente en otros shells, pero si desea usar , debe colocarlos dentro de un doble paréntesis de la siguiente manera:

if (("$#" > 1))
 ...
 810
Author: Coding District,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-26 10:40:20

Este código funciona para mí:

#!/bin/sh

argc=$#
echo $argc
if [ $argc -eq 0 -o $argc -eq 1 ]; then
  echo "foo"
else
  echo "bar"
fi

No creo que sh soporte "==". Use " = " para comparar cadenas y-eq para comparar ints.

man test

Para más detalles.

 48
Author: jbremnant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-06 02:01:36

Si está utilizando el código de salida de bash status ?? como variable, es mejor hacer esto:

if [ $? -eq 4 -o $? -eq 8 ] ; then  
   echo "..."
fi

Porque si lo haces:

if [ $? -eq 4 ] || [ $? -eq 8 ] ; then  

La parte izquierda del OR altera el ?? variable, por lo que la parte derecha de la O no tiene el original ?? valor.

 29
Author: luca76,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-10 15:25:57

A veces es necesario utilizar corchetes dobles, de lo contrario se obtiene un error como demasiados argumentos

if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]]
  then
fi
 17
Author: TechNikh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-02 15:17:32

¿has probado algo como esto:

if [ $# -eq 0 ] || [ $# -gt 1 ] 
then
 echo "$#"
fi
 6
Author: John Boker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-06 01:58:52

Si un script bash

If [[ $input -gt number  ||  $input  -lt number  ]]
then 
    echo .........
else
    echo .........

fi

exit
 2
Author: Peprah David,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-07 04:38:25

De Manual de Referencia de Bash → 3.4.2 Parámetros especiales

#
( # # ) Se expande al número de parámetros posicionales en decimal.

Por lo tanto, $# siempre será 0 o un entero mayor.

Así que si quieres hacer algo siempre que $# sea 0 o mayor que 1, solo tienes que comprobar si $# es o no es 1:

[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"

Esto usa la sintaxis:

[ condition ] && {things if true} || {things if false}
 1
Author: fedorqui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-16 09:57:08