Saltar al contenido

ejemplo de código bash if elif else

Ejemplo 1: sintaxis bash de la instrucción else if

#!/bin/bash
 
read -p "Enter your marks: " marks
 
if [ $marks -ge 80 ]
then
    echo "Very Good"
 
elif [ $marks -ge 50 ]
then
    echo "Good"
 
elif [ $marks -ge 33 ]
then
    echo "Just Satisfactory"
else
    echo "Not OK"
fi

Ejemplo 2: ejemplo de instrucción if else en un script de shell

if [ ... ]
then
  # if-code
else
  # else-code
fi

Ejemplo 3: sentencia bash if

and - &&
or - ||

# and example
if [ -r $1 ] && [ -s $1 ]
then
echo This file is useful.
fi

# or example
if [ $USER == 'bob' ] || [ $USER == 'andy' ]
then
ls -alh
else
ls
fi

Ejemplo 4: bash else if

if [ "$animal" == "penguin" ]; then
  echo "Hmmmmmm fish... Tux happy!"
elif [ "$animal" == "dolphin" ]; then
  echo "Pweetpeettreetppeterdepweet!"
else
  echo "*prrrrrrrt*"
fi

if TEST-COMMANDS; then
  CONSEQUENT-COMMANDS;
elif MORE-TEST-COMMANDS; then
  MORE-CONSEQUENT-COMMANDS;
else
  ALTERNATE-CONSEQUENT-COMMANDS;
fi

Ejemplo 5: sentencia bash if

Operator			Description
! EXPRESSION			The EXPRESSION is false.
-n STRING			The length of STRING is greater than zero.
-z STRING			The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2		STRING1 is equal to STRING2
STRING1 != STRING2		STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2		INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2		INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2		INTEGER1 is numerically less than INTEGER2
-d FILE				FILE exists and is a directory.
-e FILE				FILE exists.
-r FILE				FILE exists and the read permission is granted.
-s FILE				FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE				FILE exists and the write permission is granted.
-x FILE				FILE exists and the execute permission is granted.

Ejemplo 6: bash if else if

if [[ TEST-COMMAND ]]
then
  STATEMENTS1
else
  STATEMENTS2
fi
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *