linux - 使用 bash 自动安装脚本

标签 linux bash

我正在尝试编写以下脚本安装,如何在命令中的提示符下回答“y”

 wget -O - mic.raspiaudio.com | sudo bash

我已经尝试了通常的方法,但这行不通

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

最佳答案

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

我相信echo对此不起作用,因为它没有写入 /dev/ttybash产生。您可以使用默认功能 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.

如果您使用-c使用 bash 选项,您可以向将运行的脚本提供参数,这些参数将按照手册页中提到的方式放置。例如: bash -c "script" "arg0" "arg1" ...arg0将被放置在 $0arg1将被放置在 $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
}

并用作:

if confirm "Do you wish to continue"
then
  echo "You are good to go"
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)"
Do you wish to continue [y/N] y
You are good to go

关于linux - 使用 bash 自动安装脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235374/

相关文章:

c - 监听两个不同套接字接口(interface)的服务器程序

c - 之间有什么区别。和./在bash中?

python - Bash 或 GoogleCL : new line in a string parameter

python - 从文件中提取单词,然后列出文件以及包含这些单词的行号

file - 如何用另一个文件替换目录中所有文件的内容?

macos - 覆盖 bash PS1 不起作用

java - 如何使用 Java 获取 chromedriver 进程 PID?

c - 餐饮哲学家的变化

c++ - 在 Linux 中使用硬件性能计数器

c++ - linux 发送带有标志 MSG_DONTWAIT 的调用