1

I'm a beginner into Raspberry pi and i have a basic doubt.

I'm basically trying to make my raspberry pi into a beacon and advertise data from it to a Android app.

I wonder if I can give multiple terminal commands in a single file and run all the commands simply by compiling and running the file?

I followed this tutorial.

My basic doubt is that, each time i have to check if a device is available(bluetooth) and advertise it, it takes a command for each of this. Can i integrate multiple raspberry pi commands into a file and run all these commands simply by compiling and running the file (as a script)?

Few of the commands are as follows :

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

and few commands like these..

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • 2
    You'll repay the time you spend reading a Scripting tutorial. This looks pretty good : http://ryanstutorials.net/bash-scripting-tutorial . Good luck. – shellter Aug 29 '16 at 15:15

2 Answers2

1

Say you have a file example.txt with your commands:

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

Then you can execute those commands by running sh example.txt or bash example.txt. See ubuntu run text file as command

Community
  • 1
  • 1
pschill
  • 5,055
  • 1
  • 21
  • 42
1

If you truly want to use python for this you could use the subprocess module.

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

If you wanted this in a loop:

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

In the /home/pi/bluetoothcommands.txt file:

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