Professional Documents
Culture Documents
NGS3-2 - Introduction Bash Scripts
NGS3-2 - Introduction Bash Scripts
edu/2020/s
hell-tools/
INTRODUCTION
BASH SCRIPTS
Q
https://missing.csail.mit.edu/2020/s
hell-tools/
INTRODUCTION
BASH SCRIPTS
Q
1 INTRODUCTION AND WRITE FIRST BASH SCRIPT
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
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
7
1. INTRODUCTION AND 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
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.
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
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
22
3. IF STATEMENTS & 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
26
5. WHILE LOOPS
while [ condition ]
do
statement
command
done
27
5. WHILE LOOPS
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
30
6. FOR LOOPS
31
6. FOR LOOPS
32
6. FOR LOOPS
33
6. FOR LOOPS
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!