0% found this document useful (0 votes)
18 views14 pages

Lect 15

Uploaded by

ixvvy1012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

Lect 15

Uploaded by

ixvvy1012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

29‐Sep‐24

[15] Executable Files

CMPS 101 : Introduction to Computer Science


Fall 2024
Eng. Amelle Bedair, [email protected]

Quiz#3 on 03 ‐ Oct ‐ 2024


Material: Lecture Notes; 10 & 11 + Lab 04
Type: Online MCQ. Bring your laptop

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

Programming Language (from Lecture 6)

A programming language is a set of instructions written by a


programmer to deliver instructions to the computer to perform and
accomplish a task. Computer programmers write, modify, and test
code and scripts that allow computer software and applications to
function properly.
Programmers turn the
designs created by software
developers and engineers
into instructions that a
computer can follow.

2
29‐Sep‐24

CMPS101 Computer Files

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.

Data Log Code

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

The computer does not understand the commands you write in


human‐like statement. For the computer to execute and calculate the
result of the statement above, the compiler/interpreter has to convert
it first to binary.

Computers can understand only "machine language"

Levels of Programming Language


There are two levels of programming languages;
• A high‐level is human readable
• A low‐level is computer readable
like translator

echo "Hello"

Low‐level
High‐level Code (machine) Code

4
29‐Sep‐24

High‐level languages are designed to be closer to human language and more


abstracted from the computer’s hardware. In contrast, low‐level languages
are designed to be closer to machine language and hardware.
Therefore, for the computer to execute the code, each commands should be
translated (converted) from high‐level (text‐based) code to low‐level (binary‐
based)
when i type commands (high level)

machine language

Translator
it's a program
job of the translator: convert
Low‐Level Language (LLL)
High‐Level Language (HLL)

Translators are of two types;


• A compiler translates code from a HLL into LLL before the program
execution.
• An interpreter translates code from HHL into LLL during execution.

take the doc and translate then give me result


wait till i finish
Compiler
while i'm doing it
Interpreter
while i'm (typing-talking) they interpret immediately

5
29‐Sep‐24

Programming Language 4 Bioinformatics

execute my command

Linux Interpreters
only understand machine language that is why we need interpreter (bash shell)

Linux supports a wide variety of interpreters, which allow you to run


scripts or programs written in different languages. Below is a list of
commonly supported interpreters in Linux:
• Bash (Bourne Again Shell): /bin/bash
• Sh (Bourne Shell): /usr/bin/sh
• Ksh (Korn Shell): /usr/bin/ksh
• Python: /usr/bin/python3
• R (Statistical Computing): /usr/bin/Rscript

6
29‐Sep‐24

Bash Shell
***************************

Bash (Bourne Again SHell) is a command‐line interpreter and scripting


language used in Linux operating systems. Bash is a shell, meaning it
acts as a layer between the user and the operating system, allowing
users to execute commands, run programs, and perform various tasks.

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

How to Create Code File


• You can use Vim text editor to edit the Bach code means that this is a bash file
• It is convention to give files that are Bash scripts an extension of .sh.
As you would be aware, Linux is an extensionless system so a the
code file doesn't necessarily have to have this characteristic in order
to work, but it helps to keep track of things. It is really only there for
humans to use as a reference and most command lines and programs
will not care in the least. It won't hurt. If the script name contains an
extension, however, you must use it. It is part of the filename.
• Anything you can run normally on the command line can be put into a
script and it will do exactly the same thing. In this sense, if you know
how to do stuff at the command line then you already know a fair bit
in terms of Bash scripting.

first line always should be: The Shebang


• Bash scripts should start with a shebang.
• The shebang specifies the interpreter that should be used to execute
the script (code / program).
• The shebang is a combination of bash # and bang !
• The shebang should have no spaces and must be on first line
• The shebang is followed by the absolute path to the interpreter that
will translate the Linux commands.
• The general format is:

#! <path‐to‐the‐interpreter>

8
29‐Sep‐24

should the first line name of the translator (interpreter)

• You can find your bash


shell path (which may
vary) using the which
bash command.
• Below is the shebang
statement which we are
going to use in the lab

#!/usr/bin/bash

now linux will give it to bash for translating

# 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.

only for documentation


#!/user/bin/bash
# My first Bash Script This is a comment
echo " Hello "

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

How to Run a Code file

There are two ways to execute (run) a Bash file;


• With bash [file‐name]. This method does not require the file to
have executable permissions, because the Bash interpreter is
handling the file. do not need permission so the file can be executed
• With the ./file‐name. This method tells the system to execute the
file as a standalone program. The file must have executable
permissions. If the script has no shebang, it will be executed using the
default shell (which is usually Bash, but this depends on the system).
need permission so the file can be executed

Make the Code File Executable


• After creating the code file, look at its permission using the ‐ls ‐l
command. If as owner, you don’t have the execute permission; i.e. file
owner permission is rw‐, then use the chmod command to add the
execute permission;
chmod u+x <code‐file‐name>
• Note: After changing file permission, use the ‐ls ‐l command to
make sure that you have rwx permission.
• If you do not add the execution permission, Bash will generate an
error message when trying to execute the file
Permission denied

10
29‐Sep‐24

Example of a Bash Script


show day date and time
#!/bin/bash
# Display the day of the week script for myself
echo username: $ bash example.sh
echo ‐n "Day : "
date +"%A"
sleep 1 animation effect (one letter the wait for 1 sec and then the 2nd line)
# Display the date in dd‐mm‐yyyy format
echo ‐n "Date: "
date +"%d‐%m‐%Y"
sleep 1
• The echo command with the –n
# Display the time (in 24‐hour format)
option is to print the message and
echo ‐n "Time: "
keeps the cursor on the same line
date +"%H:%M:%S"
sleep 1
• The sleep command inserts a
echo pause in seconds.

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

The tr (translate) Command


In Linux, the tr (translate) command is used to translate (convert) or
delete characters from the input it receives.
Examples
• It is recommended to use the tr command to delete the end‐of‐line
character, easier than using the sed command  tr ‐d '\n'
• To delete all characters except a certain character, use the delete (d)
with the complement (c) options, e.g. to delete all characters except
the character 'A' tr –dc 'A' tr to count how many A's T's ........ do not use file
name in this
• Note: Use the tr command in a pipe after any command which
generates data, e.g. cat, sed, ls, etc.

Running a Bash Script


#!/bin/bash
Step‐1: Edit the code echo "========================"
using the Vim editor. print on one line and don't go to the 2nd
echo ‐n "DNA Sequence: "
The dna10 is a text file
with 10 nucleotides cat dna10
echo "========================"

Step‐2: change
$ chmod u+x
permission mode
Step‐3: Run the code $ ./file‐name

Step‐4: Observe the =========================


output DNA Sequence: ACTTATTGTA
=========================

13
29‐Sep‐24

It is recommended to write few lines, then execute. By this way, it


would be easy to fix the bugs.
#!/bin/bash
echo "========================" ===================
echo ‐n "DNA Sequence: " DNA Sequence:
cat dna10 ACTTATTGTA
echo "========================" ===================
echo ‐n "Total Count: " word count Total Count: 10
cat dna10 | tr ‐d '\n' | wc –c ===================
print all the content of the file > delete the end of line
echo "========================"

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

You might also like