Bash Script User Input

Invoer krijgen van de gebruiker is een gebruikelijke taak voor elke programmeertaal. Je kunt op verschillende manieren input van een gebruiker in bash script krijgen. Een lees commando wordt gebruikt in het bash script om gegevens van de gebruiker te krijgen. Enkele of meerdere gegevens kunnen in bash script worden opgenomen door verschillende opties van het read commando toe te passen. Enkele veel voorkomende toepassingen van het read commando worden in deze tutorial getoond.

Voorbeeld-1: Gebruik van eenvoudig read commando

In dit voorbeeld wordt een enkel gegeven van de gebruiker verkregen en wordt de waarde afgedrukt. Na het uitvoeren van het script, zal het programma wachten op de invoer van de gebruiker. Wanneer de gebruiker de gegevens invoert en op enter drukt, worden de gegevens opgeslagen in de antwoordvariabele. De waarde van de antwoord variabele wordt later afgedrukt. Een ding moet je onthouden: je hoeft het ‘

Uitvoer:

Exemplaar-2: Leescommando gebruiken met opties

-p optie wordt gebruikt met leescommando om een nuttig bericht voor de gebruiker weer te geven met betrekking tot de invoer. -s optie wordt gebruikt om de tekst die de gebruiker typt te verbergen voor de terminal. Dit wordt stille modus genoemd en wordt gebruikt voor wachtwoordgegevens. Het volgende voorbeeld toont het gebruik van beide opties.

Uitvoer:

Exemplaar-3: Leesopdracht gebruiken om meerdere invoer op te vragen

Als u meerdere invoer tegelijk wilt opvragen, moet u de leesopdracht met meerdere variabele namen gebruiken. In het volgende voorbeeld worden vier invoergegevens in vier variabelen opgenomen met behulp van het commando read.

#!/bin/bash
# Meerdere invoeren
echo “Typ vier namen van uw favoriete programmeertalen”
read lan1 lan2 lan3 lan4
echo “$lan1 is uw eerste keuze”
echo “$lan2 is uw tweede keuze”
echo “$lan3 is uw derde keuze”
echo “$lan4 is uw vierde keuze”

Output:

Exemplaar-4: Gebruik van leesopdracht met tijdslimiet

Als u invoer met tijdslimiet voor de gebruiker wilt instellen, moet u de optie -t gebruiken bij een leesopdracht. Hier wordt de tijd geteld als seconde. In het volgende voorbeeld wacht het programma 5 seconden op de invoer van de gebruiker en als de gebruiker de gegevens niet binnen 5 seconden kan invoeren, wordt het programma zonder waarde afgesloten.

#!/bin/bash
read -t 5 -p “Typ uw favoriete kleur : ” color
echo $color

Output:

U kunt de input van de gebruiker dus op verschillende manieren ophalen met het commando read, afhankelijk van de vereisten van uw script.

Voor meer informatie bekijk de video!

symbool niet te gebruiken bij het toewijzen van de waarde van een variabele, maar je moet het ‘

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!

symbool gebruiken bij het lezen van de variabele.

#!/bin/bash
echo -n “Wat is je favoriete eten :”
lees antwoord
echo “Oh! je houdt van $answer!”

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!

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *