BeagleBone tutorial: Blinking LEDs using bash

While what I am demonstrating this post has already been accomplished by others (e.g. Matt Richardson, Akademii, Dan Watts and Brian Jepson), I found it useful for me to go through the motions of writing my own version of the “Blinking LED” project in a BeagleBone board. The following script switches between high and low signals, on several pins, and uses that information to drive some connected LEDs.

Without further ado, here is the code.

To run this code, save it into a file (e.g., and type the following commands on your BeagleBone terminal.

chmod u=+rwx

To actually blink LEDs, connect them to the following pins: P8_3, P8_4, P8_5, P8_11, P8_12, P8_14, P8_15, P8_16, P8_17 P8_20 and P8_21. If you do not know where these pins are, make sure you read theĀ BeagleBone system reference manual. Keep in mind that P8, or Port 8, is the expansion port next to the small user LEDs, and the following digit is the respective pin number.

If you would like to add more LEDs and pins to my source code, you must look into the BeagleBone system reference manual, and search for Table 8: Expansion Header P8 Pinout. Search for the signal names that start with gpio and in my script, insert another line of code after:

Replace P8_21 with a new pin identifier (e.g. P8_26), assign the associated GPIOID (e.g. gpio1[29] for P8_26 ), assign the associated GPIONUMBER (e.g. 61 for P8_26, see footnotes [1] for more information about this) and finally assign the associated GPIOMUX (e.g. gpmc_csn0, which is the MODE0 “name” as described on Table 9-P8 Mux Options Modes 0-3.

[1] GPIONUMBER comes directly from the GPIOID. Consider gpioXX[YY], so the GPIONUMBER is XX*32 + YY. If we have gpio1[29], then the GPIONUMBER is 1*32+29 = 61. Yes, I could have included a function that could calculate the number on the spot, but that would have made the code a lot harder to understand. If you want to include a function that does this, you may use the following code:

File related Linux bash snippets

Here are some of extremely useful Linux bash snippets I use all the time to parse experimental data from my simulations.

How to extract the top (insert number here) lines from a file
Consider a file named test-file.txt. You can extract the top 14 lines from that file using the following:

How to extract specific lines in a file using regular expressions.
Consider a file, named test-file.txt, with the following lines:

To extract the lines that have 5 elements we can type:

To extract the other lines, we can simply negate that regular expression:

How to search and replace text on a file

Where string1 is what you are searching for, string2 is what you want it to be replaced with and file.txt is the file you want to perform this operation on.

How to print a specific line number inside a file using a variable

Where $LINENBR is the number of the line you want to print.

How to append 2 files, column by column, keeping particular columns

How to transfer files across computers with ssh
The scp command copies files to a remote Linux system.

To copy files from a remote system to your local system:

How to remove a file extension

How to remove redundant lines inside a file
In the terminal type:

How to delete the last line of a file
In the terminal type:

How to count the number of lines in a file and write that number into another file
In the terminal type:

Alternatively you can also store the number of lines into a shell variable.

How to perform the same operation over several files
In a bash script type:

this will print out all filenames via an echo command, that will have the regular expression FAULTY*

How to write a loop inside a bash file

How to split a string inside a bash file

How to do input parameter error testing
The variable ${CIRCUIT} is the first command line argument (${1})

How to perform a particular operation on each line of a file

How to ensure that two files have the same number of lines

Where file1.txt and file2.txt are the filenames you wish to compare.

How to count the number of characters in file
In this particular example, the character I am counting is the 0.

How to replace characters in a file
This particular command will replace all “:” with the newline character.

How to delete all instances of a particular character from a file
This particular command will delete all “:” from the file longString.txt and it will write it on the file readableString.txt.