相关疑难解决方法(0)

如何从管道读取用户输入?

假设我有一个以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]?了,剧本被打断了。有什么问题?

shell pipe shell-script user-input

10
推荐指数
1
解决办法
1万
查看次数

将带有“读取”的脚本传送到 bash

我需要通过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

shell bash pipe read

10
推荐指数
1
解决办法
7036
查看次数

标签 统计

pipe ×2

shell ×2

bash ×1

read ×1

shell-script ×1

user-input ×1