bbs.ChinaUnix.net
首页 | 微博 | 新闻 | Linux | FreeBSD | AIX | Windows | 博客 | 论坛 | 存储 | 网络 | 人才 | Wiki | 资料 | 读书 | 手册 | 下载 | 空间 | 搜索
  免费注册 | 忘记密码 | 会员登录 | 搜索 | 帮助 


精彩推荐帖子 研讨:Bash 内建命令 read

  首页 » 论坛 » Shell »
[打印] [收藏] [本帖文本页] [推荐此主题给朋友]

这是在"man bash"中的一段
  1. read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
  2.     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:
  3.     -a aname
  4.     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.
  5.     -d delim
  6.     The first character of delim is  used  to  terminate  the input line, rather than newline.
  7.     -e     If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line.
  8.     -n nchars
  9.     read returns after reading nchars characters rather  than waiting for a complete line of input.
  10.     -p prompt
  11.     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.
  12.    -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.
  13.    -s     Silent mode.  If input is coming from a terminal, characters are not echoed.
  14.    -t timeout
  15.    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.
  16.     -u fd  Read input from file descriptor fd.

  17.     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.
复制代码

经测试发现许多shell资料中没有介绍过的新内容,或许地球人都知道了^_^,不管怎样希望有兴趣的一起琢磨一下。
以下是我用来测试的代码,结果已经略去,请大家自行测试。
  1. read -p "how old r u? " age
  2. echo $age
  3. read -p "some words? " -a words
  4. echo ${words[*]}
  5. read -p "Password: " -s passwd                                 
  6. echo $passwd
  7. read -t 5 auth
  8. echo $auth
  9. read -n 1 key
  10. read -dq -p "input something end with q: " menu
  11. read -e file #在这试试命令历史和补齐功能
复制代码

有兴趣的,别忘了跟贴^_^



研讨:Bash 内建命令 read

bash的read选项比ksh的多两个 ,而且没有-t :(
__________________________________
/



研讨:Bash 内建命令 read

我认为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

继续跟呀~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__________________________________
/



研讨:Bash 内建命令 read

偶机器里还没bash哩 :em23:
__________________________________



研讨:Bash 内建命令 read

[quote]原帖由 "c1l2d3"]偶机器里还没bash哩 :em23:[/quote 发表:

我的机器里面没有ZSH,;)
__________________________________
/



研讨:Bash 内建命令 read

[quote]原帖由 "bjgirl"]我的机器里面没有ZSH,;)[/quote 发表:


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

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



研讨:Bash 内建命令 read



QUOTE:
原帖由 "c1l2d3" 发表:


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

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

到 rpmfind.net 上搜索吧~~,什么shell都有,就是没有石油shell ;)
__________________________________
/



研讨:Bash 内建命令 read

加灌:

bjgirl MM 變臉了哦...  ^_^
嗯! 這張比上一張可愛多了... 簽名也更有詩意了!  ^_^
__________________________________
君子博學而日參省乎己,則知明而行無過矣。

:() { :|:& }; :      # <--- 這個別亂跑!好奇會死人的!
echo '十人|日一|十十o' | sed 's/.../&\n/g'   # <--- 跟你講就不聽,再跑這個好了...



研讨:Bash 内建命令 read



QUOTE:
原帖由 "網中人" 发表:
加灌:

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

这回迷人不 :?: :!: :em09: :oops:
__________________________________
/



研讨:Bash 内建命令 read

[quote]原帖由 "bjgirl"]这回迷人不 :?: :!: :em09: :oops:[/quote 发表:


:em09:  :em09:  :em09:  :em09:
__________________________________
累了,
无语,
喝着啤酒,
发呆......


Copyright © 2001-2010 ChinaUnix.net All Rights Reserved     联系我们:

感谢所有关心和支持过ChinaUnix的朋友们    转载本站内容请注明原作者名及出处

京ICP证041476号


清除 Cookies - ChinaUnix - Archiver - WAP - TOP