您的位置:首页 > 其它

read命令简介

2009-12-17 21:29 309 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: