Wejście użytkownika skryptu Bash

Pobieranie danych wejściowych od użytkownika jest powszechnym zadaniem każdego języka programowania. W skrypcie bash można pobierać dane wejściowe od użytkownika na wiele sposobów. Polecenie read jest używane w skrypcie bash do pobierania danych od użytkownika. Pojedyncze lub wielokrotne dane mogą być pobierane w skrypcie bash przez zastosowanie różnych opcji polecenia read. Niektóre typowe zastosowania polecenia read są pokazane w tym tutorialu.

Przykład-1: Użycie prostego polecenia read

W tym przykładzie, pojedyncza dana jest pobierana od użytkownika i drukujemy jej wartość. Po uruchomieniu skryptu, program będzie czekał na wejście użytkownika. Kiedy użytkownik wpisze dane i naciśnie enter, dane zostaną zapisane do zmiennej answer. Wartość zmiennej answer jest później drukowana. Jedna rzecz, powinieneś pamiętać, że nie musisz używać symbolu '

Wyjście:

Przykład-2: Użycie polecenia read z opcjami

opcja -p jest używana z poleceniem read w celu wyświetlenia użytkownikowi jakiegoś pomocnego komunikatu związanego z danymi wejściowymi. Opcja -s jest używana do ukrywania tekstu z terminala, który będzie wpisywany przez użytkownika. Jest to tzw. tryb cichy i jest używany dla danych haseł. Poniższy przykład pokazuje użycie obu opcji.

Wyjście:

Przykład-3: Użycie polecenia read do pobrania wielu danych wejściowych

Jeśli chcesz pobrać wiele danych wejściowych naraz, musisz użyć polecenia read z wieloma nazwami zmiennych. W poniższym przykładzie, cztery dane wejściowe są pobierane do czterech zmiennych za pomocą polecenia read.

#!/bin/bash
# Pobieranie wielu danych wejściowych
echo „Wpisz cztery nazwy swoich ulubionych języków programowania”
read lan1 lan2 lan3 lan4
echo „$lan1 to twój pierwszy wybór”
echo „$lan2 to twój drugi wybór”
echo „$lan3 to twój trzeci wybór”
echo „$lan4 to twój czwarty wybór”

Wyjście:

Przykład-4: Użycie polecenia read z ograniczeniem czasowym

Jeśli chcesz ustawić ograniczone czasowo dane wejściowe dla użytkownika to musisz użyć opcji -t z poleceniem read. Tutaj czas jest liczony jako sekundy. W poniższym przykładzie program będzie czekał przez 5 sekund na wejście użytkownika i jeśli użytkownik nie będzie w stanie wpisać danych w ciągu 5 sekund to program zakończy działanie bez wartości.

#!/bin/bash
read -t 5 -p „Wpisz swój ulubiony kolor : ” color
echo $color

Wyjście:

Więc, możesz pobierać dane od użytkownika na różne sposoby używając polecenia read w zależności od wymagań Twojego skryptu.

Aby uzyskać więcej informacji, obejrzyj wideo!

w momencie przypisywania wartości zmiennej, ale musisz użyć symbolu '

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!

w momencie odczytywania zmiennej.

#!/bin/bash
echo -n „What is your favorite food :”
read answer
echo „Oh! you like $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!

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *