Bash Script User Input

Prendere input dall’utente è un compito comune per qualsiasi linguaggio di programmazione. Puoi prendere input da un utente in uno script bash in diversi modi. Un comando di lettura è usato nello script bash per prendere dati dall’utente. Dati singoli o multipli possono essere presi nello script bash applicando diverse opzioni del comando read. Alcuni usi comuni del comando read sono mostrati in questo tutorial.

Esempio-1: usando un semplice comando read

In questo esempio, un singolo dato è preso dall’utente e stampiamo il valore. Dopo aver eseguito lo script, il programma aspetterà l’input dell’utente. Quando l’utente digita i dati e preme invio, allora i dati saranno memorizzati nella variabile risposta. Il valore della variabile di risposta viene stampato in seguito. Una cosa, dovresti ricordare che non hai bisogno di usare il simbolo ‘

Output:

Esempio-2: Usare il comando read con le opzioni

-l’opzione-p è usata con il comando read per mostrare qualche messaggio utile all’utente relativo all’input. L’opzione -s è usata per nascondere il testo dal terminale che sarà digitato dall’utente. Questa è chiamata modalità silenziosa ed è usata per i dati delle password. Il seguente esempio mostra l’uso di entrambe le opzioni.

Output:

Esempio-3: Usare il comando read per prendere input multipli

Se vuoi prendere più input alla volta allora devi usare il comando read con nomi di variabili multiple. Nel seguente esempio, quattro input sono presi in quattro variabili usando il comando read.

#!/bin/bash
# Prendendo input multipli
echo “Scrivi quattro nomi dei tuoi linguaggi di programmazione preferiti”
read lan1 lan2 lan3 lan4
echo “$lan1 è la tua prima scelta”
echo “$lan2 è la tua seconda scelta”
echo “$lan3 è la tua terza scelta”
echo “$lan4 è la tua quarta scelta”

Output:

Esempio-4: Usare il comando read con il limite di tempo

Se vuoi impostare un input limitato nel tempo per l’utente allora devi usare l’opzione -t con un comando read. Qui, il tempo viene contato come secondo. Nel seguente esempio, il programma aspetterà per 5 secondi l’input dell’utente e se l’utente non è in grado di digitare i dati entro 5 secondi, il programma uscirà senza valore.

#!/bin/bash
read -t 5 -p “Type your favorite color : ” color
echo $color

Output:

Quindi, potete recuperare l’input dall’utente in diversi modi usando il comando read in base ai requisiti del vostro script.

Per maggiori informazioni guardate il video!

al momento di assegnare il valore di una variabile ma devi usare il simbolo ‘

Output:

Example-2: Using read command with options

-p option is used with read command to display some helpful message for the user related to input. -s option is used to hide the text from the terminal which will be typed by the user. This is called silent mode and used for password data. The following example shows the use of both options.

Output:

Example-3: Using read command to take multiple inputs

If you want to take multiple inputs at a time then you have to use read command with multiple variable names. In the following example, four inputs are taken in four variables by using read command.

#!/bin/bash
# Taking multiple inputs
echo “Type four names of your favorite programming languages”
read lan1 lan2 lan3 lan4
echo “$lan1 is your first choice”
echo “$lan2 is your second choice”
echo “$lan3 is your third choice”
echo “$lan4 is your fourth choice”

Output:

Example-4: Using read command with the time limit

If you want to set time restricted input for the user then you have to use -t option with a read command. Here, time is counted as second. In the following example, the program will wait for 5 seconds for user’s input and if the user is unable to type the data within 5 seconds then the program will exit without value.

#!/bin/bash
read -t 5 -p “Type your favorite color : ” color
echo $color

Output:

So, you can retrieve input from the user in different ways using read command based on the requirement of your script.

For more information watch the video!

al momento di leggere la variabile.

#!/bin/bash
echo -n “Qual è il tuo cibo preferito:”
leggi risposta
echo “Oh! ti piace $risposta!”

Output:

Example-2: Using read command with options

-p option is used with read command to display some helpful message for the user related to input. -s option is used to hide the text from the terminal which will be typed by the user. This is called silent mode and used for password data. The following example shows the use of both options.

Output:

Example-3: Using read command to take multiple inputs

If you want to take multiple inputs at a time then you have to use read command with multiple variable names. In the following example, four inputs are taken in four variables by using read command.

#!/bin/bash
# Taking multiple inputs
echo “Type four names of your favorite programming languages”
read lan1 lan2 lan3 lan4
echo “$lan1 is your first choice”
echo “$lan2 is your second choice”
echo “$lan3 is your third choice”
echo “$lan4 is your fourth choice”

Output:

Example-4: Using read command with the time limit

If you want to set time restricted input for the user then you have to use -t option with a read command. Here, time is counted as second. In the following example, the program will wait for 5 seconds for user’s input and if the user is unable to type the data within 5 seconds then the program will exit without value.

#!/bin/bash
read -t 5 -p “Type your favorite color : ” color
echo $color

Output:

So, you can retrieve input from the user in different ways using read command based on the requirement of your script.

For more information watch the video!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *