免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 18107 | 回复: 21
打印 上一主题 下一主题

研讨:Bash 内建命令 read  关闭 [复制链接]

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-01-05 23:05 |只看该作者 |倒序浏览
这是在"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 #在这试试命令历史和补齐功能
复制代码

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

论坛徽章:
0
2 [报告]
发表于 2004-01-05 23:41 |只看该作者

研讨:Bash 内建命令 read

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

论坛徽章:
0
3 [报告]
发表于 2004-01-05 23:46 |只看该作者

研讨: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

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

论坛徽章:
0
4 [报告]
发表于 2004-01-05 23:46 |只看该作者

研讨:Bash 内建命令 read

偶机器里还没bash哩

论坛徽章:
0
5 [报告]
发表于 2004-01-05 23:48 |只看该作者

研讨:Bash 内建命令 read

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

我的机器里面没有ZSH,

论坛徽章:
0
6 [报告]
发表于 2004-01-05 23:55 |只看该作者

研讨:Bash 内建命令 read

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


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

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

论坛徽章:
0
7 [报告]
发表于 2004-01-06 00:10 |只看该作者

研讨:Bash 内建命令 read

原帖由 "c1l2d3" 发表:


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

灌水的隐又犯了,向版主道歉。
到 rpmfind.net 上搜索吧~~,什么shell都有,就是没有石油shell

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2004-01-06 03:02 |只看该作者

研讨:Bash 内建命令 read

加灌:

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

论坛徽章:
0
9 [报告]
发表于 2004-01-06 03:06 |只看该作者

研讨:Bash 内建命令 read

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

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

论坛徽章:
0
10 [报告]
发表于 2004-01-06 08:43 |只看该作者

研讨:Bash 内建命令 read

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


      
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP