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,551

2 Answers2

6

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,431
  • 9
  • 52
  • 78
  • 1
    I am sorry, I don’t think there is any way to make this command simpler. Every character in this command has its own purpose. The only thing I can simplify is to write apt instead of apt-get. – Melebius Jan 02 '19 at 14:06
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,516
  • 12
  • 24
  • yes, perfect thanks. As an aside, is it possible or advisable to pipe apt search output through a script or utility? Assuming you know ahead of time that the result set is limited. You already answered the question I asked, it's just an aside. – Thufir Jan 02 '19 at 13:38
  • 1
    You can pipe the output but IMO that's really asking for it since you could get many matches that you didn't intend. I'd recommend outputing to a file first (like: apt search some-pack_name > packages) and then editing that file before doing the install. – Eric Mintz Jan 02 '19 at 13:57
  • It's the editing which is annoying. – Thufir Jan 02 '19 at 14:02
  • With bash, it would probably make more sense to slurp all the lines into an array and use that mapfile -t packs < "$packagefile" ; apt-get install -y "${packs[@]}" – steeldriver Jan 02 '19 at 15:28