I have a number of files and want to use their names as arguments to a command so that the command becomes
<command> <option> <file1> <option> <file2> ...
For each file name I want to prepend that with the option name. I don't know how many files there are. How can I do this? Does bash/shell have something similar to map
?
The files exists so I would get the names using find
, or mayb ls
if I'm sure about the filenames, so I was looking for something like you can do with xargs
ls -1q <pattern> | xargs <command> ...
But instead of what xargs
do (turning it into one command for each file) I want a single command with many arguments with <option>
inserted before all filenames.
In my specific example I want to combine an unknown number of coverage data files with one command:
lcov -o total.coverage -a <file1> -a <file2> ...
This is inside a Makefile, but I'd prefer a "standard" shell approach.
Try this :
files=(pattern)
# This will expand the [pattern], and put all the files in [files] variables
lcov -o total.coverage ${files[@]/#/-a }
# ${files[@]/#/-a } replaces the beginning of each element in files with [-a ],
# meaning prepend [-a ]
# For more information, see section [${parameter/pattern/string}] in
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion
assuming you don't have special characters (like spaces) in your file names.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras