发新话题
打印

研讨:Bash 内建命令 read

研讨:Bash 内建命令 read


这是在"man bash"中的一段
[code:1:7c7a1a56c3]read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
    One  line  is  read  from  the  standard input, or from the file descriptor fd supplied as an argument to the -u option, and  the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their  intervening  separators  assigned  to the last name.  If there are fewer words read from the input stream than names, the remaining names are  assigned  empty  values.  The characters in IFS are used to split the line into words.  The backslash character (\)  may  be used  to  remove any special meaning for the next character read and for line continuation.  Options, if supplied, have the  following meanings:
    -a aname
    The words are assigned to sequential indices of the array variable aname, starting at 0.  aname is unset before any new  values  are  assigned.   Other  name  arguments  are ignored.
    -d delim
    The first character of delim is  used  to  terminate  the input line, rather than newline.
    -e     If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line.
    -n nchars
    read returns after reading nchars characters rather  than waiting for a complete line of input.
    -p prompt
    Display prompt on standard error, without a trailing newline, before attempting to read any input.  The prompt is displayed only if input is coming from a terminal.
   -r     Backslash does not act as an escape character.  The backslash is considered to be part of the line.  In  particular,  a  backslash-newline pair may not be used as a line continuation.
   -s     Silent mode.  If input is coming from a terminal, characters are not echoed.
   -t timeout
   Cause  read  to time out and return failure if a complete line of input is not read within timeout  seconds.  This option  has  no  effect if read is not reading input from the terminal or a pipe.
    -u fd  Read input from file descriptor fd.

    If no names are supplied, the line read is assigned to the ariable  REPLY.   The  return  code  is zero, unless end-of-file is ncountered, read times out, or an invalid  file  descriptor  is supplied as the argument to -u.
[/code:1:7c7a1a56c3]
经测试发现许多shell资料中没有介绍过的新内容,或许地球人都知道了^_^,不管怎样希望有兴趣的一起琢磨一下。
以下是我用来测试的代码,结果已经略去,请大家自行测试。
[code:1:7c7a1a56c3]
read -p "how old r u? " age
echo $age
read -p "some words? " -a words
echo ${words[*]}
read -p "assword: " -s passwd                                 
echo $passwd 
read -t 5 auth
echo $auth
read -n 1 key
read -dq -p "input something end with q: " menu
read -e file #在这试试命令历史和补齐功能
[/code:1:7c7a1a56c3]
有兴趣的,别忘了跟贴^_^


 bjgirl 回复于:2004-01-05 23:41:54
bash的read选项比ksh的多两个 ,而且没有-t 


 bjgirl 回复于:2004-01-05 23:46:44
我认为read ?很好用,比echo "input:";read input...用简便!像这样:
read input?"input:"
echo $input

而且read a b c这样用也很实用:
echo -n "please year month day"
read a b c
echo $a $b $c

另外就是read读fd,比如常用的两个文件的拼接:
while read -u3 i && read -u4 j;do
echo $i $j
done 3<afile 4<bfile

继续跟呀~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


 c1l2d3 回复于:2004-01-05 23:46:51
偶机器里还没bash哩 :em23:


 bjgirl 回复于:2004-01-05 23:48:45
[quote:b4bb8dfe33="c1l2d3"]偶机器里还没bash哩 :em23:[/quote:b4bb8dfe33]
我的机器里面没有ZSH,;)


 c1l2d3 回复于:2004-01-05 23:55:24
[quote:9f4649eac7="bjgirl"]我的机器里面没有ZSH,;)[/quote:9f4649eac7]

ZSH啊!MM快发一个给我 :em11: 

灌水的隐又犯了,向版主道歉。 ops:


 bjgirl 回复于:2004-01-06 00:10:22
[quote:10ee4461a2="c1l2d3"]

ZSH啊!MM快发一个给我 :em11: 

灌水的隐又犯了,向版主道歉。 ops:[/quote:10ee4461a2]
到 rpmfind.net 上搜索吧~~,什么shell都有,就是没有石油shell ;)


 網中人 回复于:2004-01-06 03:02:32
加灌:

bjgirl MM 變臉了哦...  ^_^
嗯! 這張比上一張可愛多了... 簽名也更有詩意了!  ^_^


 bjgirl 回复于:2004-01-06 03:06:57
[quote:fd47d86022="網中人"]加灌:

bjgirl MM 變臉了哦...  ^_^
嗯! 這張比上一張可愛多了... 簽名也更有詩意了!  ^_^[/quote:fd47d86022]
这回迷人不 :?: :!: :em09: ops:


 路人甲 回复于:2004-01-06 08:43:58
[quote:14cc34da48="bjgirl"]这回迷人不 :?: :!: :em09: :oops:[/quote:14cc34da48]

 :em09:  :em09:  :em09:  :em09:


 c1l2d3 回复于:2004-01-06 10:52:21
没理由不在来一贴,这回bjgril变PL了


 r2007 回复于:2004-01-06 10:57:25
水贴到此为止。
[code:1:9f21f472ea]set echo off[/code:1:9f21f472ea]


 寂寞烈火 回复于:2004-11-01 01:32:32
好文章,收藏之 ;)


 james_dlc 回复于:2004-11-01 20:29:02
收到


 shaoping0330 回复于:2005-01-17 10:45:35
今天学到 用read拼接,高啊!


 YGC 回复于:2005-06-12 11:08:06
学习。。


 ywchen2000 回复于:2005-08-11 11:42:14
study


 heijude 回复于:2005-08-11 11:50:23
好水啊,我再浇一瓢,楼下的别在灌了啊!


 huhaiyan 回复于:2005-08-16 10:59:26
read -p "how old r u? " age 
我怎么用不行呀  它说-p是indentier


 yttlovezxx 回复于:2005-09-15 00:12:00
[quote:3303881755="bjgirl"]这回迷人不 :?: :!: :em09: :oops:[/quote:3303881755]

 :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04:  :em04: 我是单身!       :D

LUPA社区系统帐号.转贴文章之用.
发新话题