Skip to main content
 首页 » 编程设计

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

2025年05月04日12txw1958

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

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

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

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

请您参考如下方法:

你可以在 python2 中使用 subprocess.call 和 raw_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')