6

Xargs Sh -c Skipping the First Argument

 3 years ago
source link: https://happy123.me/blog/2020/08/12/xargs-sh-c-skipping-the-first-argument/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xargs Sh -c Skipping the First Argument

Aug 12th, 2020 | Comments

其实这个问题已经见过很多次了,但是知其然不知其所以然;今天偶尔在stackoverflow上看到了,记录一下;

shell中的arg1, arg2…

在bash shell中,$1, $2代表arg1, arg2,比如

1
2
3
# echo hello world|xargs echo $1 $2

hello world

$0 代表执行环境,如果是一个执行脚本的话,$0 代表其脚本名;比如下面这个脚本hello.sh:

1
2
3
4
5
#!/bin/bash

echo $0
echo $1
echo $2
1
# ./hello.sh arg1 arg2
1
2
3
./hello.sh
arg1
arg2

xargs 调用sh -c 中的arg

但是使用xargs sh -c时会出现一个比较疑惑的情况,比如执行:

1
2
3
# echo hello world|xargs sh -c 'echo $1 $2'

world

此时$1代表world,$2已经没有值了;而执行

1
2
3
# echo hello world|xargs sh -c 'echo $0 $1'

hello world

反而得到了正确结果;

之前我一直认为xargs sh -c调用的时候吃掉了$0,不求甚解;偶尔读了一下sh的手册才发现玄机:

From the documentation for the -c option:

Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters (1,1,2, and so on) in sequence from the remaining argument operands.

就是说在上面这条命令中,其实是没有找到要执行的命令,或者说要执行的命令为空,而hello world作为$1, $2传给一个空命令了;

后面追加一个dummy的命令会看的更清楚:

1
2
3
# echo hello world|xargs sh -c 'echo $1 $2' _

hello world

后面我加了一条下划线作为xargs的dummy command,这样$1, $2就恢复正常了;

虽然这是一个啥用也没有的Magic Topic,但是搞明白之后还是挺有意思的,娱乐用;

另外隐隐约约觉得这里面隐含着一些安全方面的问题,暂时只是一种感觉,将来需要留意有没有这方面的hack手段;

https://stackoverflow.com/questions/41043163/xargs-sh-c-skipping-the-first-argument

Posted by brain-zhang

Aug 12th, 2020tools

« How to do logging on solidity contract with truffle suite How to sort by length of string followed by alphabetical order »

Comments


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK