read命令
  read命令是一个内置命令,用于从终端或文件读取输入,read命令读取一个输入行,直至遇到换行符。行尾的换行符在读入时被转换成一个空字符。如果read命令后未跟变量名,读入的行将被赋给内置变量REPLY。
你也可以使用read命令来中断程序的运行,直至用户输入一个回车。
  如果代-r选项,read命令将忽略反斜杠/新行符对,把反斜杠作为行的一部分。

 

表14-1 read命令

格    式

含    义

read answer

从标准输入读取一行并赋值给变量answer

read first last

从标准输入读取一行,直至遇到第一个空白符或换行符。把用户键入的第一个词存到变量first中,把该行的剩余部分保存到变量last中

read

标准输入读取一行并赋值给内置变量REPLY

read –a arrayname

读入一组词,依次赋值给数组arrayname③

                                        (续表)  

格    式

含    义

read -e

在交互式shell命令行中启用编辑器。例如,如果编辑器是vi,则可以在输入行时使用vi命令

read –p prompt

打印提示符,等待输入,并将输入赋值给REPLY变量

read –r line

允许输入包含反斜杠

 

 

#!/bin/bash
#scriptname: nosy
echo -e "are you happy? /c"
read answer
echo "#answer is the right response."
echo -e "what is your full name? /c"
read first middle last
echo "Hello $first"
echo -n "where do you work? "
read
echo Iguess $REPLY keeps you busy!
read -p "enter your job title: "
echo "I thought you might be an $REPLY."

echo -n "who are your best friends? "
read -a friends
echo "Say hi to ${friends[2]}."

The results:
are you happy? never too happy
never too happy is the right response.
what is your full name? tu you wu
Hello tu
where do you work? NUAA
I guess NUAA keeps you busy!
enter your job title: Com
I thought you might be an Com.
who are your best friends? sj cjs lj
Say hi to lj.