Python Fabric awk适用于本地但不运行

时间:2014-04-08 20:08:24

标签: awk fabric

我正在编写一个Fabric命令来获取在一台机器上运行进程的用户列表。当我在shell中运行时,以及当我使用本地函数但在使用run函数时它出错时,该命令有效:

from fabric.api import run, env, local

# Works
local("ps auxww | awk '{print $1}' | sort | uniq -c | sort -rn | awk '{print $2}'")

# Does not work
run("ps auxww | awk '{print $1}' | sort | uniq -c | sort -rn | awk '{print $2}'")

这是错误:

Executing task 'get_users'
[localhost] run: ps auxww | awk '{print $1}' | sort | uniq -c | sort -rn | awk '{print $2}'
[localhost] out: awk: syntax error at source line 1
[localhost] out:  context is
[localhost] out:    {print >>>  \ <<< }
[localhost] out: awk: illegal statement at source line 1
[localhost] out: awk: syntax error at source line 1
[localhost] out:  context is
[localhost] out:    {print >>>  \ <<< }
[localhost] out: awk: illegal statement at source line 1
[localhost] out: 

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

尝试使用带有单引号的run命令而不是双引号。像:

run('ps auxww | awk \'{print $1}\'')  

请参阅:"Escaping" $ when executing a remote bash command from python fabric

答案 1 :(得分:0)

试试这个

run("ps auxww | awk '{print $1}' | sort | uniq -c | sort -rn | awk '{print $2}'"
, shell_escape=False)