运行bash脚本时自动确认?

dan*_*mcc 15 apt bash scripts automation

我正在编写一个简单的 bash 脚本,该脚本可以按照我的需要配置新服务器。这没什么特别的,但它有望在未来为我节省很多时间。

如何防止出现这样的提示:

  You are about to add the following PPA to your system:
    Stable version of nginx.
    More info: https://launchpad.net/~nginx/+archive/stable
  Press [ENTER] to continue or ctrl-c to cancel adding it
Run Code Online (Sandbox Code Playgroud)

从脚本运行这样的命令时:

  sudo add-apt-repository ppa:nginx/stable 
  sudo apt-get update
  sudo apt-get -y install nginx
Run Code Online (Sandbox Code Playgroud)

是否有我可以设置的自动接受标志或类似的东西?

Den*_*ker 31

add-apt-repository有一个-y您可以使用的标志,它应该可以解决问题。

虽然我真的想建议您查看像 puppet 或 Chef 这样的配置管理系统来配置服务器。他们会为你节省很多时间!


小智 15

可能值得注意的是,这就是yes编写的目的。默认情况下y,它yes <string>会重复打印 a ,但只会重复“字符串”。

如果您看到一堆回复,例如:

Press [ENTER] to continue or ctrl-c to cancel adding it

您可以执行以下操作:

yes '' | sudo add-apt-repository ppa:nginx/stable
Run Code Online (Sandbox Code Playgroud)

在每次提示时自动输入。鉴于 apt-add-repository 有一个-y选项,您应该使用它,但是如果您遇到没有该选项的脚本,您可以使用yes.