免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2218 | 回复: 3

[文本处理] read 循环中套一个read获取键盘输入值问题 [复制链接]

论坛徽章:
0
发表于 2017-07-20 19:03 |显示全部楼层
  1. cat file1|while read line
  2. do
  3.    nvme id-ns $line
  4.    type_max=`nvme id-ns $line |tail -1 |awk '{print $2}'`
  5.   echo "please select format type from 0 to $type_max,your select type is :"
  6.   read ftype
  7.   nvme format $line -l $ftype -s 0
复制代码
其中file1内容为  /dev/nvme0 -n 1
/dev/nvme1  -n 1

其中nvme format 的指令是
nvme format /dev/nvme0  -n 1 -l 0 -s 0 是可以执行的。

出现的问题是执行read ftype没有被执行,ftype的变量没获取到,如何能让他执行呢?

我尝试了把echo和read改成
   read -t 10 -p "please select format type from 0 to $type_max,your select type is :" ftype
  都不行,也打印输出please xxx但就是没有让从键盘输入。


论坛徽章:
39
辰龙
日期:2013-08-21 15:45:192015亚冠之广州富力
日期:2015-05-12 16:34:52亥猪
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17双鱼座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亚冠之卡尔希纳萨夫
日期:2015-05-24 15:24:35黄金圣斗士
日期:2015-12-02 17:25:0815-16赛季CBA联赛之吉林
日期:2017-06-24 16:43:52
发表于 2017-07-20 19:17 |显示全部楼层
本帖最后由 关阴月飞 于 2017-07-20 19:32 编辑

回复 1# 刘彩霞
两个read  争抢一个 '标准输入', 就出现这个问题了:

问题也很好解决,就是让两个read各占一个输入源:
exec 3<file1
while read  -u 3  line
do
   nvme id-ns $line
   type_max=`nvme id-ns $line |tail -1 |awk '{print $2}'`
  echo "please select format type from 0 to $type_max,your select type is :"
  read ftype
  nvme format $line -l $ftype -s 0
done


之前   read line   <==  FD0(标准输入 <file )     read ftype  <== FD0(标准输入 <file )      
改后   read line   <==  FD3( <file )                         read ftype  <== FD1(标准输入)

论坛徽章:
0
发表于 2017-07-21 09:03 |显示全部楼层
本帖最后由 刘彩霞 于 2017-07-21 09:14 编辑

回复 2# 关阴月飞

谢谢。

论坛徽章:
0
发表于 2017-07-24 13:10 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP