【发布时间】:2016-04-06 00:38:34
【问题描述】:
例子:
#!/bin/bash
function my_test(){
echo this is a test $1
}
my_test 1
python -c "from subprocess import check_output; print(check_output('my_test 2', shell=True))"
输出:
this is a test 1
/bin/sh: my_test: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/subprocess.py", line 629, in check_output
**kwargs).stdout
File "/usr/lib/python3.5/subprocess.py", line 711, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'my_test 2' returned non-zero exit status 127
【问题讨论】:
-
为什么不将
my_test函数保存在一个单独的shell 文件中并使用另一个shell 脚本调用运行python -c? -
/bin/sh不支持function关键字。使用#!/bin/bash(或其他shell)作为你的shebang。 -
请问您为什么要调用仅在该 shell 中定义的函数?
标签: python shell subprocess