Bash 将文件名列表映射为命令的选项值
我有许多文件,希望使用它们的名称作为命令的参数,以便命令成为Bash 将文件名列表映射为命令的选项值,bash,shell,dictionary,options,Bash,Shell,Dictionary,Options,我有许多文件,希望使用它们的名称作为命令的参数,以便命令成为 <command> <option> <file1> <option> <file2> ... 但是,我想要的不是xargs所做的(将其转换为每个文件的一个命令),而是一个包含多个参数的命令,在所有文件名之前插入 在我的特定示例中,我希望将未知数量的覆盖率数据文件与一个命令组合: lcov -o total.coverage -a <file1> -a <
<command> <option> <file1> <option> <file2> ...
但是,我想要的不是xargs
所做的(将其转换为每个文件的一个命令),而是一个包含多个参数的命令,在所有文件名之前插入
在我的特定示例中,我希望将未知数量的覆盖率数据文件与一个命令组合:
lcov -o total.coverage -a <file1> -a <file2> ...
lcov-o总覆盖率-a-a。。。
这在Makefile中,但我更喜欢“标准”shell方法。您可以:
ls <pattern> |sed "s/^/-optionItself /g"|xargs
ls|sed“s/^/-optionItself/g”xargs
因此,在每个文件名前面附加-optionItself
,然后将其发送到xargs
ls -1q <pattern> | xargs <command> ...
我相信还有很多其他方法可以实现这一点,这是最简单、最接近您的工作方式。您可以:
ls <pattern> |sed "s/^/-optionItself /g"|xargs
ls|sed“s/^/-optionItself/g”xargs
因此,在每个文件名前面附加-optionItself
,然后将其发送到xargs
ls -1q <pattern> | xargs <command> ...
我相信还有很多其他方法可以实现这一点,这是最简单、最接近您的工作方式。也许是这样
function multi_lcov(){
files_params=($@)
lcov ${files_params[@]}
}
multi_lcov "$(ls | sed -r 's/(.*)/\-a \1/g')"
也许是这个
function multi_lcov(){
files_params=($@)
lcov ${files_params[@]}
}
multi_lcov "$(ls | sed -r 's/(.*)/\-a \1/g')"
试试这个:
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
假设文件名中没有特殊字符(如空格)。请尝试以下操作:
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
假设文件名中没有特殊字符(如空格)。在查找文件时也打印选项,使用零分隔流处理所有可能的文件名:
find . -maxdepth 1 -mindepth 1 -type f -printf "-a\0%p\0" | xargs -0 lcov -o total.coverage
与用作列表元素分隔符的换行符相同:
find . -maxdepth 1 -mindepth 1 -type f -printf "-a\n%p\n" | xargs -d '\n' lcov -o total.coverage
在查找文件并打印选项的同时,使用零分隔流来处理所有可能的文件名:
find . -maxdepth 1 -mindepth 1 -type f -printf "-a\0%p\0" | xargs -0 lcov -o total.coverage
与用作列表元素分隔符的换行符相同:
find . -maxdepth 1 -mindepth 1 -type f -printf "-a\n%p\n" | xargs -d '\n' lcov -o total.coverage
列表来自哪里?
我有文件名列表
文件名是如何存储的?在bash数组中?在文件中?在变量中?在make变量中?在bash变量中?变量中的条目是否用换行符分隔?它们是零终止的吗?是其他人干的?请发一些代码<代码>bash/shell是否有类似于map的东西?是的,bash有关联arary..它们不是来自任何地方。编辑以更清楚地了解这一点。文件存在,因此我将使用ls
获取名称,然后至少使用ls-1
。(ls-1q
)列表来自哪里?我有文件名列表
文件名是如何存储的?在bash数组中?在文件中?在变量中?在make变量中?在bash变量中?变量中的条目是否用换行符分隔?它们是零终止的吗?是其他人干的?请发一些代码<代码>bash/shell是否有类似于map的东西?是的,bash有关联arary..它们不是来自任何地方。编辑以更清楚地了解这一点。文件存在,因此我将使用ls
获取名称,然后至少使用ls-1
。(ls-1q
)完美!如果你再解释一点,我就接受你的回答。当然,没有在Makefile中工作(bash
而不是sh
,后者是make
使用的标准shell)。但因为我想要一个通用的解决方案,它仍然是一个候选方案;-)我想你可以在你的Makefile中重新定义SHELL。是的,但我不想这么做。太好了!如果你再解释一点,我就接受你的回答。当然,没有在Makefile中工作(bash
而不是sh
,后者是make
使用的标准shell)。但因为我想要一个通用的解决方案,它仍然是一个候选方案;-)我想你可以在你的Makefile中重新定义SHELL。是的,但我不想这样做。这也很好。它是标准的shell,在Makefile中工作。这也很好。它是标准的shell,在Makefile中工作。-printf“%P\n”| xargs-0
毫无意义。只是printf“%P\n”
-printf“%P\n”| xargs-0
没有任何意义。只需打印“%P\n”。