-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparisons.sh
More file actions
55 lines (38 loc) · 1.43 KB
/
comparisons.sh
File metadata and controls
55 lines (38 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/bash
#Conditions used are:
######## Arithmetic Conditions ##########
#1. -eq : equal to
#2. -ne : not equal to
#3. -gt : greater than
#4. -ge : greater than or equal
#5. -lt : less than
#6. -le : less than or equal
#Note: Above are used when condition is in square brackets.
#example : [ 10 -ge 6 ]
# In order to use
# < , > , = , <= , >=
# we need to use ((__)) instead of square brackets []
# example (( 10 >= 6 ))
######### String Comparisons ##########
# = or == : equal to # used as [ "$a" = "$b" ]
# != : not equal to
# < or > : compares ASCII values # used as [[ "$a" < "$b" ]]
# -z : To check if string is NULL that is have zero length
########### AND OR ###########
# 1. And : [ $age -gt 18 ] && [ $age -le 60 ]
# [ $age -gt 18 -a $age -le 60 ] # -a flag is used for and operation
# [[ $age -gt 18 && $age -le 60 ]]
# 2. Or : Same as and except that '||' is symbol for or and -o is the flag.
# Arithmetic operations on numbers
echo 1+1 # will print 1+1
num1=20
num2=5
echo $(( num1 + num2 )) # print 25
#or
echo $(expr $num1 \* $num2) # \ is placed before * so that multiplied can be performed else * will be printed as it is.
#Arithmetic operations on floating point numbers
num_1=20.5
num_2=15.6
echo "$num_1+$num_2" |bc # bc is used to perform operations on floating numbers
echo "scale=2; 20.824/3.6" |bc # scale is used
echo "scale=2; sqrt($num_1)" |bc -l # -l is used to call the math library