Lect 17
Lect 17
1
05‐Oct‐24
Topics
• Decision Making
• The if-then statement
• The if-then-else statement
• Garbage-in / garbage-put
• User input validation
2
05‐Oct‐24
If I have more
than 50 QR, I will
buy a
cheeseburger.
true no action
Is money > 50 QR?
condition is satisfied
If the result of the
condition = false,
there will be no
action
3
05‐Oct‐24
Bash if statement
• When coding, you might need to make decisions based on certain
conditions.
• Conditions are expressions that evaluate to a Boolean expression
(true or false).
• Bash supports two versions of conditions;
bash have
2 types of • The if‐then else
decision • The if‐then‐else then
making
if
4
05‐Oct‐24
if [ condition ]
then pushing called indented
pushing the statement
a bit # Action when the condition is true
fi
5
05‐Oct‐24
for 2 actions
In the Action sections, lines are indented, i.e. there are few
spaces before each action statement. This spacing are not
mandatory, but recommended for clarity.
6
05‐Oct‐24
7
05‐Oct‐24
Odd or Even
Example: The following code (ex.sh) is an example script (code) that will
prompt you to input a number and then it checks whether the given
number is even.
#!/usr/bin/bash
# Prompt the user to enter a number
read ‐p "Enter Number: " n
# Check if the entered number is even
if [ $((n % 2)) ‐eq 0 ] $ bash ex.sh
then Enter a number: 6
echo " The number is even " The number is even
fi $
$ bash ex.sh
Enter a number: 5
$
8
05‐Oct‐24
Example: The following code (ex.sh) is an example script (code) that will
prompt you to input a number and then it checks whether the given
number is odd or even.
#!/usr/bin/bash
$ bash ex.sh
read ‐p "Enter Number: " num
Enter a number: 16
if [ $((num%2)) ‐eq 0 ] The number is even
then $
echo "The number is even"
else $ bash ex.sh
echo "The number is odd" Enter a number: 15
The number is odd
fi
$
echo "========================"
Example: The following code (ex.sh) is an example script (code) that will prompt you
to input a number and then it checks whether the given number is odd or even.
#!/usr/bin/bash
read ‐p "Enter Number: " num $ bash ex.sh
Enter a number: 6
if [ $num ‐ge 0 ]
The number is positive
then $
pushed echo "The number is positive"
to make clear that this is an action
else
$ bash ex.sh
echo "The number is negative" Enter a number: ‐9
fi The number is negative
$
echo "========================"
9
05‐Oct‐24
if condition
then align the commands
command1
command2
command3
...
commandN
fi useful for automation
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
if condition1
multiple if > multiple conditions
then
Action1
You can also put multiple if‐ fi
then statements in sequence.
if condition2
The next slide shown an then
example in which Bash can Action2
state the relation between fi
two numbers; greater than,
less than, or equal. if condition3
then
Action3
fi
10
05‐Oct‐24
#!/usr/bin/bash
read ‐p "Enter first Number: " n1 $ bash ex.sh
read ‐p "Enter second Number: " n2 Enter first number: 5
Enter second number: 2
if [ $n1 ‐gt $n2 ] The first number is larger
1 then $
echo " The first number is larger "
fi $ bash ex.sh
if [ $n1 ‐lt $n2 ] Enter first number: 5
then
Enter second number: 8
2 The second number is larger
echo "The second number is larger"
$
fi
if [ $n1 ‐eq $n2 ]
$ bash ex.sh
then Enter first number: 5
3
echo "They are equal" Enter second number: 5
fi They are equal
$
echo "========================"
11
05‐Oct‐24
Note: The odd/even code sample does not handle cases where the
user enters a non‐numeric value. If the user enters a non‐numeric
value, it may produce unexpected results.
it can understand the letters so it will count it
as a zero > that is why the output is even
$ bash b1.sh
Enter Number: abc
The number is even
$
The reason your script is outputting "The number is even" when you
enter abc, is that the script might simply be treating it as 0 (which is
considered even). Additional input validation could be added to
address this.
One of the most famous saying among computer programmers is “garbage in,
garbage out”. This saying, sometimes abbreviated as GIGO, refers to the fact that
computers cannot tell the difference between good data and bad data. If the user
provides bad data as input to a program, the program will process that bad data
and, as a result, will produce bad data as output.
12
05‐Oct‐24
In general users are the ones that are supposed to use an application
(program) and programmers are the ones that develops an application
to fulfill the needs of users. 8888888888888888888
/////////////////////////////////////
8888888888888888888
////////////////////////////////////
8888888888888888888
////////////////////////////////////
8888888888888888888
Input Validation
13
05‐Oct‐24
Input Validation
14
05‐Oct‐24
Negating Conditions
You can negate (convert true to false or false to true) conditions
by using the ! operator.
if [ condition ]
then
# Action when condition = true
means not
fi
if ! [ condition ]
then
!false=true # Action when condition = false
fi
if the condition is false then the action will happen
Where:
• The ! negates the condition, if the condition = false, then !false
is true.
• The condition matches the regex with the value of the variable num.
• The =~ operator is used to perform pattern matching with regular
expressions
• The double square brackets provide several features not available in
single square brackets, e.g. regular expression pattern matching.
15
05‐Oct‐24
#!/usr/bin/bash
read ‐p "Enter an integer: " num
$ bash ex.sh
Enter an integer: 5
The number is odd
========================
$ bash ex.sh
Enter an integer: 4
The number is even
========================
$ bash ex.sh floating point > dot is not acceptable since i didn't tell
Enter an integer: 2.8
Error: '2.8' is not a valid integer.
========================
$ bash ex.sh
Enter an integer: abc
Error: 'abc' is not a valid integer.
========================
16
05‐Oct‐24
Example
$ bash age.sh
What is your name? Huda
What is your year of birth? 1800 User input
===========================
Thank you Huda.
You are 224 years old.
17
05‐Oct‐24
18