【问题标题】:Problem with Passing both single and double quote via subprocess?通过子进程同时传递单引号和双引号的问题?
【发布时间】:2021-03-07 19:07:59
【问题描述】:

所以我想传递的原始命令是 -

runuser -l chrome -c 'DISPLAY= /opt/google/chrome-remote-desktop/start-host --code="4/yourcode" --redirect-url="https://remotedesktop.google.com/_/oauthredirect" --name=VastInstance'

我尝试了两种方法。第一个是-

def pcall(pram):
    s.run([pram], shell=True)

pcall("runuser -l raiyan -c 'DISPLAY= /opt/google/chrome-remote-desktop/start-host --code=\"4/0AY0e-g6DezhppO-9eF6BCGahZOOxN-Rs2uG0lm-G0FRIL1PvLR22-oqnCBHGmVUjr86o2A\" --redirect-url=\"https://remotedesktop.google.com/_/oauthredirect\" --name=Raiyan\'s PC' --pin=000000") ```  
      

where pcall is a function that calls subprocess.run() and the error says ```/bin/sh: 1: Syntext error: Unterminated quoted string.```

So i tried another way - ```s.run({'runuser','-l,'raiyan','-c','DISPLAY=','/opt/google/chrome-remote-desktop/start-host','--code=','4/SomeCode','--redirect-url=','https://remotedesktop.google.com/\_/oauthredirect','--name=','raiyan'])``` 

And this also show Syntex error right here
>https:// remotedesktop.google .com/\➡️_/⬅️oauthredirect 

我完全不知道在这里做什么。 [而且我也不明白为什么代码标签在这里不起作用:( ]

【问题讨论】:

  • 为什么不使用shlex
  • 老实说,我不知道这存在。 shlex 是否比 subprocess 更高效?
  • shlex 不会运行任何东西,但它会像 shell 一样拆分您的命令,以便您可以将其传递给subprocess.run。你可以用shlex.split("""runuser -l ....""") 试试看,它把它分成5个参数,而你把它分成12个(导致它失败)

标签: python linux subprocess


【解决方案1】:

您可以尝试使用三引号,这样您就不必在命令中转义任何内容。

pcall("""runuser -l chrome -c 'DISPLAY= /opt/google/chrome-remote-desktop/start-host --code="4/yourcode" --redirect-url="https://remotedesktop.google.com/_/oauthredirect" --name=VastInstance'""")

【讨论】:

  • File "<ipython-input-4-367a8ac28089>", line 60 pcall(""" runuser -l raiyan -c 'DISPLAY= /opt/google/chrome-remote-desktop/start-host --code="4/SomeCode" --redirect-url="https://remotedesktop.google.com/_/oauthredirect" --name=Raiyans PC --pin=000000' """) ^ SyntaxError: invalid syntax
  • @IsitiakMahmud 从您在此处分享的内容很难判断,但我认为语法问题在另一行,它很可能与字符串或函数调用直接无关。如果您可以分享更多代码,可能会更清楚问题所在。
  • 我确实编辑了我的代码,但似乎我无法正确编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多