微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell – 从标准输出管道传输中使用两次参数

我有一个命令行工具,它接收两个参数:

TOOL  arg1 -o arg2

我想用arg1和arg2提供的相同参数调用它,为了让我这么容易,我想我会这样做:

each <arg1_value> | TOOL $1 -o $1

但这不起作用,$1不会被替换,但会在命令行末尾添加一次.

一个明确的例子,执行:

cp fileA fileA

返回错误fileA和fileA相同(未复制)
执行时:

echo fileA | cp $1 $1

返回以下错误
用法:cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file … target_directory

有任何想法吗?

解决方法

如果要使用xargs,[ – I]选项可能会有所帮助:

-I replace-str
              Replace  occurrences of replace-str in the initial-arguments with names read from standard input.  Also,unquoted blanks do not terminate input items; instead the separa‐
              tor is the newline character.  Implies -x and -L 1.

这是一个简单的例子:

mkdir test && cd test && touch tmp
ls | xargs -I '{}' cp '{}' '{}'

返回错误cp:tmp和tmp是同一个文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐