Kipibenkipod view of the world

Here is my Katas for creating BASH programs that work. Nothing is new here, but
from my experience pepole like to abuse BASH, forget computer science and create
a Big ball of mud from their programs.
Here I provide methods to defend your programs from braking, and keep the code
tidy and clean.

usage(){ cat <<- EOF usage: $PROGNAME options Program deletes files from filesystems to release space. It gets config file that define fileystem paths to work on, and whitelist rules to keep certain files. OPTIONS: -c --config configuration file containing the rules. use --help-config to see the syntax. -n --pretend do not really delete, just how what you are going to do. -t --test run unit test to check the program -v --verbose Verbose. You can specify more then one -v to have more verbose -x --debug debug -h --help show this help --help-config configuration help Examples: Run all tests: $PROGNAME --test all Run specific test: $PROGNAME --test test_string.sh Run: $PROGNAME --config /path/to/config/$PROGNAME.conf Just show what you are going to do: $PROGNAME -vn -c /path/to/config/$PROGNAME.conf EOF}

Pay attention that there should be real tab ‘\t’ in the start of the line for each
line.
In vim you can use this replace command if your tab is 4 spaces:

Intro

I want to block program from running twice simultaneously. Why would I want such
a thing? Lets say you have a program that creates the directory /tmp/prog and
update files inside this directory. If you run the program twice at the same
time, files inside this directory will get clobbered. Searching the net for
solution, flock comes up as a good way to solve the problem. flock
is part of the util-linux package.

At the heart of the locking mechanizm is the last line, which create the file
/var/lock/.myscript.exclusivelock using file descriptor 200. The syntax
9>textfile, is the way in BASH to create textfile using file descriptor 9.
Then we check if the file is locked in line 8, flock -n 200 using the
file descriptor 200 we used to create the lock file with. So while the
critical code after line 8, that runs in the sub shell, is running, the file
/var/lock/.myscript.exclusivelock stays locked, and will be released when the
sub shell exits.

Next step

My first rule of programming in BASH, is to have all the code functional and
the only line that is globally executed is main like this:

First example breaks my rule, and main is not the only global code, but is not
too bad. The second example, does not look good to me. It seems that main have
more syntax complexity then it should. What we really want, in order to keep the code
clean, is creating a lock function that will behave like this:

12345

main(){ lock ||exit 1
do_A
do_B
}

Creating a locking function

To move locking to a function, we’ll need to get rid of this sub shell syntax.

So now we have a lock function that does what we want. It gets the prefix of
the lock filename, and then variable fd is 200 by default, but you can specify
different file descriptor number like this lock $PROGNAME 500. The important
change in the lock function is the turning of 200 to a variable $fd, and to
be able to execute the lock file creation, we use eval. Try it without eval
and see what happens. You will get error that there is no command 200.

Conclusion

We arrived from script like locking mechanism to a well closed lock function,
that will create exclusivity of the program running. I would go further and
paste this lock command to a library of its own. Then source it. i.e. source
/usr/lib/lock.sh. Look again at the main function and see how elegant and
expressive it is. i.e. lock program or exit the program, because another
instance of the program is already running.

Intro

In the preparations for getting my first smart phone, mainly to use it as a
heart rate monitor and GPS for my workouts, I opted for buying a bluetooth heart
rate chest strap. Until I’ll get new phone, I want to connect the chest strap to
my laptop, for logging heart rate when exercising at home.

Searching the net for Linux HRM (heart rate monitor) implementation, I have
found an interesting post Listening to a Polar Bluetooth HRM in Linux. The
author wrote a Ruby script that listens to the Polar bluetooth device. So I
opted to buy the chest strap he uses in the blog post - Polar WearLink+
transmitter with Bluetooth. Pay attention this is not the Polar H7
which is the newer and support Bluetooth 4.0, but have a lot of issues
connecting to android devices. I paid 80$ include shipping to Israel.

After getting the Polar strap, I notice problem connecting the device.
Searching Google, I found that there are issues with the battery, that drains
too fast. Here is the thread about this problem. I took the battery out,
and tried to replace it with other batteries, but nothing. My computer couldn’t
find the device. Next day I tried again, and replaced the battery again to the
original. Then by magic, the computer did found the device. I don’t know how or
why this happened, but I’ll keep track of the battery life and post back.

My heart rate on the screen ;-)

In his blog post Daniel created a small program that listen to the
heart rate belt, then prints the data to the screen. Here is a sample output of
the program, and the column :hr=> shows my heart rate:

Few steps to get it working

Compile kernel Bluetooth drivers

make menuconfig

and in Networking support -> Bluetooth subsystem support (NEW) I just marked
everything as module. After you understand what driver you need, you can unmark
the drivers that are not needed by your system. This procedure is for
Gentoo users, or people that use custom kernels. For other OSs Bluetooth is
usually provided with the standard kernel.

Install software

You will need to install bluez for handling the Bluetooth service:

emerge -av net-wireless/bluez

Next, if you have Broadcom BCM203x and STLC2300 Bluetooth chips, you should
install their firmware:

emerge -av net-wireless/bluez-firmware

For debugging the connection you can use hcidump similar to tcpdump:

emerge -av net-wireless/bluez-hcidump

And last, but very important for providing PIN to the Bluetooth device:

emerge -av net-wireless/blueman

Enable Bluetooth

I have an Lenovo X201 laptop, so I enable and disable the Bluetooth with this
command:

If you don’t have a Thikpad, this method will not work, as this command relays
on thinkpad.ko kernel module. Please Google to find out how to enable Bluetooth
on your setup.

Now start the bluez service:

/etc/init.d/bluetooth start

Search the Polar device

In order to search the chest strap, you will need to run:

hcitool scan

and the output should look similar to this:

Scanning ...
00:22:D0:00:95:31 Polar iWL

The mac address is important, as you need to supply it as a parameter to
polar.rb.

I found that my laptop will not find the Polar device on the first scan, so I
ran the following command to scan few times:

while true ; do hcitool scan ; sleep 1 ; done

It took a while, but it finds the Polar device eventually.

Pairing laptop with Polar strap

Ok, I must admit, this was quit challenging. I didn’t install the bluman package
at first, and saw that running

ruby polar.rb 00:22:D0:00:95:31

result with connection refused. Using hcidump, I saw that the Bluetooth on the
chest strap is aborting the connection due to lack of PIN code. The manual
states that the PIN is 0000 (4 times zero), so I searched for a way to add this
to the program. It wasn’t straight forward task, so I read again Daniel’s
post and noticed that he uses the blueman package. I installed it. Then ran
the command:

blueman-applet

Hit the Search button and it should find the Polar device, then hit the Pair
button to allow the device to request the PIN. This provided the initial pairing
of the device with the laptop.

After this procedure, I was able to run the polar.rb using the command: