Editors, IDEs, scripts, interpreters, and compiled code
- Page ID
- 33017
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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;
- bash
- sh
- perl
- python
- python3
- 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;
- Thonny
- Geany
- 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.