假设我有一个以confirmation.sh以下内容命名的文件:
#!/bin/bash
echo -n "Are you sure [Y/n]? "
read line
case "$line" in
n|N) echo "smth"
;;
y|Y) echo "smth"
;;
esac
Run Code Online (Sandbox Code Playgroud)
我想通过以下方式运行这个脚本:
cat confirmation.sh | sh
Run Code Online (Sandbox Code Playgroud)
我看到Are you sure [Y/n]?了,剧本被打断了。有什么问题?
我需要通过bash使用管道来运行脚本wget(而不是直接使用 bash 运行它)。
$ wget -O - http://example.com/my-script.sh | bash
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为我的脚本中有read语句。出于某种原因,这些在管道到 bash 时不起作用:
# Piping to bash works in general
$ echo 'hi'
hi
$ echo "echo 'hi'" | bash
hi
# `read` works directly
$ read -p "input: " var
input: <prompt>
# But not when piping - returns immediately
$ echo 'read -p "input: " var' | bash
$
Run Code Online (Sandbox Code Playgroud)
取而代之的提示input:和要求的值,因为它应该读命令只是获取由经过bash。
有谁知道我如何用readto管道脚本bash?