English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Comando read di Linux

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.

Sintassi

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

Spiegazione dei parametri:

  • -a seguita da una variabile, questa variabile viene considerata un array e viene assegnato un valore, il separatore predefinito è lo spazio.
  • -d seguita da un segno di spunta, in realtà solo il primo carattere è utile, come segno di fine.
  • -p seguita da un messaggio di prompt, viene stampato un messaggio di prompt prima dell'inserimento.
  • -e Può utilizzare la funzione di completamento dei comandi durante l'inserimento.
  • -n seguita da un numero, definisce la lunghezza del testo di input, molto utile.
  • -r Blocca \, senza questa opzione \ è un carattere di escape, con questa opzione \ è un carattere normale.
  • -s Modalità silenziosa, non visualizza i caratteri in input sullo schermo, ad esempio quando si inserisce la password durante il login.
  • -t seguita da secondi, definisce il tempo di attesa per l'inserimento dei caratteri.
  • -u seguita da fd, legge dal descrittore di file, che può essere un descrittore aperto da exec.

Esempio online

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

Manuale completo dei comandi di Linux