Elágazások

IF

#!/bin/bash

if [ $1 -gt 100 ]
then
    echo Hey that\'s a large number.

fi

#!/bin/bash
if [ $1 -gt 100 ]
then
    echo Hey that\'s a large number.

else
    echo Hey that\'s a small number.

fi


Figyelmeztetjük a felhasználót, hogy adjon meg paramétert a szkriptnek:

if [ ! -n "$1" ]; then
  echo "Parameter not supplied."
fi
Rakjátok össze egy szkriptet ezzel a teszteléssel!


Tesztelő Operators

Operator Description
! EXPRESSION The EXPRESSION is false.
-n STRING The length of STRING is greater than zero.
-z STRING The length 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.


IF ELIF ELSE

#!/bin/bash
# elif statements

if [ $1 -ge 18 ]
then
    echo You may go to the party.
elif [ $2 == 'yes' ]
then
    echo You may go to the party but be back before midnight.
else
    echo You may not go to the party.
fi


Zuletzt geändert: Sunday, 7. November 2021, 21:39