我有这个函数,我需要它引用一个使用GNU parallel的函数的多个参数。

foo () {

    cd ${HOME}/sh/xxx/xxx/xxx/folder_with_scripts
    bash -H $1 #replace with echo in test run {echo $1 is being echoed}
    bash -H $2 #replace with echo in test run {echo $2 is being echoed}
}

export -f foo
parallel foo ::: *script.sh bash*.sh

包含脚本内容的文件夹
$ ls
firstscript.sh
secondscript.sh
thirdscript.sh
bashhim.sh
bashscript.sh
bashher.sh

parallel foo基本上通过将其作为*script.sh内的参数引用来执行foo内的所有脚本。即$1。我要做的是让它也通过使用bash*.sh*来执行folders_with_scripts目录中的$2
根据man parallel,语法为:
 parallel [options] [command [arguments]] ( ::: arguments | :::: argfile(s) )

因为::: arguments是复数,所以我假设这是非常可能的。
结果
为了方便起见,我将bash替换为echo
$ ./foo.sh
firstscript.sh is being echoed
secondscript.sh is being echoed
thirdscript.sh is being echoed
              is being echoed
              is being echoed
              is being echoed

想要的结果
firstscript.sh is being echoed
secondscript.sh is being echoed
thirdscript.sh is being echoed
bashhim.sh is being echoed
bashscript.sh is being echoed
bashher.sh is being echoed

就我而言,parallel foo ::: *.sh不会切断它。
$1的位置与*script.sh的位置交换不会切断它

最佳答案

你的问题不包含MVCEhttps://stackoverflow.com/help/mcve特别是我不能直接在我的系统上运行你的代码。所以这个答案是基于我对你的猜测。
假设要为每个bash*.sh运行所有*script.sh

parallel foo ::: *script.sh ::: bash*.sh

如果每个bash*.sh链接到*script.sh:::+在版本20160422中引入):
parallel foo ::: *script.sh :::+ bash*.sh

--dry-run测试:
parallel --dry-run foo ::: *script.sh ::: bash*.sh
parallel --dry-run foo ::: *script.sh :::+ bash*.sh

如果这不能回答您的问题,请用您希望GNU并行运行的命令更新问题,例如:
foo firstscript.sh bashhim.sh
foo secondscript.sh thirdscript.sh
foo bashscript.sh bashher.sh

并在运行echo *script.sh bash*.sh的目录中显示parallel的输出。
:::后面的数据称为输入源。
有关输入源的处理,请参见https://zenodo.org/record/1146014的第4章(印刷体:http://www.lulu.com/shop/ole-tange/gnu-parallel-2018/paperback/product-23558902.html

关于bash - 并行:如何从一个函数引用多个参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50636075/

10-14 04:42