在“同时读取行”之后读取未评估(SHELL)

时间:2018-12-05 18:16:13

标签: bash shell sh

我当前正在尝试调试一些我编写的脚本,但我找不到执行“读取”指令的方法。

总而言之,我有两个函数,一个具有在管道后调用的“ while读取行”,另一个函数在处理读取后读取用户输入。

现在让我用代码解释一下:

  • 这就是我调用函数的方式($ lines包含多行,用'\ n'分隔)

回显“ $ lines” | saveLines

saveLines(){
  # ...
  while read line ; do
    # processing lines
  done
  myOtherFunction
}

myOtherFunction(){
  echo "I am here !" # <= This is printed in console
  read -p "Type in : " tmp  # <= Input is never asked to user, and the message is not printed
  echo "I now am here !" # <= This is printed in console
}

此代码已简化,但其实质在这里。 我试图在“ read -p ...”之前插入一条“ read”指令,但似乎并没有改变任何事情……

因此,如果您可以显示我的错误或告诉我为什么会出现这种现象,请您感到非常高兴。谢谢你的时间

1 个答案:

答案 0 :(得分:1)

在稍微不同的情况下,这个问题非常接近that other question。更准确地说,如OP所述,命令运行为

echo "$lines" | saveLines

表示saveLines执行的代码的标准输入不再是终端,而是与echo...命令的标准输出相同的描述符。

要解决此问题,只需更换

…
read -p "Type in : " tmp
…

使用

…
read -p "Type in : " tmp </dev/tty
…