next up previous
Next: trap Up: Estructuras de control Previous: for ... do ...

if ... then ... fi

Esta estructura de control permite ejecutar porciones de código de scripts condicionalmente. Su sintaxis:
if expresión1 then
lista de comandos1
elif expresión2 then
lista de comandos2
else lista de comandos3
fi

Un ejemplillo:



width 3pt
width .3pt415pt$ cat if.sh
echo -n énter a number: '
read number
if [ $number -lt 0 ]
then
 echo negative
elif [ $number -eq 0 ]
then
 echo zero
else
 echo positive
fi
$ if.sh
enter a number: 0
zero
$ if.sh
enter a number: -4
negative
$ if.sh
enter a number: 10
positive
$
width .3pt
depth .3pt width -3pt



Vicente González Ruiz
1998-07-13