【发布时间】:2016-08-02 19:55:58
【问题描述】:
我的.profile 定义了一个函数
myps () {
ps -aef|egrep "a|b"|egrep -v "c\-"
}
我想从我的 python 脚本中执行它
import subprocess
subprocess.call("ssh user@box \"$(typeset -f); myps\"", shell=True)
返回错误
bash: -c: line 0: syntax error near unexpected token `;'
bash: -c: line 0: `; myps'
转义;结果
bash: ;: command not found
【问题讨论】:
-
正常的 ssh 命令是什么样子的(例如
ssh user@box ....)? -
感谢您的提问。普通命令看起来像
ssh user@box "$(typeset -f); myps"。它工作得很好。 -
你可以试试
subprocess.call(["/usr/bin/ssh","user@box","$(typeset -f);myps"])? -
@JoranBeasley,这实际上不起作用——只有第一个数组元素被解析为 shell 脚本内容,所以
$(typeset -f)不会在本地运行(导出本地 shell 函数)。 -
@Jam, ...btw,这里的部分问题是您的
.profile没有在非交互式 shell 中执行,因此实际上不会加载函数定义。