Chinaunix

标题: 求助while read line循环中,调用含有read的脚本问题 [打印本页]

作者: zjxapple    时间: 2015-08-25 17:42
标题: 求助while read line循环中,调用含有read的脚本问题
本帖最后由 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的第二行呢?
作者: guofzhao    时间: 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.  "
作者: zjxapple    时间: 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的问题我也明白了







欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2