5

This looks good:

for i in package1 package2 package3; do
  sudo apt-get install -y $i
done

but with the packages listed in a file:

package1
package2
..

each on its own line. Looking for simplest script to read, performance not really an issue. Of course, the odd package will require some human intervention during install to agree or for configuration.

As an aside, what's the "real" way of dealing with large lists of packages to be installed? I'm just looking for monkey-see-monkey-do.

Thufir
  • 4,631

2 Answers2

7

There is the xargs program which transforms a file to command-line arguments. Simply prepend xargs to the command (with all arguments) for which you’d like to supply additional arguments from the file (let’s call it list.txt) and let xargs to read your file using standard input redirection.

< list.txt xargs sudo apt-get install -y

You can test it by putting echo before (or instead of) sudo or removing the -y option.

Melebius
  • 11,750
4

Something like this?

# check that the filename was supplied (keeping it simple for the example)
set -o nounset

packagefile=$1

# initialize the package variable
packages=''

# read the lines of the package file
while IFS= read -r line; do
    packs+=" $line"
done < $packagefile

# apt install all of the packages
apt install -y $packs
Eric Mintz
  • 2,536