[英]Passing arguments from shell to embedded Python scripts
我写了一个shell函数,旨在与zsh和bash兼容:
py () { python -c 'print($1)'; }
但是当我使用py hello
,我从Python解释器得到了一个错误:
➜ ~ py hello
File "<string>", line 1
print($1)
^
SyntaxError: invalid syntax
我究竟做错了什么? 谢谢!
根本不要使用字符串替换-这样就可以构成Bobby Tables (的表弟)。 而是带外传递参数:
py() { python -c 'import sys; print sys.argv[1]' "$@"; }
py hello
为了说明另一种方法为什么很危险:
py() { python -c "print('${1}')"; }
py "hello' + str(__import__('os').system('touch /tmp/broke-your-security')) + '"
运行时,此代码创建文件/tmp/broke-your-security
。 考虑如果该命令改为使用rm -rf
或curl | sh
会发生什么情况? curl | sh
删除rootkit。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.