0

Say that I have a file with a list of packages I want to install, separated by spaces or newlines. How can I pass that file to my package manager?

3 Answers 3

3

Try xargs command, for example:

cat file | xargs ls

Or

 cat file | xargs gzip -c
1

Read the file into a an array:

token=($(<list.txt))
yourCommand ${token[@]}

If the tokens are filenames, containing spaces and separated by newlines, this will not work, because tokens are usually separated by whitespace in the shell.

1
  • 1
    “If the tokens are filenames, containing spaces and separated by newlines”, then you have to set IFS. pastebin.com/1yFufEq4
    – manatwork
    Jul 23, 2013 at 15:30
1

Assuming that the package names don't contain any wildcard characters (i.e. none of \[?*), you can use a command substitution:

install-packages $(cat list-of-packages.txt)

Assuming that the package names don't contain any of the characters \"', you can use xargs. Note that this redirects the input of the install-packages command from the package list, so it may not work if the installed requires some interactive input.

xargs install-packages <list-of-packages.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .