如何传递与xargs的所有参数在linux命令的中间(How can I pass all argu

2019-08-16 16:56发布

我想通过所有的文件,Linux上的单一的说法,但我不能这样做。

这是工作

ls | sort -n | xargs  -i pdftk  {} cat output combinewd2.pdf

这通过每个命令的一个参数,但我想在一个命令所有。

Answer 1:

这是做这件事

pdftk $(ls | sort -n) cat output combinewd2.pdf

或使用反引号

pdftk `ls | sort -n` cat output combinewd2.pdf

正如在评论中指出,这将不包含空格的文件名使用。 在这种情况下,你可以使用eval

eval pdftk $(while IFS= read -r file; do
    echo \"$file\"
done < <(ls | sort -n)) cat output combinewd2.pdf

假设有一个名为“0 foo”和“1条”两个文件,然后EVAL的结果将是需要的命令,用双引号的文件名:

pdftk " 0 foo " " 1 bar " cat output combinewd2.pdf

如果文件名可能包含换行符,然后使用find命令,请参阅@ andrewdotn的答案的评论通过@joeytwiddle讨论。 下面的解决方案还负责与使用双引号的文件名sed命令逃脱双引号:

eval pdftk $(while IFS= read -r -d '' file; do
    echo \"$file\"
done < <(find . -maxdepth 1 -type f -print0 | \
    sed 's/"/\\"/g'| sort -zn)) cat output combinewd2.pdf


Answer 2:

使用-I选项:

echo prefix | xargs -I % echo % post

输出:

prefix post


Answer 3:

这是丑陋的,但你可以运行sh -c和访问由传递的参数列表xargs"${@}" ,就像这样:

ls | sort -n | xargs -d'\n' sh -c 'pdftk "${@}" cat output combinewd2.pdf' "${0}"

额外的"${0}"在到底是那里,因为,作为sh手册页说:

-c 字符串

如果有-c选项,那么命令将从string中读取。 如果字符串后还有争论,他们被分配到的位置参数,以$ 0起始。

为了验证这一点,让我们先创建一个复杂的名字有些文件会搞乱了大部分其他解决方案:

$ seq 1 100 | xargs -I{} touch '{} with "spaces"'
$ ls
1 with "spaces"    31 with "spaces"  54 with "spaces"  77 with "spaces"
10 with "spaces"   32 with "spaces"  55 with "spaces"  78 with "spaces"
100 with "spaces"  33 with "spaces"  56 with "spaces"  79 with "spaces"
11 with "spaces"   34 with "spaces"  57 with "spaces"  8 with "spaces"
12 with "spaces"   35 with "spaces"  58 with "spaces"  80 with "spaces"
13 with "spaces"   36 with "spaces"  59 with "spaces"  81 with "spaces"
14 with "spaces"   37 with "spaces"  6 with "spaces"   82 with "spaces"
15 with "spaces"   38 with "spaces"  60 with "spaces"  83 with "spaces"
16 with "spaces"   39 with "spaces"  61 with "spaces"  84 with "spaces"
17 with "spaces"   4 with "spaces"   62 with "spaces"  85 with "spaces"
18 with "spaces"   40 with "spaces"  63 with "spaces"  86 with "spaces"
19 with "spaces"   41 with "spaces"  64 with "spaces"  87 with "spaces"
2 with "spaces"    42 with "spaces"  65 with "spaces"  88 with "spaces"
20 with "spaces"   43 with "spaces"  66 with "spaces"  89 with "spaces"
21 with "spaces"   44 with "spaces"  67 with "spaces"  9 with "spaces"
22 with "spaces"   45 with "spaces"  68 with "spaces"  90 with "spaces"
23 with "spaces"   46 with "spaces"  69 with "spaces"  91 with "spaces"
24 with "spaces"   47 with "spaces"  7 with "spaces"   92 with "spaces"
25 with "spaces"   48 with "spaces"  70 with "spaces"  93 with "spaces"
26 with "spaces"   49 with "spaces"  71 with "spaces"  94 with "spaces"
27 with "spaces"   5 with "spaces"   72 with "spaces"  95 with "spaces"
28 with "spaces"   50 with "spaces"  73 with "spaces"  96 with "spaces"
29 with "spaces"   51 with "spaces"  74 with "spaces"  97 with "spaces"
3 with "spaces"    52 with "spaces"  75 with "spaces"  98 with "spaces"
30 with "spaces"   53 with "spaces"  76 with "spaces"  99 with "spaces"
$  ls | sort -n | xargs -d'\n' sh -c 'set -x; pdftk "${@}" cat output combinewd2.pdf' "${0}"
+ pdftk '1 with "spaces"' '2 with "spaces"' '3 with "spaces"' '4 with "spaces"' '5 with "spaces"' '6 with "spaces"' '7 with "spaces"' '8 with "spaces"' '9 with "spaces"' '10 with "spaces"' '11 with "spaces"' '12 with "spaces"' '13 with "spaces"' '14 with "spaces"' '15 with "spaces"' '16 with "spaces"' '17 with "spaces"' '18 with "spaces"' '19 with "spaces"' '20 with "spaces"' '21 with "spaces"' '22 with "spaces"' '23 with "spaces"' '24 with "spaces"' '25 with "spaces"' '26 with "spaces"' '27 with "spaces"' '28 with "spaces"' '29 with "spaces"' '30 with "spaces"' '31 with "spaces"' '32 with "spaces"' '33 with "spaces"' '34 with "spaces"' '35 with "spaces"' '36 with "spaces"' '37 with "spaces"' '38 with "spaces"' '39 with "spaces"' '40 with "spaces"' '41 with "spaces"' '42 with "spaces"' '43 with "spaces"' '44 with "spaces"' '45 with "spaces"' '46 with "spaces"' '47 with "spaces"' '48 with "spaces"' '49 with "spaces"' '50 with "spaces"' '51 with "spaces"' '52 with "spaces"' '53 with "spaces"' '54 with "spaces"' '55 with "spaces"' '56 with "spaces"' '57 with "spaces"' '58 with "spaces"' '59 with "spaces"' '60 with "spaces"' '61 with "spaces"' '62 with "spaces"' '63 with "spaces"' '64 with "spaces"' '65 with "spaces"' '66 with "spaces"' '67 with "spaces"' '68 with "spaces"' '69 with "spaces"' '70 with "spaces"' '71 with "spaces"' '72 with "spaces"' '73 with "spaces"' '74 with "spaces"' '75 with "spaces"' '76 with "spaces"' '77 with "spaces"' '78 with "spaces"' '79 with "spaces"' '80 with "spaces"' '81 with "spaces"' '82 with "spaces"' '83 with "spaces"' '84 with "spaces"' '85 with "spaces"' '86 with "spaces"' '87 with "spaces"' '88 with "spaces"' '89 with "spaces"' '90 with "spaces"' '91 with "spaces"' '92 with "spaces"' '93 with "spaces"' '94 with "spaces"' '95 with "spaces"' '96 with "spaces"' '97 with "spaces"' '98 with "spaces"' '99 with "spaces"' '100 with "spaces"' cat output combinewd2.pdf

所有参数都正确引用。 请注意,如果任何文件名包含换行符,并认为这将失败ls -v基本上ls | sort -n ls | sort -n



Answer 4:

这应该含有空格,换行,撇号和引号(所有这些都可能对UNIX文件系统)的文件名工作:

find . -maxdepth 1 -type f -print0 |
  sort -zn |
  xargs -0 sh -c 'pdftk "$@" cat output combinewd2.pdf' "$0"

这可能是矫枉过正比较公认的答案,如果你知道你用简单的文件名的工作。

但是,如果你正在写,将在今后再次使用的脚本,希望它不会爆炸,有一天它遇到异常(但有效)的投入。

这基本上是一个适应andrewdotn的回答,其终止与一个零字节的输入文件,而不是与一个新行,因此保留其含有一个或多个换行字符的文件名。

各选项-print0-z-0告诉每个输入/输出应当由零字节被限定的程序。 三种不同的方案,三种不同的参数!



Answer 5:

我发现的最直观的方法是:

  • 第一构造具有-I {}和“回声”的命令,
  • 然后执行与“打击”的命令(好像你正在执行一个shell脚本)

这里是重命名从“.TXT”到“.txt.json”扩展的示例:

find .|grep txt$|xargs -I{} echo "mv {} {}.json"|bash

略先进例如重命名的.txt到上传.json(除去.txt扩展)

find $PWD|grep txt$|cut -d"." -f1|xargs -I{} echo "mv {}.txt {}.json"|bash

我曾经有一个要求追加到所有文件的字符串“文件结束”。

find .|grep txt|xargs -I{} echo "echo End of File >> {}"|bash

如果你这样做是正确的,xargs的是所有的命令之王!



Answer 6:

这里的问题是,虽然xargs的可以把个别参数与命令执行过程中-i{}它拒绝了多个参数做到这一点。 这似乎是最终导致了我们很多麻烦的监督!

除了上面的解决方案,另一种选择是简单地添加你要来的文件到文件列表结束后的参数。

(
  ls | sort -n
  echo cat
  echo output
  echo combinewd2.pdf
) | xargs -d'\n' pdftk

这种方法不会对包含换行符的文件名的工作 。 对于罕见的情况下,你应该通过一个零字节到xargs的终止,因为我在其他的答案提供的线路。



Answer 7:

您可以通过链接两个调用xargs的做到这一点。 使用第一个链中的所有参数的个数连成一个字符串,并传递作为一个参数去echo使用,而第二-I放置ARGS到您想要的地方是链,如下所示:

ls | sort -n | xargs echo | xargs -I {} pdftk {} cat output combinewd2.pdf


文章来源: How can I pass all arguments with xargs in middle of command in linux
标签: linux xargs