Skip to main content
Engineering LibreTexts

Editors, IDEs, scripts, interpreters, and compiled code

  • Page ID
    33017
    • The basics of programming are writing instructions in a file.  If it is a script then an interpreter will be applied to run the file containing the instructions. 

      Examples of interpreters;

      1. bash
      2. sh
      3. perl
      4. python
      5. python3
      6. expect

       

      If the instructions are for compiled code like C\C++ then a compiler is used to make an executable.

       

      Editors and IDEs:

      For both scripts and compiled code instructions are placed into a file using an editor or IDE.

      These tools are for writing unformatted text to a file.  Unformatted text refers to raw ASCII characters as opposed to a word processor file of formatted text.

       

      Editors:

      Examples of command line editors on the RPi are vim and nano.

       

      IDEs:

      The IDE is an integrated development environment for programming.  Examples of IDEs are shown here;

      1. Thonny
      2. Geany
      3. Greenfoot IDE
         

      The value of the IDE is the ability to run single lines or blocks of code for testing. Showing values contained in variables is also useful.

      The IDE is for development.  Development and testing often go together.  In a true Linux/IoT deployment environment there will not be a “run” button like in the IDE.  Testing often involves command line parameters.

      Command line parameters:

      Understanding command line parameters and using them is an important Linux/IoT skill.

      First examples of command line parameters, next how to get then in your scripts.

       

      pi@serverNine$ cat myBashScript.sh
      
      
      ARG1=$1
      VALX=$2
      
      echo $ARG1 $VALX
      
      
      pi@serverNine$ bash myBashScript.sh arg1 valx
      arg1 valx
      
      

      The command “bash” is the first thing. The bash interpreter is a program, programs take command line parameters.  Here the first parameter is the text file named myBashScript.sh containing the commands to be interpreted.

      The script myBashScript.sh takes in the command line parameters arg1 and valx.

      pi@serverNine$ cat myPythonScript.py
      import sys
      
      print(sys.argv[0])
      print(sys.argv[1])
      print(sys.argv[2])
      
      pi@serverNine$ python3 ./myPythonScript.py argx val1
      ./myPythonScript.py
      argx
      val1
      

      The python example above is essentially the same concept but using a different interpreter.

       

      Here is a Perl example;

      pi@serverNine$ cat myPerlScript.pl
      
      my ($argOne, $valx) = @ARGV;
      print "arg1='$argOne' valx='$valx'\n";
      
      
      pi@serverNine$ perl ./myPerlScript.pl argx val1
      arg1='argx' valx='val1'
      

      From the examples above a pattern emerges.

      interpreter ./script.ext arg1 arg2 arg3 argN

      Making the script executable:

      Here is the outcome of running a bash script before setting the executable bit;

      pi@serverNine$ ls -l myBashScript.sh
      -rw-r--r-- 1 pi pi 37 Aug 12 15:07 myBashScript.sh
      -----------
      ~
      pi@serverNine$ ./myBashScript.sh
      -bash: ./myBashScript.sh: Permission denied
      

       After setting the executable bit;

      pi@serverNine$ chmod +x myBashScript.sh
      -----------
      ~
      pi@serverNine$ ls -l myBashScript.sh
      -rwxr-xr-x 1 pi pi 37 Aug 12 15:07 myBashScript.sh
      -----------
      ~
      pi@serverNine$ ./myBashScript.sh
      -----------
      ~
      pi@serverNine$ ./myBashScript.sh arg1 valx
      arg1 valx
      

       

      After setting the executable bit;

      Notice that “bash” is not at the front of the command.

      What interpreter is being used here?

      The answer is the default interpreter. For many distributions of Linux bash is the default interpreter.  For some situations bash is not the default interpreter, for example, in a cronjob.

      Good programming practice is to set the interpreter when you first start writing your script.

      First is to find the interpreter you want.  Here is how for bash;

       

      pi@serverNine$ which bash
      /bin/bash
      

       

      Here is how to use it to set the interpreter in your bash script;

      #!/bin/bash
      
      ARG1=$1
      VALX=$2
      
      echo $ARG1 $VALX
      

      The script is saved with the first line as follows;

      #!/bin/bash

      A pattern emerges.

      For python use;

      which python

       

      For python3.

      which python3

       

      In the top of a python3 script set the interpreter from the output of the which command as follows;

      #!/usr/bin/python3

      The ‘#’ is a comment, therefore not a statement for the interpreter.

      The ‘!’ tells the OS that the interpreter to use is next, (/usr/bin/python3).

       

      The general method for scripts is as follows;

      Put the interpreter in the top of the file and set the executable bit.

       

      Compile C code:

      C++ Programming/Examples/Hello world

      https://en.wikibooks.org/wiki/C%2B%2...es/Hello_world

      // 'Hello World!' program
      
      #include <iostream>
      
      int main()
      {
        std::cout << "Hello World!" << std::endl;
        return 0;
      }
      

      Save the C++ code above in a file named hello.cc.

      Compile the code using the following command;

      g++ hello.cc -o hello
      

      Run the compiled code as follows;

      pi@serverNine$ ./hello 
      Hello World!
      

       

      Mix and match:

      Success is often based on the choice of the right tools for the job.

      Here the tools are the OS, in this case Linux, and the programming/scripting language.

      Often the bash script is used as the main driver, calling other programs/scripts as appropriate to the job.

      In this example the compiled program “hello” will be used as a stand in for sensor data collection.  A bash script is the main driver calling the sensor program “hello”.

      #!/bin/bash
      
      # Get data from sensor named "hello"
      DATA=$(./hello)
      
      echo data from sensor 
      echo $DATA
      
      

      Run the main driver script;

      pi@serverNine$ ./myBashScriptDriver.sh 
      data from sensor
      Hello World!
      

      In this next example the bash main driver is calling a python3 script called "helloWorld.py".

      #!/usr/bin/python3
      
      print("Hello World!")
      
      

      The modified bash main driver "myBashScriptDriver.sh".

      #!/bin/bash
      
      # Get data from sensor named "helloWorld.py"
      DATA=$(./helloWorld.py)
      
      echo data from sensor 
      echo $DATA
      
      

      Run the main driver script;

      pi@serverNine$ ./myBashScriptDriver.sh
      data from sensor
      Hello World!
      
      

      In the next example the python3 “mainDriver.py” script is used as the main driver for calling the bash script “myBashScriptDriver.sh”.

      #!/usr/bin/python3
      
      import subprocess
      
      # returns output as byte string
      returned_output = subprocess.check_output("./myBashScriptDriver.sh")
      
      print(returned_output.decode("utf-8"))
      
      

      Output from running the python3 “mainDriver.py” script;

      pi@serverNine$ ./mainDriver.py
      data from sensor
      Hello World!
      
      

      It is important to point out here that having a python3 main driver, calling a bash script that calls a python script is not an optimal approach.  These examples only serve to point out the possibilities in terms of the mix and match of tools and choices.

       

       

       

       

      • Was this article helpful?