在函数/管道If语句中提示用户输入-Bash

在函数/管道If语句中提示用户输入-Bash,bash,if-statement,pipe,Bash,If Statement,Pipe,在bash中调用函数时是否可以提示用户输入 以此为例: #!/bin/bash test1(){ echo "Do you wish to install this program?" select yn in "Yes" "No"; do case $yn in Yes ) make install; break;; No ) exit;; esac done } pip list 2>/dev/null | if grep http

在bash中调用函数时是否可以提示用户输入

以此为例:

#!/bin/bash
test1(){

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make install; break;;
        No ) exit;;
    esac
done

}

pip list 2>/dev/null | if grep httplib2; then echo 2>/dev/null; else test1; fi
pip list 2>/dev/null | if grep httplib2; then echo 2>/dev/null; else test1 </dev/tty; fi
exec 3<&0; echo http | test1 <&3; exec 3<&-
忽略我正在检查httplib2的事实,因为我知道如果您响应“Test”,那么它可以正常工作。我已经尝试过stackoverflow和tldp的例子,所以我现在有点困惑

您不能从管道if语句捕获用户输入吗

刚刚测试过

pip list 2>/dev/null | if grep httplib2; then 
echo 2>/dev/null; 
else
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year
fi

也有相同的效果。

如果
test1
在管道中,但您需要从终端输入,请使用:

test1 </dev/tty
或者


exec 3/dev/null |如果grep httplib2;然后echo 2>/dev/null;else test1如果我使用
exec 3I认为“If grep”从stdout获得了输入,那么语句的其余部分就是它是真是假?@这一点起初似乎合理,但
If then else
构造是一个复合命令。如果将stdin重定向到复合命令,则除非我们显式更改它,否则复合命令中的每个语句都会看到重定向的stdin。@User112638726“断管”表示
test1
echo
之前完成。在这种情况下,这是一条无害的消息。@更准确地说,当
test1
启动时,它会请求
pip list
进程进行输入。由于
pip列表
进程已经将它所拥有的一切发送到
grep
,因此什么都没有了。当
test1
中的
select
语句被告知没有可用的输入时,它退出。
exec 3<&0
pip list 2>/dev/null | if grep httplib2; then echo 2>/dev/null; else test1 <&3; fi
exec 3<&-