Bash-Skript-Benutzereingabe

Die Entgegennahme von Benutzereingaben ist eine übliche Aufgabe für jede Programmiersprache. In einem Bash-Skript können Sie auf verschiedene Weise Eingaben vom Benutzer entgegennehmen. Ein Lesebefehl wird im Bash-Skript verwendet, um Daten vom Benutzer zu übernehmen. Einzelne oder mehrere Daten können im Bash-Skript durch Anwendung verschiedener Optionen des read-Befehls übernommen werden.

Beispiel-1: Verwendung eines einfachen Lesebefehls

In diesem Beispiel werden einzelne Daten vom Benutzer entnommen und der Wert gedruckt. Nach dem Ausführen des Skripts wartet das Programm auf die Benutzereingabe. Wenn der Benutzer die Daten eingibt und die Eingabetaste drückt, werden die Daten in der Antwortvariablen gespeichert. Der Wert der Antwortvariable wird später gedruckt. Eine Sache, die Sie sich merken sollten, ist, dass Sie das ‚

Ausgabe:

Beispiel-2: Verwendung des read-Befehls mit Optionen

Die Option -p wird mit dem read-Befehl verwendet, um eine hilfreiche Meldung für den Benutzer bezüglich der Eingabe anzuzeigen. Die Option -s wird verwendet, um den Text aus dem Terminal auszublenden, der vom Benutzer getippt wird. Dies wird als stiller Modus bezeichnet und für Kennwortdaten verwendet. Das folgende Beispiel zeigt die Verwendung beider Optionen.

Ausgabe:

Beispiel-3: Verwendung des read-Befehls zur Erfassung mehrerer Eingaben

Wenn Sie mehrere Eingaben auf einmal erfassen möchten, müssen Sie den read-Befehl mit mehreren Variablennamen verwenden. Im folgenden Beispiel werden vier Eingaben in vier Variablen mit dem read-Befehl erfasst.

#!/bin/bash
# Mehrere Eingaben entgegennehmen
echo „Geben Sie vier Namen Ihrer bevorzugten Programmiersprachen ein“
read lan1 lan2 lan3 lan4
echo „$lan1 ist Ihre erste Wahl“
echo „$lan2 ist Ihre zweite Wahl“
echo „$lan3 ist Ihre dritte Wahl“
echo „$lan4 ist Ihre vierte Wahl“

Ausgabe:

Beispiel-4: Verwendung des read-Befehls mit Zeitlimit

Wenn Sie die Eingabe für den Benutzer zeitlich begrenzen wollen, müssen Sie die Option -t mit einem read-Befehl verwenden. Dabei wird die Zeit als Sekunde gezählt. Im folgenden Beispiel wartet das Programm 5 Sekunden lang auf die Eingabe des Benutzers und wenn der Benutzer die Daten nicht innerhalb von 5 Sekunden eingeben kann, wird das Programm ohne Wert beendet.

#!/bin/bash
read -t 5 -p „Geben Sie Ihre Lieblingsfarbe ein: “ color
echo $color

Ausgabe:

So können Sie mit dem read-Befehl je nach Anforderung Ihres Skripts auf unterschiedliche Weise Eingaben vom Benutzer abrufen.

Für weitere Informationen sehen Sie sich das Video an!

-Symbol nicht verwenden müssen, wenn Sie den Wert einer Variablen zuweisen, aber Sie müssen das ‚

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!

-Symbol verwenden, wenn Sie die Variable lesen.

#!/bin/bash
echo -n „Was ist Ihr Lieblingsessen : „
Lesen Sie die Antwort
echo „Oh! Sie mögen $Antwort!“

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!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.