tags: Bash_Operatori_Di_Comparazione bash
== | is equal to |
!= | is not equal to |
< | is less than in ASCII alphabetical order |
> | is greater than in ASCII alphabetical order |
-z | if the string is empty (null) |
-n | if the string is not null |
Gli operatori di confronto di stringhe ”< / >” funzionano solo all’interno delle doppie parentesi quadre [[ <condizione> ]]. Possiamo trovare la tabella ASCII su Internet o utilizzando il seguente comando nel terminale. Diamo un’occhiata a un esempio più tardi. |
È importante notare che inseriamo la variabile per l’argomento specificato (1”). Questo indica a Bash che il contenuto della variabile deve essere gestito come una stringa. Altrimenti, si otterrebbe un errore.
#!/bin/bash
# Check the given argument
if [ "$1" != "HackTheBox" ]
then
echo -e "You need to give 'HackTheBox' as argument."
exit 1
elif [ $# -gt 1 ]
then
echo -e "Too many arguments given."
exit 1
else
domain=$1
echo -e "Success!"
fiOperatori per Interi
Confrontare numeri interi può essere molto utile per noi se sappiamo quali valori vogliamo confrontare. Di conseguenza, definiamo i passaggi successivi e i comandi su come lo script dovrebbe gestire il valore corrispondente.
-eq | is equal to |
-ne | is not equal to |
-lt | is less than |
-le | is less than or equal to |
-gt | is greater than |
-ge | is greater than or equal to |
#!/bin/bash
# Check the given argument
if [ $# -lt 1 ]
then
echo -e "Number of given arguments is less than 1"
exit 1
elif [ $# -gt 1 ]
then
echo -e "Number of given arguments is greater than 1"
exit 1
else
domain=$1
echo -e "Number of given arguments equals 1"
fiOperatori di File
Gli operatori dei file sono utili se vogliamo conoscere permessi specifici o se esistono.
-e | if the file exist |
-f | tests if it is a file |
-d | tests if it is a directory |
-L | tests if it is if a symbolic link |
-N | checks if the file was modified after it was last read |
-O | if the current user owns the file |
-G | if the file’s group id matches the current user’s |
-s | tests if the file has a size greater than 0 |
-r | tests if the file has read permission |
-w | tests if the file has write permission |
-x | tests if the file has execute permission |
#!/bin/bash
# Check if the specified file exists
if [ -e "$1" ]
then
echo -e "The file exists."
exit 0
else
echo -e "The file does not exist."
exit 2
fiOperatori Logici e Booleani
Otteniamo come risultato un valore booleano “falso” o “vero” con gli operatori logici. Bash ci dà la possibilità di confrontare stringhe utilizzando doppie parentesi quadre [[ <condizione> ]]. Per ottenere questi valori booleani, possiamo usare gli operatori di stringa. Indipendentemente dal fatto che il confronto corrisponda o meno, otteniamo il valore booleano “false” o “true”.
#!/bin/bash
# Check the boolean value
if [[ -z $1 ]]
then
echo -e "Boolean value: True (is null)"
exit 1
elif [[ $#--1-| > 1 ]]
then
echo -e "Boolean value: True (is greater than)"
exit 1
else
domain=$1
echo -e "Boolean value: False (is equal to)"
fiCon gli operatori logici, possiamo definire diverse condizioni all’interno di una. Ciò significa che tutte le condizioni che definiamo devono corrispondere prima che il codice corrispondente possa essere eseguito.
! | logical negotation NOT |
&& | logical AND |
| | logical OR |
#!/bin/bash
# Check if the specified file exists and if we have read permissions
if [[ -e "$1" && -r "$1" ]]
then
echo -e "We can read the file that has been specified."
exit 0
elif [[ ! -e "$1" ]]
then
echo -e "The specified file does not exist."
exit 2
elif [[ -e "$1" && ! -r "$1" ]]
then
echo -e "We don't have read permission for this file."
exit 1
else
echo -e "Error occured."
exit 5
fi