运行bash脚本时自动确认?

我正在开发一个简单的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 

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

  sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get -y install nginx 

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

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

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

值得注意的是,这就是为之所写的。 默认情况下,它会重复打印y ,但是yes 将只重复“string”。

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

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

你可以这样做:

 yes '' | sudo add-apt-repository ppa:nginx/stable 

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