python - 根据按下的键值执行 shell 脚本

标签 python shell keyboard-shortcuts

如何使用 python 根据不同的键盘事件执行不同的 shell 脚本(tes1.sh、test2.sh、..)。

如果用户按下 "1" 执行 test1.sh 如果 "2" test2.sh 等等.

我使用“原始输入”按下了键,并根据输入值使用 subprocess.call 调用了“test1.sh”或“test2.sh”。

如果按下不同的键,如何停止运行当前的 shell 脚本并运行新的。

最佳答案

你可以在 python2 中使用 subprocess.callraw_input(),在 python3 中使用 input:

import subprocess
num=raw_input('print the number : ')

if num=='1' :
   subprocess.call('test1.sh')
elif num == '2':
   subprocess.call('test2.sh')

关于python - 根据按下的键值执行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215265/

相关文章:

python - 使用python在一行中打印多个变量

python - 使用Python回测多只股票

python - 如何从网页中的多个表格中抓取内容

bash - 如果重新分配,局部变量是否保持局部变量?

macos - 在 Mac OS X 终端中使用命令作为 ctrl

python - 将 DataFrame 转换为对象数组

linux - 如何将命令输出作为多个参数传递给另一个命令

shell - "history -p"有什么作用?

vim - 在 vim 中插入类似行时,如何最大限度地减少击键次数?

c++ - 跨平台 Qt 应用程序中的全局热键