Mixing python and bash shell xargs casts doubt on the command

system : Centos 6.7 Lasted 
Shell : bash 
python : 2.6.6

It really bothers me! example below:

5 files:

a1111  a2222  b1111  b2222  t.py

t.py content:

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))

do it like this:

ls | xargs -I{} echo $(python t.py '{}')

output:

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'>

my question is why len (a1) = 2, but str (a1) = a1111 ?, the line length is obviously not 2 ,

no echo is OK, but that's not my question. I have the xargs -p option to print cmd

ls | xargs -I{} python t.py '{}'
+4
source share
2 answers

, $(python t.py '{}') , xargs. $(python t.py '{}') prints "id (a1) = 139821454683184, len (a1) = 2, str (a1) = {}, type (a1) =", xargs, {} ...

, , :

$ 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'>

"+" , . ( , xargs , , t.py , (/escape) .)

, , , : id , t.py , .

+6

:

len(a1)=2

replStr, -I xargs. {}, 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 '{}')

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.

+2

All Articles