Linux 使用bash自动安装脚本

Linux 使用bash自动安装脚本,linux,bash,Linux,Bash,我正在尝试下面的脚本安装,如何在命令提示下回答“y” wget -O - mic.raspiaudio.com | sudo bash 我试过通常的方法,但不管用 echo "y" | wget -O - mic.raspiaudio.com | sudo bash 免责声明:下面的解决方案适用于具有非交互式开关的脚本 我相信echo在这方面不会起作用,因为它没有写入bash产生的/dev/tty。您可以使用bash提供的默认功能bash来完成 从手册页: -c If th

我正在尝试下面的脚本安装,如何在命令提示下回答“y”

 wget -O - mic.raspiaudio.com | sudo bash
我试过通常的方法,但不管用

echo "y" |  wget -O - mic.raspiaudio.com | sudo bash

免责声明:下面的解决方案适用于具有非交互式开关的脚本

我相信
echo
在这方面不会起作用,因为它没有写入
bash
产生的
/dev/tty
。您可以使用bash提供的默认功能
bash
来完成

从手册页:

-c        If the -c option is present, then commands are read from the first 
          non-option argument command_string.  If there are arguments after the
          command_string,  the  first argument is assigned to $0 and any remaining
          arguments are assigned to the positional parameters.
如果在bash中使用
-c
选项,则可以为将运行的脚本提供参数,这些参数将按照手册页中的说明放置。如:
bash-c“脚本”“arg0”“arg1”…
arg0
将放在
$0
中,
arg1
将放在
$1
中,依此类推

现在,我不知道这是否可以推广,但是这个解决方案只有在脚本中有一个非交互模式时才有效

如果您看到脚本,它具有以下功能:

FORCE=$1

confirm() {
    if [ "$FORCE" == '-y' ]; then
        true
    else
        read -r -p "$1 [y/N] " response < /dev/tty
        if [[ $response =~ ^(yes|y|Y)$ ]]; then
            true
        else
            false
        fi
    fi
}
因此,如果我们可以将$1设置为“-y”,则不会要求确认,我们将尝试通过以下方式执行此操作:

$ bash -c "$( wget -qO - mic.raspiaudio.com)" "dummy" "-y"
如果脚本没有任何其他交互选项,那么这应该适用于脚本。我没有用我自己的最小脚本测试原始脚本,它似乎可以工作。例如:

$ bash -c "$(wget -qO - localhost:8080/test.sh)" "dummy" -y
You are good to go

这可能是您正在寻找的,直接从远程源运行脚本会带来相当大的安全风险,尤其是使用根帐户时。请参阅:。总是喜欢单独下载、查看和运行。另见:
$ bash -c "$(wget -qO - localhost:8080/test.sh)" "dummy" -y
You are good to go
$ bash -c "$(wget -qO - localhost:8080/test.sh)"
Do you wish to continue [y/N] y
You are good to go