Linux shell read command

Options and usage Linux shell read command

Piracy since: https://www.cnblogs.com/zwgblog/p/5997367.html

1. Read some of the options

 Read may have -a, -d, -e, -n, -p, -r, -t, and -s eight options.

-a  : The content read into numerical values

echo -n "Input muliple values into an array:"
read -a array
echo "get ${#array[@]} values in array"

 

-d  : represents the delimiter, delimiter that is, under normal circumstances is IFS interval parameter, but by -d, we can define the character position has been read execution occurs. For example read -d madfds value, when there is a character read a m not continue to read back in, for example, input hello m, valid values "hello", please note m in front of the space, etc. will be deleted. This embodiment can enter multiple strings, such as the definition. "" Symbols, etc. as a binding.

-e  : only for scripts to interact with each other, it will be used to collect readline input line. Read these words do not quite understand what it meant, skip.

-n  : used to define the maximum number of characters that can be read as valid. For example echo -n 4 value1 value2, if we try to enter 1234, it is only effective in front of 123, as an input, in fact, enter the first four characters of your '3' after the end of the input automatically. Here is the result value of 12, value2 3.

-p  : used to give prompt, in the previous example we use the echo -n "..." to give you a prompt, you can use read -p value of only one way to represent the statement '... my promt?' .

-r  : In the input parameters, we can use the '/' means no complete input, line input continues, if we need the last line of '/' as a valid character, can be carried out by -r. Also in the input character, we want / n these special characters into effect, should also adopt the -r option.

-s  : For special symbols, such as an arrow number, they will not be printed on the Terminal, for example, read -s key, we press the cursor, after a carriage return, if we require display, i.e., echo, cursor up, if not used -s, at the time of input, the input display ^ [[a, i.e., printed on the Terminal, if required after the echo, the cursor moves up.

-t  : indicates the time to wait for input, in seconds, the wait time exceeded, will continue behind the script, do not pay attention as null input parameters will retain the original value

2. Read the associated instance

a. splicing file

The first three lines # afile file in the first four lines with bfile stitching together 
the while I && -u3 Read Read -u4 J; do 
echo $ I $ J 
DONE. 3 <afile. 4 <bfile

 b. Enter not display terminal

read -p "Input passwd:" -s Passwd
echo $Passwd

 c. Enter the limit, otherwise exit

# Delay five seconds, no input will automatically exit the 
read -p "Input a number:" -t 5 Number

 d. read characters defined

# 5 characters taken from the input 
read -p "Input a word:" -n 5 Word

 e. q exit wait output

# Input until you enter q, will automatically exit 
read -dp -p "Input some words end with q:" word

 ====================================================================================================

 read commands -n (no line breaks) -p (precautionary statements) -n (number of characters) -t (waiting time) -s (does not echo)


 

1, the basic reading

receive standard read command input (keyboard) input, or other input file descriptors (hereinafter say). After obtaining input, read command data into a standard variable. The following is a read command

The simplest form ::

! # / bin / bash 

echo -n "the Enter your name:" // parameter -n role is not wrapped, echo default is a newline 

read name // from the keyboard 

echo "hello $ name, welcome to my program" // displays information 

exit 0 // exit the shell program.

Since the read command provides a -p parameter allows you to specify a prompt read directly on the command line.

So the above script can be abbreviated to the following script ::

#!/bin/bash

read -p "Enter your name:" name

echo "hello $name, welcome to my program"

exit 0

 Read back above a variable name only, there may be a plurality, then if a plurality of input data, the first data to the first variable, the second data to the second variable, if the number of the input data too, the last all values ​​gave the first variable. If too little input does not end.

Variable may not be specified in the read command line. If the variable is not specified, then the command will read the received data is placed in the environment variable REPLY.

E.g::

read -p "Enter a number"

All data contained in the environment variable REPLY input, can be used like any other variable REPLY use environment variables in shell scripts.

2, clocking input.

Use the read command there is a potential danger. The script is likely to stop and have been waiting for user input. If the script regardless of whether the input data must continue to implement, you can use the -t option to specify a timer.

-t option to specify the number of seconds to wait for the read command input. When the timing is full, read command returns a non-zero exit status;

#!/bin/bash

if read -t 5 -p "please enter your name:" name

then 

    echo "hello $name ,welcome to my script"

else

    echo "sorry,too slow"

fi

exit 0

 

In addition to the input time counting, you may also be provided in the character input read command count. When the number of input characters reaches a predetermined number, automatic withdrawal, and the input data assigned 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 a value of 1, indicating the read command as long as receive a character exits. Just press a character to answer, read command immediately

Accept input and pass variables. Without pressing the Enter key.

3, silent reading (input is not displayed on the monitor)

Sometimes the script needs user input, but do not want to enter the data displayed on the monitor. A typical example is the input password, of course, there are many other hidden data needs.

The -s option is possible to read data command is not entered displayed on the monitor (in fact, the data is displayed, the just read command text and background color to the same color).

#!/bin/bash

read  -s  -p "Enter your password:" pass

echo "your password is $pass"

exit 0 

 

4, read the file

Finally, you can also use the read command to read files on a Linux system.

Every call to read command reads the file "line" text. When the file is not readable row, read commands will be nonzero state exit.

The key is how to read the file transfer data to read the text of the command.

The most common method is to use cat command file and transmits the results to the command includes the read command while pipes directly by

example::

! # / bin / bash 

COUNT = 1 // assignment statement, no spaces 

cat test | output while read line // cat command as input read commands, read read values are placed in the line 

do 

   echo "$ COUNT Line : $ Line " 

   COUNT = $ [$ COUNT + 1] // note in parentheses spaces. 

DONE 

echo "Finish" 

Exit 0

Guess you like

Origin www.cnblogs.com/faberbeta/p/linux-shell-read-command.html