Wes*_*Wes 15 command-line shell xargs arguments
我在 Ubuntu 上。我复制了一些参数(由换行符分隔),我可以使用xsel将它们打印出来
$ xsel
arg1
arg2
arg3
arg4
...
Run Code Online (Sandbox Code Playgroud)
现在,我想将这些参数中的每一个用于另一个命令,并根据参数的数量执行该命令。
所以我试过了
$ xsel | mycommand "constantArgument" $1
Run Code Online (Sandbox Code Playgroud)
但是,这mycommand仅对第一个参数执行。我如何为每个参数执行它?
Rah*_*til 14
你可以简单地使用 xargs
xsel | xargs -n1 echo mycommand
Run Code Online (Sandbox Code Playgroud)
-n1 意味着 mycommand 的一个参数,但它只是空运行,它将显示将要运行的内容,运行它删除 echo
对于常量参数
xsel | xargs -I {} -n1 echo mycommand "constantArgument" {}
Run Code Online (Sandbox Code Playgroud)
xsel | while read line; do mycommand "$line"; done
Run Code Online (Sandbox Code Playgroud)
或者类似的东西。您还可以使用xargs,这是一个非常强大的命令,用于操作命令行参数。