Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

https://missing.csail.mit.

edu/2020/s
hell-tools/

INTRODUCTION
BASH SCRIPTS

Presenter: Nguyen Duy Thanh


th
9 January, 2021

Q
https://missing.csail.mit.edu/2020/s
hell-tools/

INTRODUCTION
BASH SCRIPTS

Presenter: Nguyen Duy Thanh


th
9 January, 2021

Q
1 INTRODUCTION AND WRITE FIRST BASH SCRIPT

2 VARIABLES AND MATH FUNCTIONS

3 IF STATEMENTS & UNIVERSAL UPDATE SCRIPT


CONTENT
4 EXIT CODES

5 WHILE LOOPS

6 FOR LOOPS
3
1. INTRODUCTION AND WRITE FIRST BASH SCRIPT

Bash script

- A Bash script is a plain text file which contains a series of commands. These
commands are a mixture of commands we would normally type ouselves on the
CML (such as ls or cp for example) and commands we could type on the CML

- Written in the Bash programming language and extension of .sh


Example: myscript.sh

Anything you can run normally on the CML can be put into a script and it will
do exactly the same thing. Similarly, anything you can put into a script can also
be run normally on the CML and it will do exactly the same thing.
5
1. INTRODUCTION AND WRITE FIRST BASH SCRIPT

Another term is executing the script. Before we can execute a script it must have the
execute permission set. If you forget to grant this permission before running the
script you'll just get an error message telling you as such and no harm will be done.

6
1. INTRODUCTION AND WRITE FIRST BASH SCRIPT

WRITE FIRST BASH SCRIPT


- On most Linux systems nowadays the default shell is Bash
- Depending on which distribution of Linux. We need to be running Bash

7
1. INTRODUCTION AND WRITE FIRST BASH SCRIPT

WRITE FIRST BASH SCRIPT

8
2. VARIABLES AND MATH FUNCTIONS

VARIABLES

All disappeared

Type exit to exit out of terminal that's going to close the entire window and
then I'll open another terminal => What happened here? 9
2. VARIABLES AND MATH FUNCTIONS

VARIABLES

10
2. VARIABLES AND MATH FUNCTIONS

VARIABLES
If you're curious which variables which your session you could find out by
typing EnV

11
2. VARIABLES AND MATH FUNCTIONS

MATH FUNCTIONS
In Python if you wanted to add 30 plus 10: 30 + 10
But that's not going to work in bash. In bash we have the evaluate expressions
command which is abbreviated expr

12
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

- If statements (related case statements) allow us to make decisions in our Bash


scripts. They allow us to decide whether or not to run a piece of code based upon
conditions that we may set.
- If statements, combined with loops allow us to make much more complex scripts
which may solve larger tasks.

Line 5 - Let's see if the first command line argument is equal to 20


001=1 is false 001 -eq 1 is true
Line 6 and 7 - Will only get run if the test on line 5 returns true.
Line 8 and 9 - Will only get run if the test on line 5 returns false.
Line 10 - fi signals the end of the if statement. All commands after
this will be run as normal.

13
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Example 1 if [ conditions ]
then
ouput
fi

14
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Example 2
! - the EXPRESSION is false.

if [ $number -ne 20 ] means not equal


if [ $number -gt 20 ] means INTEGER1 is numerically greater than INTEGER2
15
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Example 3

16
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

The square brackets ( [ ] ) in the if statement above are actually a reference to the
command test.

17
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Example 4

The variable $? holds the exit status of the


previously run command (in this case test). 0
means TRUE (or success). 1 = FALSE (or
failure).

1
0
1
0

18
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Indenting

You'll notice that in the if statement above we indented the commands that were run if
the statement was true.

This is referred to as indenting and is an important part of writing good, clean code.

The aim is to improve readability and make it harder for us to make simple, silly
mistakes.

19
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Nested if statements

Line 4 - Perform the following, only if the first command line argument is greater than 100.
Line 7 - This is a light variation on the if statement.
Line 9 - Only gets run if both if statements are true. 20
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT
Universal update script

21
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Universal update script

22
3. IF STATEMENTS & UNIVERSAL UPDATE SCRIPT

Universal update script

23
4. EXIT CODES

Exit codes allow us to do is actually determine whether the command that we've
just entered was successful or not successful
Normally, 0 corresponds to successful processing , and 1 or more corresponds
to failed processing

#!/bin/bash
Everything is
cd $memory/bad_memory gone!!!
rm *

24
4. EXIT CODES

2 statements true and false above do nothing except return exit code. What I want
you to notice here is $?- the environment variable is used to store the exit code of
the last command executed before

25
4. EXIT CODES

You can control exit status for scirpts by “exit”

26
5. WHILE LOOPS

bash while loop syntax

The syntax is as follows:

while [ condition ]
do
statement
command
done

27
5. WHILE LOOPS

Reading a file with a while loop

28
5. WHILE LOOPS
Infinite while loop
To create an infinite loop using a while loop statement. We don’t need to put any
condition in the while loop and hence the loop iterates infinitely or the condition
we set is always true

29
6. FOR LOOPS

Bash For Loop Syntax


The for loop iterates over a range of values and executes a set of commands.

for variable_name in value1 value2 value3 .. n


do
command1
command2
commandn
done

30
6. FOR LOOPS

Bash For Loop with Ranges

31
6. FOR LOOPS

Bash For Loops with Arrays


The @ operator accesses or targets all the elements. This makes it possible to
iterate over all the elements one by one.

32
6. FOR LOOPS

Use the ‘Continue’ statement with Bash For Loop

Line 2: Marks the beginning of the for loop and


iterate the variable n from 1 to 10.

Line 4: Checks the value of n and if the variable is


equal to 6, the script echoes a message to stdout
and restarts the loop at the next iteration in line 2.

Line 9: Prints the values to the screen only if the


condition in line 4 is false.

33
6. FOR LOOPS

Use the ‘Break’ statement with Bash For Loop

Line 15: Marks the beginning of the for loop and


iterate the variable n from 1 to 10.
Line 17: Checks the value of n and if the variable
is equal to 6, the script echoes a message to stdout
and halts the iteration.
Line 22: Prints the numbers to the screen only if
the condition in line 4 is false.

34
6. FOR LOOPS

Classwork

Using If statements write a script file to search the Downloads directory. Returns
"Directory’s exists." if Condition is true, return "Directory’s not exists" otherwise.

Homework
Write a script file that prints all odd and even
numbers divisible by 10 within the range 10 to 100
(Prints only results less than or equal to 70 and the
distance between the two numbers is 15)

35
THANKS YOU FOR EVERY ONE!

You might also like