The read command in Linux is mainly used to read and input terminals or files. It is also a command that will be used frequently. The following will give you a detailed introduction to the usage of the read command under Linux. Let's take a look at it.
I. Overview
read command receiving input standard input (keyboard), or other input file descriptors. After getting the input, the read command puts the data into a standard variable.
Second, use examples (only some commonly used options are listed here)
1. Basic reading
The code is as follows:
#! /bin/bash
echo -n “Enter your name:”#Parameter-n is not a line break, echo default is a newline
read name #Enter from keyboard
echo “hello $name, welcome to my program”
exit 0 # Exit the shell program.
It is equivalent to the following:
The code is as follows:
read -p“Enter your name:”name #-p parameter, allowed directly in the read command line Specify a prompt
The variable after the read above has only one name, and there can be more than one. If you input multiple data, the first data is given to the first variable, and the second data is given to the first data. Two variables. If the number of input data is too large, all the last remaining values are given to the last variable, and if too little input does not end.
2. You can also not specify a variable in the read command line.
If you do not specify a variable, the read command will place the received data in the environment variable REPLY
The code is as follows:
read -p “Enter a number”
echo $REPLY
3. Timing input
There is a potential danger in using the read command . The script is likely to stop and wait for the user's input. If you must continue execution regardless of whether you enter a data script, you can use the -t option to specify a timer that specifies the number of seconds the read command waits for input. When the timer is full, the read command returns a non-zero value (0 is the normal exit state);
The code is as follows:
#! /bin/bash
ifread -t 5 -p “please enter your name:”name
then
echo “hello $name, welcome to my script&rdquo ;
else
echo “sorry,too slow”
fi
exit 0
Previous12Next Total 2 Pages
High-availability cluster can make the overall service of the cluster as available as possible, redu
The Nginx log in the Linux system can view the system running records and error des
In the Linux system, the top command can be used to monitor the status of the process, or to modify
Linux system operation, many people are practical Golang development language, in o
How to install and use Sphinx in Linux system
Steps to install Fluentd on RedHat
How to delete the specified file in swap
How to choose qcow2 and raw image format for Linux system
Linux system startx command function and usage
How to install LAMP in yum in CentOS6.5 system
How to modify the default timeout time of sudo in Ubuntu system?
How to install and configure Vim IDE for Linux systems
Linux prompts when you use Mkdir can not create directory ‘test’?
Suggestions for Linux system desktops
How to make a bootable img/iso image file on a Linux system
What should I do if libpng fails to compile after Gentoo upgrade?
Win7 change U disk background skills (with code)
How does Win7 set the mouse arrow color?
After upgrading win10, how do I set the start menu to the original habit?
Let Windows XP desktop text disappear
Two ways to easily enter scores in word
Win8 start screen map route function introduction
Win7 how to modify the file default open mode
What should I do if the entire screen color of my computer turns blue?