[英]Give multiple terminal commands in a single file and run all the commands at once?
我是Raspberry pi的初学者,我有一个基本的疑问。
我基本上是想把我的树莓派变成一个信标,并将它的数据发布到Android应用程序。
我想知道是否可以在单个文件中给出多个终端命令,然后仅通过编译和运行文件来运行所有命令?
我遵循了本教程 。
我的基本怀疑是,每当我必须检查设备是否可用(蓝牙)并发布广告时,它都会为此使用一个命令。 我可以将多个raspberry pi命令集成到一个文件中,然后仅通过编译和运行文件(作为脚本)来运行所有这些命令吗?
很少有以下命令:
sudo hcitool lescan,
sudo hcitool hci0,
sudo hcitool -i hci0 0x008,
像这样的几个命令
假设您的命令包含文件example.txt
:
sudo hcitool lescan
sudo hcitool hci0
sudo hcitool -i hci0 0x008
然后,您可以通过运行sh example.txt
或bash example.txt
来执行这些命令。 参见ubuntu以命令方式运行文本文件
如果您确实要为此使用python,则可以使用subprocess模块。
import subprocess
with open ('/home/pi/bluetoothcommands.txt') as btcommands:
for line in btcommands:
subprocess.run (line)
如果您想循环执行以下操作:
import subprocess
with open ('/home/pi/bluetoothcommands.txt') as btcommands:
while True:
for line in btcommands:
subprocess.run (line)
在/home/pi/bluetoothcommands.txt文件中:
sudo hcitool lescan
sudo hcitool hci0
sudo hcitool -i hci0 0x008
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.