Bash 如何在while read循环中提示用户?

Bash 如何在while read循环中提示用户?,bash,Bash,出于我的目的,我需要执行一个shell命令,实现输出,并为每一行请求用户提示。 问题是在读取提示上,stdinbuffer不是空的 这是我的代码: #!/bin/sh git branch -a | sed 's/remotes\/origin\///g' echo "############################" git branch -a | sed 's/remotes\/origin\///g' | while read line do if [[ "$

出于我的目的,我需要执行一个shell命令,实现输出,并为每一行请求用户提示。 问题是在读取提示上,stdinbuffer不是空的

这是我的代码:

#!/bin/sh

git branch -a | sed 's/remotes\/origin\///g'

echo "############################"

git branch -a | sed 's/remotes\/origin\///g' | while read line
do
        if [[ "$line" != *develop* ]] \
                && [[ "$line" != *master ]] \
                && [[ "$line" != *release/* ]] \
                && [[ "$line" != *hotfix* ]]
        then
                read -r -p "Do you want to delete branch $line <y/N>?" prompt
                echo $prompt
        fi
done
#/垃圾箱/垃圾箱
git分支-a | sed's/remotes\/origin\///g'
回声
git分支-a | sed's/remotes\/origin\///g'|在读取行时
做
如果[[“$line”!=*开发*]]\
&&[[“$line”!=*master]]\
&&[[“$line”!=*发布/*]]\
&&[[“$line”!=*修补程序*]]
然后
阅读-r-p“是否要删除分支$line?”提示
echo$prompt
fi
完成
该行:

read-r-p“是否要删除分支$line?”提示

甚至不显示到视频,并提示变量显示以上行变量的结果。 如何解决此问题?

使用0以外的FD(标准输入法),使原始标准输入法不受用户输入的限制:

#!/usr/bin/env bash
#              ^^^^- NOT /bin/sh; also, do not run with "sh scriptname"

while read -r line <&3; do
  line=${line#remotes/origin/}  # trim remotes/origin/ w/o needing sed
  case $line in
    *develop*|*master|*release/*|*hotfix*) continue ;;
    *) read -r -p "Do you want to delete branch $line <y/N>?" prompt
       echo "$prompt" ;;
  esac
done 3< <(git branch -a)
#/usr/bin/env bash
#^^^^-非/bin/sh;另外,不要使用“sh scriptname”运行

read-r line(标记community wiki以避免从已知副本获取rep)在意外标记'3'@fstarred附近的最后一行语法错误上收到此错误时,您的shell是什么<代码>…也就是说,在运行过程中,我确实错过了关闭
案例的
esac
。哎呀。:)工作就像一个魅力!!!非常感谢你,伙计