0

我想从 bash 标准输入执行像 python 这样的脚本代码,因为我不想先将脚本写入文件。怎么办?如果我想从 bash 标准输入运行 python 代码,例如:

#!/usr/bin/env python

print 'hello world'

$cat mycode.py | 重击 -s

但它不起作用。显示错误:

Warning: unknown mime-type for "hello world" -- using "application/octet-stream"

错误:没有这样的文件“hello world”

4

2 回答 2

4

但它不起作用。显示错误:警告:“hello world”的未知 mime-type -- 使用“application/octet-stream”错误:没有这样的文件“hello world”

当然,bash 不知道命令hello world和系统建议安装一些东西。下面可以工作

python -c 'print "ls -l"' | bash
于 2013-08-28T08:57:46.763 回答
1

作为变体,您可以进行类似于此场景的操作:

$ cat test.py 
#!/usr/bin/python

import sys

if sys.version_info[0] == 2:
    sys.stdout.write("ls -l")

$ python test.py | bash
итого 4
-rw-rw-r-- 1 miha miha 91 авг.  28 12:21 test.py
$ chmod +x test.py
$ ./test.py | bash
итого 4
-rwxrwxr-x 1 miha miha 91 авг.  28 12:21 test.py
于 2013-08-28T09:29:54.820 回答