7

I want to write a bash or perl script to install a number of packages on my debian based machine.

I want it to be something like :

aptitude install package1 
aptitude install package2

But, I do not know how how to automatically say "yes" through the script at the prompt to confirm you want to install that package. Can someone give me an example in perl and bash ?

gratz!

Lucas Kauffman
  • 16,818
  • 9
  • 57
  • 92

4 Answers4

26

aptitude install -y package1 package2 package3

Jure1873
  • 3,692
  • 1
  • 21
  • 28
14

I guess this isn't really a script, but you could install puppet and use the package type. Then it will simply be a matter of creating a file with a bunch of package directives.

For example:

package { zsh:
        ensure => latest
}

package { vim-enhanced:
        ensure => latest,
        name => $operatingsystem ? {
                'centos' => 'vim-enhanced',
                'ubuntu' => 'vim',
        }

}

You could also use puppet to customize a bunch of other things like your shell, and your .rc files etc if you get in the mood to extend your scope. It might sound like overkill, but in the long run the little bit of extra effort will likely pay off.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
10

In addition to using the -y flag for yes answers you will also want to set DEBIAN_FRONTEND=noninteractive to prevent debconf setting questions from popping up.

DEBIAN_FRONTEND=noninteractive apt-get install PACKAGE -y

That being said, it might be a better road in the long run to follow Kyle's suggestion and take a look at a configuration management system such as Puppet.

andol
  • 6,848
  • 28
  • 43
1

My answer is a slightly wider scope than the original question, but if you want to do this repeatedly, from scratch, across multiple machines you are setting up then I'd recommend spending the time looking into pxeboot. (I'm assuming you are a debian derivative mentioning aptitude). The learning curve is steep, the documentation a bit patchy but worth persisting if you need it.

If you have to do machines like this on a semi regular basis it's a no brainer. We have machines build from blank to fully configured in around 8 minutes with a ton of custom packages, auto installed.

For bonus points, look into some sort of apt caching mechanism as well.

A perfectly good setup is to make a build server / apt cache out of a virtual machine image to avoid polluting an existing machine

Aitch
  • 1,179
  • 8
  • 14