简体   繁体   English

通过 bash 脚本在控制台中双击执行 python 脚本

[英]Execute python script in console via bash script with double-click

I have a python script, that runs fine if i run in on the console with python3 script.py .我有一个 python 脚本,如果我使用python3 script.py在控制台上运行,它运行良好。 For some reason, the script should be executed via a bash-script "start.sh".出于某种原因,该脚本应通过 bash 脚本“start.sh”执行。 A MWE would be MWE 将是

#!/bin/bash 
python3 GUIpyPCS.py

When I run the script by double-clicking it, it doesent open a terminal window.当我通过双击运行脚本时,它不会打开终端 window。 Hence, the python script is executed invisibly in the background and not on a new console window.因此,python 脚本在后台不可见地执行,而不是在新控制台 window 上执行。

How can I ensure, that the python script is executed on a console by double-clicking the bash-script?我如何确保通过双击 bash 脚本在控制台上执行 python 脚本? Is there something I can add to the bash script?有什么可以添加到 bash 脚本的内容吗? I am currently testing on Kubuntu 20.04, but the solution should also applicable to other distributions/window managers.我目前正在 Kubuntu 20.04 上进行测试,但该解决方案也应该适用于其他发行版/窗口管理器。

---------- Assumptions ---------- ---------- 假设 ----------

  • the script is in a directory脚本在一个目录中
  • you open that directory in the graphics interface (Nemo, Nautilus, any other file manager)您在图形界面中打开该目录(Nemo、Nautilus、任何其他文件管理器)
  • you double-click the icon representing the script你双击代表脚本的图标
  • you expect to see a terminal window open, with the script execution in it您希望看到一个终端 window 打开,其中执行脚本

---------- In your script do ---------- ---------- 在你的脚本中做 ----------

  • start a terminal window启动终端 window
  • that terminal command includes a call to the script该终端命令包括对脚本的调用
  • and lastly a call to the read command, to keep the window opened until you are done最后调用read命令,以保持 window 处于打开状态,直到完成
  • you press enter to close the window你按回车键关闭 window

---------- Ex ---------- - - - - - 前任 - - - - -

#!/bin/bash
#
xterm -e "/bin/ls /etc; read" &

---------- Details ---------- - - - - - 细节 - - - - -

  • the -e option to xterm is the command that will be executed in the new window xterm-e选项是将在新的 window 中执行的命令

  • I put /bin/ls /etc; read我把/bin/ls /etc; read /bin/ls /etc; read instead of your script. /bin/ls /etc; read而不是您的脚本。 You would put your_script.bash arg1 arg2; read你会把your_script.bash arg1 arg2; read your_script.bash arg1 arg2; read

  • you could do something similar with gnome-terminal instead of xterm你可以用gnome-terminal而不是xterm做类似的事情

  • the read is not essential, it could be a sleep 5 for example. read不是必需的,例如可以是sleep 5 Or if your script already has a delay or something, you might not need anything.或者,如果您的脚本已经有延迟或什么,您可能不需要任何东西。

  • I used xterm since it is available on all distributions, which ever window manager you use.我使用了xterm ,因为它适用于所有发行版,无论您使用哪种 window 管理器。

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

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