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
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“If the tokens are filenames, containing spaces and separated by newlines”, then you have to set
IFS. pastebin.com/1yFufEq4manatwork– manatwork2013-07-23 15:30:59 +00:00Commented Jul 23, 2013 at 15:30
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