Embedded Linux – Shell Scripting 101

The Android and Linux operating systems have become very popular in the last few years. Due to their open source nature, the cost is right and thankfully large numbers of engineers have joined in the effort to port these operating systems to nearly ever platform imaginable. Their wide use makes it nearly inevitable that at some point, an embedded developer will take the dive into developing a system based on these systems.


One of the most useful tools when developing from within a Linux environment is the use of shell scripting. Scripting can help aid in setting up environment variables, performing repetitive and complex tasks and ensuring that errors are kept to a minimum. Since scripts are ran from within the terminal, any command or function that can be performed manually from a terminal can also be automated!


Creating a script extremely straight forward. It can be created by either open an editor such as gedit or through a terminal editor such as VI. The most common type of script is a bash script. These scripts usually are associated with a .sh extension but it isn’t necessary to include an extension at all! At the top of any script should be included on the first line #!/bin/bash. This tells what interpreter should be used for running the script.


Beyond that the script can be customized to meet the developers needs. A good first script, just like any program, is to create a “Hello World” script. This can be done by creating the script file as described above in a favorite editor and then entering the text from Listing 1. Beyond the interpreter specifier, clear is used to clear the terminal of any previous messages and echo is used to output text.


el1-listing 1

Once the script has been created, it can be ran using the terminal. However, running the script immediately as ./scripts.sh will usually result in an error. This error is due to the files permissions. This can be resolved by first typing the command chmod +x script_name.sh. This will add the script to have executable permissions. The script can then be ran by typing ./script_name.sh.


Now that a first script has been ran, the real question comes down to what can be done in a script? What commands and functions can be ran? The answer is anything that can be ran or executed from the terminal! A script can use cd to change directories. Perhaps the script need to create a directory and move into it. Not a problem! The script can include the mkdir command and then cd can be used to traverse the new directory. A developer may even want to add the created directory to the path variable. This can be done using the export command. Please see Listing 2 for an example of these script features!


el1-listing 2

When developing software for an embedded system it can sometimes be necessary to want to modularize the scripts. Perhaps there is a need to only compile the kernel and sometimes there is a need to compile the kernel and then link in some external library. It is possible to create scripts to handle these individual items and then create a script that calls these individual scripts based on the build need! This is a relatively straight forward process and it pull together all of the examples that have been presented so far. Listing 3 shows an example of how to call a script from within a script.


el1-listing 3


Just like in a regular computer program, there may be a need to use variables to keep track of data. Whether this is string or number data, scripts can access variables just like a standard application. Within the scripting environment, there are a couple of requirements for variable names just like in any regular computer language. These rules are very similar to the rules of C. The first, is that the variable must start with a letter. Second, the variable cannot contain spaces but instead should use underscores to bridge names. Third, the variable name should not be a keyword or reserved word that is understood by the bash interpreter. This would lead to confusion and inconsistent or incorrect results. Finally, the variable name should not include any punctuation marks. Following these rules will ensure that a developer enjoys a bug-free scripting experience.


Now creating a variable is straight forward. Unlike in C, there are no type specifiers. In a script, a variable can be declared by naming it and then setting it to a value. An example of this can be found in Listing 4. It is also possible to access system variables and information from within a script. For example, the computer host name can be accessed by using $HOSTNAME. The date and current user information can also be stored. An example of this is shown in Listing 4.


el1-listing 4


Armed with this information, a developer can now has a fundamental understanding of the necessary commands needed to start automating the software development process. While the information provided here is a good start, keep an eye open for “Shell Scripting 102” which will go into details on how to control script execution flow and accept user information.

Share >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.