首页 > shell读取文件的时候,能否读取控制台的输入

shell读取文件的时候,能否读取控制台的输入

比如

#!/bin/bash
flag=true
cat demo.txt | while read line
do
    echo $line
    echo "please input flag"
    read flag
done

问题1:是不是在读取文件内容的时候,不能在这个循环中再读取控制台的输入?
问题2:有没有什么方法能够实现这个功能?


因为你读取的都是标准输入,当然不能这样。。

#!/bin/bash
flag=true
exec 3<demo.txt # 3 是 fd 
cat <&3 | while read line
do
    echo $line
    echo "please input flag"
    read -u 1 flag
    echo $flag;
    if [ ! $flag == "true" ];then
        exit;
    fi
done

这样子能实现你要的效果

【热门文章】
【热门文章】