linux - 由于 While,读取命令无法按预期工作

标签 linux bash macos shell unix

<分区>

我遇到了一个奇怪的错误,

我有一个包含一些输入的文件,根据这些输入,我要求用户确认并继续执行操作(如下所示),

#!/bin/bash

func() {
cat testing | while read line
do
  if [ $line = "hi" ]
  then
    while true
    do
      read -p "[y/n] : " c
      case $c in
        [nN] ) return 1;;
        [yY] ) return 0;;
        * ) echo "Enter y/Y or n/N";;
      esac
    done
  fi
  echo "HELLO !!"
done
}

func

由于函数中的 while read line,实际的 read -p "[y/n] : " 从上面的 testing 文件而不是 STDIN 中选择输入。

如果删除 cat 测试,程序工作正常 | while read line 循环(这显然不是我想要的)。

有没有人遇到过这个问题或者有人可以帮我解决这个问题吗?

提前致谢!

最佳答案

使用不同的文件描述符(不是管道)进行测试

while IFS= read -r line <&3; do
    ...
done 3< testing

关于linux - 由于 While,读取命令无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028224/

相关文章:

linux - linux内核中的大尺寸kmalloc kmalloc

linux - GCC 错误消息 "Error: unsupported for ` mov'"是什么意思?

linux - 如何在 bash 中对文本文件行进行 block 排序?

python - Crontab 在重新启动时不运行 Python 脚本

node.js - Express 命令在终端中不起作用

c++ - 使用 `libopencv_ffmpeg.so` 在 Linux 上构建 OpenCV 2.4.11

linux - 为什么我的arp表总是只有路由器的信息?

bash - 我怎样才能正确地将多个 bash 脚本源到彼此?

java - 错误: Could not find or load main class Mac Eclipse

macos - 如何获得半透明模糊背景的窗口