Lect 15
Lect 15
1
29‐Sep‐24
Topics
• What is an executable File?
• What is an interpreter / Compiler?
• The Bash Shell interpreter
• The shebang line
• Comments in Bash
• Executing bash files
• The tr (translate) command
2
29‐Sep‐24
In the computer lab, you have been working with two types of files,
data (e.g. DNA sequence, database) & log files (e.g. records the status
of the system). Next, you will be creating code files; i.e. files with Linux
commands and programming structures, e.g. condition and repetition.
record commands
if you save the command in a file then run it once and you'll get it> so you don't need to type every time
Process Automation
• A process is simply an executable which is a running
program.
• In Linux, process automation relies heavily on shell
scripting. This involves creating a file containing a
series of commands that can be executed together. CODE
It allows for the automation of repetitive tasks,
simplifying complex sequences of commands into a
single, executable script. An executable file
• By saving these commands in a file, you can repeat is a file which
the same sequence of steps multiple times only by contains code
executing the file.
3
29‐Sep‐24
echo "Hello"
Low‐level
High‐level Code (machine) Code
4
29‐Sep‐24
machine language
Translator
it's a program
job of the translator: convert
Low‐Level Language (LLL)
High‐Level Language (HLL)
5
29‐Sep‐24
execute my command
Linux Interpreters
only understand machine language that is why we need interpreter (bash shell)
6
29‐Sep‐24
Bash Shell
***************************
interpreter
Programmer Bash shell Linux OS
Executable Files
In Linux, executable files come in two forms:
• Code Files: These are text files, and you can use commands like cat
to view their contents. To run these files, you need to execute them
through an interpreter.
• Binary Files: These files contain machine code that has already been
translated by the interpreter. The system’s hardware can run this code
directly. To convert a code file into a binary file, use the shc
command, which needs to be installed first.
CODE BIN
not understandable to human
i can understand it and read it
7
29‐Sep‐24
#! <path‐to‐the‐interpreter>
8
29‐Sep‐24
#!/usr/bin/bash
# is not like #!
Comments in Bash Code
• A comment is a line that is not executed by the shell.
• Comments start with a # in bash scripting.
• Except for the shebang, any line that begins with a # is a comment
and will be ignored by the interpreter.
Do not put a comment BEFORE the shebang (#!) line. The shebang
line must always be the first line in a script
9
29‐Sep‐24
10
29‐Sep‐24
Formatting
code file should be readable by other not only you
There are many areas in Bash scripts where formatting is important.
• Typically it involves spaces and either the presence or absence of a
space can be the difference between the command working or not.
You might spend quite a bit of time looking at a piece of code that
looks perfectly fine but isn't working. Then, you discover that the
reason was a space either should or shouldn't be there.
• Indenting of code is another area of formatting that is important.
Although, in Linux, indenting is not required but it does make your
code easier to read and find simple errors.
• Comments are very helpful in documenting the code, and it is a good
practice to add them to help others understand the code.
11
29‐Sep‐24
Practical Code
Edit Bash script to count the number of nucleotides (nt); A, C, G and T
in a DNA sequence. Use this data to test your code.
• Input: A text file with DNA sequence
when i write it it should give me the total DNA sequence
• Output: Nucleotide (nt) count count and how many A C G T
==================================
Total count: . . .
A‐nt count: . . .
C‐nt count: . . .
G‐nt count: . . .
T‐nt count: . . .
==================================
Input Sample;
ACTTATTGTA
The expected output; with one program i can get all these
==================================
Total count: 10
A‐nt count: 3
C‐nt count: 1
G‐nt count: 1
T‐nt count: 5
==================================
Note:
• The output can depends on the given sequence
• Use the TR (translate) command to delete the end of line character.
12
29‐Sep‐24
Step‐2: change
$ chmod u+x
permission mode
Step‐3: Run the code $ ./file‐name
13
29‐Sep‐24
The last line is a pipe, where the cat generates the contents of the dna10
data file, the tr (with d option) deletes the new‐line character while the
wc (with c option) counts the number of characters; i.e. the pipe displays
the length of the DNA sequence.
14