Python Fabric awk可用于本地,但不能用于运行

Python Fabric awk可用于本地,但不能用于运行,awk,fabric,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 | aw

我正在编写一个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: 
执行任务“获取用户”
[本地主机]运行:ps auxww | awk'{print$1}'| sort | uniq-c | sort-rn | awk'{print$2}'
[localhost]out:awk:源代码第1行出现语法错误
[localhost]输出:上下文为

[localhost]out:{print>>\>\尝试使用带有单引号而不是双引号的
运行
命令。例如:

run('ps auxww | awk \'{print $1}\'')  
请参阅:

试试这个

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

不,这仍然不起作用。我收到了相同的错误。@ChadVernon Ok。要进行调试,您可以尝试将
$1
替换为
“Hello”
,然后查看错误是否仍然存在。(我怀疑错误与美元符号有关。)