在一个文件中给出多个终端命令并同时运行所有命令?

2024-09-25 10:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是树莓派的初学者,我有一个基本的怀疑。在

我基本上是想把我的树莓皮做成一个信标,然后把它的数据发布到一个Android应用程序上。在

我想知道我是否可以在一个文件中给出多个终端命令,并通过编译和运行该文件来运行所有命令?在

我跟着this tutorial。在

我最基本的怀疑是,每次我要检查一个设备是否可用(蓝牙)并为其做广告时,它都需要一个命令。我可以将多个raspberry pi命令集成到一个文件中,并通过编译和运行该文件(作为脚本)来运行所有这些命令吗?在

以下命令很少:

sudo hcitool lescan, 
sudo hcitool hci0,
sudo hcitool -i hci0 0x008,

很少有像这样的命令。。在


Tags: 文件数据命令应用程序终端sudothistutorial
2条回答

假设您有一个文件example.txt,其中包含以下命令:

sudo hcitool lescan
sudo hcitool hci0
sudo hcitool -i hci0 0x008

然后可以通过运行sh example.txtbash example.txt来执行这些命令。 见ubuntu run text file as command

如果您真的想使用python实现这一点,可以使用subprocess模块。在

import subprocess
with open ('/home/pi/bluetoothcommands.txt') as btcommands:
    for line in btcommands:
        subprocess.run (line)

如果你想要一个循环:

^{pr2}$

在/home/pi中/蓝牙命令.txt文件:

^{3}$

相关问题 更多 >