免费注册 查看新帖 |

Chinaunix

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

[文本处理] 求助while read line循环中,调用含有read的脚本问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-25 17:42 |只看该作者 |倒序浏览
本帖最后由 zjxapple 于 2015-08-25 17:42 编辑

1.sh内容
  1. #/bin/bash
  2. while read line
  3. do
  4.     exec ./2.sh
  5. done < file
复制代码
file内容:
1
22
333

2.sh内容:
  1. #!/bin/bash
  2. read -p "input something:" sth
  3. echo $sth
复制代码
运行脚本1.sh得到的结果为:
22

请问各位大神,为什么不提示“input something:”而是直接输出了file的第二行呢?

论坛徽章:
0
2 [报告]
发表于 2015-08-25 19:36 |只看该作者
$ man read
    -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.

$ man exec
    "If  command  is  specified,  it replaces the shell.  No new process is created.  "

论坛徽章:
0
3 [报告]
发表于 2015-08-25 20:15 |只看该作者
本帖最后由 zjxapple 于 2015-08-25 20:24 编辑

回复 2# guofzhao


谢谢!

还有一点没有明白,1.sh为什么会直接把参数传到2.sh呢,如果想直接调用2.sh接收用户的输入要怎么做?

关于exec略过第一行的
while read line
do
echo $line
sed 's/.*/sed eat the line ( & )/'
done < file

-p的问题我也明白了


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP