简体   繁体   English

将bash脚本转换为python3

[英]convert a bash script to python3

I have a bash script, which is running perfectly: 我有一个bash脚本,可以完美运行:

gvim  --servername "servername" $1
if [ -f ${1%.tex}.pdf ];
then
  evince ${1%.tex}.pdf &
fi
evince_vim_dbus.py  GVIM servername ${1%.tex}.pdf  $1 &

I am trying to convert it to python as: 我正在尝试将其转换为python:

#!/usr/bin/env python3

from subprocess import call
import sys, os

inp_tex = sys.argv[1]
oup_pdf = os.path.splitext(sys.argv[1])[0]+".pdf"
print(oup_pdf)

call(["gvim", "--servername", "servername",  sys.argv[1]])

if os.path.exists(oup_pdf):
  call(["evince", oup_pdf])

call(["evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])

in the python, both gvim and evince window is open, but evince_vim_dbus.py line is not working. 在python中,同时打开了gvim和evince窗口,但evince_vim_dbus.py行不起作用。 Not that it is giving any error, but it is not showing intended result, as it should, and is doing with the bash script. 并不是说它给出了任何错误,而是它没有像预期的那样显示预期的结果,并且与bash脚本有关。

trying with check_call (I have to kill it after a while, here's the traceback): 尝试使用check_call (我必须在一段时间后将其杀死,这是回溯):

Traceback (most recent call last): 追溯(最近一次通话):

  File "/home/rudra/vims.py", line 28, in <module>
    check_call(["python","/home/rudra/bin/evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])
  File "/usr/lib64/python3.5/subprocess.py", line 576, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib64/python3.5/subprocess.py", line 559, in call
    return p.wait(timeout=timeout)
  File "/usr/lib64/python3.5/subprocess.py", line 1658, in wait
    (pid, sts) = self._try_wait(0)
  File "/usr/lib64/python3.5/subprocess.py", line 1608, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt

I'm going to have a guess that your real problem isn't the evince_vim_dbus.py line itself, but rather the gvim line, because you pass it the server name 'servername' instead of simply servername , and so doesn't match the name on the line that runs evince_vim_dbus.py . 我将猜测,您真正的问题不是evince_vim_dbus.py行本身,而是gvim行,因为您向其传递了服务器名'servername'而不是简单的servername ,因此与运行evince_vim_dbus.py的行上的名称。

I'm not familiar with gvim or its server functionality, but I'm guessing the evince_vim_dbus.py program connects to gvim using the given name, in which case it's going to fail since the server of the right name isn't running. 我不熟悉gvim或它的服务器功能,但是我猜evince_vim_dbus.py程序使用给定名称连接到gvim,在这种情况下,由于名称正确的服务器未运行,它将失败。

If that's not it, then maybe the problem is that subprocess.call() runs the given program and waits for it to exit, whereas in your original bash script, you run evince with an ampersand, causing bash not to wait for it, so maybe the problem is that evince_vim_dbus.py never runs at all until you exit Evince. 如果不是,那么问题可能出在subprocess.call()运行给定程序并等待其退出,而在您的原始bash脚本中,您使用&符运行evince ,导致bash不等待它,所以也许问题在于,在退出Evince之前, evince_vim_dbus.py根本不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM