Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1188
  • Last Modified:

How can i write a script to call many 'apt-get install' functions?

I need to install many dependencies on a few machines, of which i know the dependency names, and i really dont want to have to write these out each time.

How can i write a script to run these for me?

i.e. to do 'apt-get install this package' then 'apt-get install this package2' etc...

0
kerpoise
Asked:
kerpoise
  • 2
3 Solutions
 
MorcalavinCommented:
Just create a file called install_script.sh(or whatever you want) and put the follwing code in it(assuming you use sudo):
sudo apt-get install package1
sudo apt-get install package2
sudo apt-get install package3
..etc..

Save the file, and give it execute permissions: chmod u+x install_script.sh

The when you want to run the script, use:
./install_script.sh

You will still be prompted for a password if using sudo.
0
 
ixtiCommented:
Why do you want to call apt-get install for each package?
apt-get allows you to install all necessary packages in one pass, so you can give all their names to it.
For example, to install Apache2 and it's documentation, you can do it like this:
    $ sudo apt-get install apache2 apache2-doc

Or, for example, you want to install all packages which names are started with apache2,
so you can simply do it using regexp, like this:
    $ sudo apt-get install '^apache2'

So you can make a script like this:
    #!/bin/sh
    sudo apt-get install package1 package2 ...
and giving it an execution permissions like Morcalavin wrote above.

PS Also you can do it simply by creating a simple text file, with packages names separated
with space, just for example, call it 'list' and put all names of necessary packages there.
For example it can be done like this:
    $ echo package1 package2 > list
Or you can do it using your favourite text editor.
Then you can use this file like a packages list for apt-get, for example, like this:
    $ apt_list=$(cat /path/to/list) && sudo apt-get install $apt_list

And finally, you can easily write a simple script which will be doing everything automaticaly.
For example like this:
    #!/bin/sh
    aptget='/usr/bin/apt-get'
    if [ ! -e $1 ]; then
        exit
    fi
    aptlist=$(cat $1)
    $aptget install $aptlist
Then you have to make it executable, and can call it like this:
$ ./installer.sh list.txt
0
 
FilipeMaiaCommented:
I suggest adding -y to the apt-get command line to answer yes to most questions automatically. Very useful when not running interactively.
0
 
ixtiCommented:
In addition to -y option suggested by FilipeMaia I suggest you also to add -q option if it's used in automation mode, so it will produce output suiteable for logging.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now