English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Manuale completo dei comandi di Linux
Il comando read di Linux viene utilizzato per leggere numeri da input standard.
Il comando read interno viene utilizzato per leggere dati da input standard. Questo comando può essere utilizzato per leggere l'input da tastiera, e quando viene utilizzato il reindirizzamento, può leggere una riga da un file.
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Spiegazione dei parametri:
1、Lettura semplice
#!/bin/bash #Di default, verrà effettuata una transizione di riga echo "Inserisci il nome del sito: " # Read input from the keyboard read website echo "The website name you entered is $website" exit 0 # Exit
Test results are:
Enter the website name: it.oldtoolbag.com The website name you entered is it.oldtoolbag.com
2. The -p parameter allows you to specify a prompt directly in the read command line.
#!/bin/bash read -p "Enter the website name:" website echo "The website name you entered is $website" exit 0
Test results are:
Enter the website name: it.oldtoolbag.com The website name you entered is it.oldtoolbag.com
3. The -t parameter specifies the number of seconds the read command waits for input. When the timer is up, the read command returns a non-zero exit status.
#!/bin/bash if read -t 5 -p "Enter the website name:" website then echo "The website name you entered is $website" else echo "\nSorry, you have exceeded the time limit for input." fi exit 0
When executing the program without input, wait for 5 seconds:
Enter the website name: Sorry, you have exceeded the time limit for input
4. In addition to inputting time counting, you can also use -n Parameter settings read The command counts the input characters. When the number of input characters reaches the predetermined number, it automatically exits and assigns the input data to the variable.
#!/bin/bash read -n1 -p "Do you want to continue [Y/N]?" answer case $answer in Y | y) echo "fine ,continue";; N | n) echo "ok,good bye";; *) echo "error choice";; esac exit 0
This example uses the -n option followed by the number 1, indicating that the read command will exit as soon as it receives one character. Just press a character to answer, and the read command will immediately accept the input and pass it to the variable without pressing the Enter key.
Only receive 2 inputs and exit:
#!/bin/bash read -n2 -p "Please enter two characters at will: " any echo "\nYou entered two characters are: $any" exit 0
When executing the program, enter two characters:
Please enter two characters at will: 12 The two characters you entered are: 12
5.-s The option can make read The data entered in the command is not displayed in the command terminal (in fact, the data is displayed, but read The command sets the text color to the same as the background color). This option is often used when entering a password.
#!/bin/bash read -s -p "Please enter your password:" pass echo "\nYou entered the password is $pass" exit 0
When executing the program, the entered password is not displayed:
Please enter your password: The password you entered is w3codebox
6. Read file
Ogni volta che viene chiamato il comando read, viene letto un "riga" di testo dal file. Quando non ci sono più righe da leggere, il comando read esce con uno stato non zero.
Come trasmettere i dati del file al comando read? Utilizzare il comando cat e trasmettere i risultati direttamente al comando while che contiene il comando read.
Il contenuto del file di test test.txt è il seguente:
123 456 w3codebox
Codice di test:
#!/bin/bash count=1 # Assegnazione di valore, senza spazi cat test.txt | while read line # L'output del comando cat viene utilizzato come input per il comando read, il valore di > viene messo in line do echo "Linea $count:$line" count=$[ $count + 1 ] # Prestare attenzione agli spazi nei parentesi quadri. done echo "finish" exit 0
Il risultato dell'esecuzione è:
Linea 1:123 Linea 2:456 Linea 3:w3codebox finish
usare -e parametro, di seguito è riportato un esempio di input di caratteri a Premere Tab La pressione di una chiave esce i nomi dei file correlati (che esistono nel directory):
$ read -e -p "Inserisci il nome del file:" str Inserisci il nome del file: a a.out a.py a.pyc abc.txt Inserisci il nome del file: a