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
-zif the string is empty (null)
-nif 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!"
fi

Operatori 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.

-eqis equal to
-neis not equal to
-ltis less than
-leis less than or equal to
-gtis greater than
-geis 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"
fi

Operatori di File

Gli operatori dei file sono utili se vogliamo conoscere permessi specifici o se esistono.

-eif the file exist
-ftests if it is a file
-dtests if it is a directory
-Ltests if it is if a symbolic link
-Nchecks if the file was modified after it was last read
-Oif the current user owns the file
-Gif the file’s group id matches the current user’s
-stests if the file has a size greater than 0
-rtests if the file has read permission
-wtests if the file has write permission
-xtests 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
fi

Operatori 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)"
fi

Con 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