我有一个命令行工具,它接收两个参数:
TOOL arg1 -o arg2
Run Code Online (Sandbox Code Playgroud)
我想用arg1和arg2提供的相同参数调用它,为了让我这么容易,我想我会这样做:
each <arg1_value> | TOOL $1 -o $1
Run Code Online (Sandbox Code Playgroud)
但这不起作用,$ 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.
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子:
mkdir test && cd test && touch tmp
ls | xargs -I '{}' cp '{}' '{}'
Run Code Online (Sandbox Code Playgroud)
返回错误cp:tmp 和 tmp 是同一个文件
它们$1,$2...$N仅对 bash 脚本可见,用于解释这些脚本的参数,并且不会按照您希望的方式工作。管道重定向stdout到stdin并且也不是您正在寻找的内容。
如果你只想要一句台词,请使用类似的东西
ARG1=你好&&工具$ARG1 $ARG1