【问题标题】:Using Xargs max-procs with multiple arguments from a file将 Xargs max-procs 与文件中的多个参数一起使用
【发布时间】:2017-12-17 06:11:23
【问题描述】:

我有一个脚本可以让我得到我想要的结果。我想提高脚本的性能。

我的脚本从文件 file1.txt 中获取参数。

内容如下:

table1
table2
table3
and so on

现在,当我使用下面的 while 语句时,脚本会按顺序运行。

while 声明如下:

while IFS=',' read -r a; do import.sh "$a"; done <  file1.txt

现在,当我在 bash 中使用 xargs max-procs 实用程序时,脚本会根据 max-procs 的数量并行运行。

xargs 声明如下:

xargs --max-procs 10 -n 1 sh import.sh <  file1.txt

现在我有另一个脚本

此脚本从文件 file2.txt 获取参数。

内容如下:

table1,db1
table2,db2
table3,db3
and so on

当我使用while 语句时,脚本运行良好。

while IFS=',' read -r a b; do test.sh "$a" "$b"; done <  file2.txt

但是当我使用 xargs 语句时,脚本会给我 usage 错误。

xargs 声明如下。

xargs --max-procs 10 -n 1 sh test.sh <  file2.txt

error 声明如下:

Usage : test.sh input_file

为什么会这样?

我该如何解决这个问题?

【问题讨论】:

  • 您可以通过在命令中添加echo 轻松调试此问题:xargs --max-procs 10 -n 1 echo sh test.sh &lt; file2.txt

标签: linux bash while-loop xargs


【解决方案1】:

您的第二个脚本test.sh 需要两个参数,但xargs 只提供一个(一个单词,在本例中是完整的行)。您可以通过首先将逗号 , 转换为换行符(使用简单的 sed 脚本)然后在每次调用 test.sh(使用 -n2)时传递两个参数(现在是两行)来修复它:

sed s/,/\\n/g file2.txt | xargs --max-procs 10 -n2 sh test.sh

请注意,xargs 通过-d 选项支持自定义分隔符,并且您可以在file2.txt 中的每一行以, 结尾的情况下使用它(但您可能应该去掉每个第一个前缀的换行符字段)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2019-04-12
    相关资源
    最近更新 更多