混合python和bashshell的xargs命令疑惑
Mixing python and bash shell's xargs command doubts
system : Centos 6.7 Lasted
Shell : bash
python : 2.6.6
这让我很困惑!下面的示例:
5 个文件:
a1111 a2222 b1111 b2222 t.py
t.py内容:
import sys
if __name__ == '__main__':
a1 = sys.argv[1]
print 'id(a1)=%s, len(a1)=%s, str(a1)=%s, type(a1)=%s' % (id(a1), len(a1), str(a1), type(a1))
这样做:
ls | xargs -I{} echo $(python t.py '{}')
输出:
id(a1)=139821454683184, len(a1)=2, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=t.py, type(a1)=<type 'str'>
我的问题是为什么len(a1)=2,而str(a1)=a1111?, 字符串长度明显不等于2,
没有回声是可以的,但这不是我的问题。我使用 xargs -p 选项打印 cmd
ls | xargs -I{} python t.py '{}'
当您看到此输出时:
len(a1)=2
这实际上是在 xargs
的 -I
选项之后使用的 replStr
的长度。这是 {}
因此长度是 2
.
如果您使用不同的replStr
。
ls | xargs -I% echo $(python ./t.py '%')
那么你会在输出中看到 len=1
因为 %
的长度是 1
.
id(a1)=4342949968, len(a1)=1, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=t.py, type(a1)=<type 'str'>
原因是当你使用:
ls | xargs -I{} echo $(python ./t.py '{}')
您实际上是在子 shell 中调用 python 程序,并且将文字 {}
传递给 python。
这是显示相同内容的调试输出:
bash -cx 'ls | xargs -I{} echo $(python ./t.py "{}")' >/dev/null
+ ls
++ python ./t.py '{}'
+ xargs '-I{}' echo 'id(a1)=4460329040,' 'len(a1)=2,' 'str(a1)={},' 'type(a1)=<type' ''\''str'\''>'
无论你们使用什么:
bash -cx 'ls | xargs -I{} python ./t.py "{}"' >/dev/null
+ ls
+ xargs '-I{}' python ./t.py '{}'
您可以在 xargs 调用 python 命令行的方式中看到不同的行为。
发生这种情况的原因是 $(python t.py '{}')
表达式在 被传递给 xargs
之前被计算 。 $(python t.py '{}')
打印 "id(a1)=139821454683184, len(a1)=2, str(a1)={}, type(a1)=",因此 that 被传递给 xargs
,用每个文件名替换 {}
...
这是一个 shell 跟踪,显示正在发生的事情:
$ set -x # turn on tracing
$ ls | xargs -I{} echo $(python t.py '{}')
+ ls
++ python t.py '{}'
+ xargs '-I{}' echo 'id(a1)=4560222208,' 'len(a1)=2,' 'str(a1)={},' 'type(a1)=<type' ''\''str'\''>'
id(a1)=4560222208, len(a1)=2, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=t.py, type(a1)=<type 'str'>
“+”行显示 shell 实际执行的内容。 (您可以忽略 xargs
的参数显示在单引号中;这只是因为 t.py
的输出被拆分为单词,但其输出中的其他 shell 元字符被忽略,并且要直接在命令行上获得相同的效果,您必须引用(/转义)参数。)
顺便说一句,还有另一个赠品表明这是正在发生的事情:每行的 id 都是相同的,但是如果 t.py
实际上是为每一行单独执行的,你会得到不同的 ids。
system : Centos 6.7 Lasted
Shell : bash
python : 2.6.6
这让我很困惑!下面的示例:
5 个文件:
a1111 a2222 b1111 b2222 t.py
t.py内容:
import sys
if __name__ == '__main__':
a1 = sys.argv[1]
print 'id(a1)=%s, len(a1)=%s, str(a1)=%s, type(a1)=%s' % (id(a1), len(a1), str(a1), type(a1))
这样做:
ls | xargs -I{} echo $(python t.py '{}')
输出:
id(a1)=139821454683184, len(a1)=2, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=t.py, type(a1)=<type 'str'>
我的问题是为什么len(a1)=2,而str(a1)=a1111?, 字符串长度明显不等于2,
没有回声是可以的,但这不是我的问题。我使用 xargs -p 选项打印 cmd
ls | xargs -I{} python t.py '{}'
当您看到此输出时:
len(a1)=2
这实际上是在 xargs
的 -I
选项之后使用的 replStr
的长度。这是 {}
因此长度是 2
.
如果您使用不同的replStr
。
ls | xargs -I% echo $(python ./t.py '%')
那么你会在输出中看到 len=1
因为 %
的长度是 1
.
id(a1)=4342949968, len(a1)=1, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=4342949968, len(a1)=1, str(a1)=t.py, type(a1)=<type 'str'>
原因是当你使用:
ls | xargs -I{} echo $(python ./t.py '{}')
您实际上是在子 shell 中调用 python 程序,并且将文字 {}
传递给 python。
这是显示相同内容的调试输出:
bash -cx 'ls | xargs -I{} echo $(python ./t.py "{}")' >/dev/null
+ ls
++ python ./t.py '{}'
+ xargs '-I{}' echo 'id(a1)=4460329040,' 'len(a1)=2,' 'str(a1)={},' 'type(a1)=<type' ''\''str'\''>'
无论你们使用什么:
bash -cx 'ls | xargs -I{} python ./t.py "{}"' >/dev/null
+ ls
+ xargs '-I{}' python ./t.py '{}'
您可以在 xargs 调用 python 命令行的方式中看到不同的行为。
发生这种情况的原因是 $(python t.py '{}')
表达式在 被传递给 xargs
之前被计算 。 $(python t.py '{}')
打印 "id(a1)=139821454683184, len(a1)=2, str(a1)={}, type(a1)=",因此 that 被传递给 xargs
,用每个文件名替换 {}
...
这是一个 shell 跟踪,显示正在发生的事情:
$ set -x # turn on tracing
$ ls | xargs -I{} echo $(python t.py '{}')
+ ls
++ python t.py '{}'
+ xargs '-I{}' echo 'id(a1)=4560222208,' 'len(a1)=2,' 'str(a1)={},' 'type(a1)=<type' ''\''str'\''>'
id(a1)=4560222208, len(a1)=2, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=4560222208, len(a1)=2, str(a1)=t.py, type(a1)=<type 'str'>
“+”行显示 shell 实际执行的内容。 (您可以忽略 xargs
的参数显示在单引号中;这只是因为 t.py
的输出被拆分为单词,但其输出中的其他 shell 元字符被忽略,并且要直接在命令行上获得相同的效果,您必须引用(/转义)参数。)
顺便说一句,还有另一个赠品表明这是正在发生的事情:每行的 id 都是相同的,但是如果 t.py
实际上是为每一行单独执行的,你会得到不同的 ids。