-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional_statements.sh
155 lines (129 loc) · 2.74 KB
/
conditional_statements.sh
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/bash
#There are four types of Conditional statements
#1. If-statement
#Synatx:
# if [ condition ]
# then
# statements
# fi #fi marks the end
#Example:
count=10
if [ $count -ge 9 ]
then
echo " Condition is true"
fi
count=20
if (( $count < 21))
then
echo "$count is less than 21. Condition is true"
fi
#2. If-else
#Syntax:
#if [ condition ]
#then
# statements
#else
# statements
#fi
#Example:
read -p "Enter your age:" age
if [ $age -ge 18 ]
then
echo "You can vote in the elections"
else
echo "You are under 18 hence cannot vote"
fi
#3. If-else-if ladder
#Syntax:
#if [ condition ]
#then
# statements
#elif [ condition ]
#then
# statements
#else
# statements
#fi
#Example:
read -p "Enter your age:" uage
if [ $uage -ge 18 ]
then
echo "You can vote and drive with license."
elif [ $uage -ge 16 ] && [ $uage -le 18 ]
then
echo "You cannot vote but drive with learning license."
else
echo "You are under age. You cannot drive or vote."
fi
#4. Switch statement(Case)
# Synatx:
# case expression in
# pattern1 )
# statements ;;
# pattern2 )
# statements ;;
# pattern n )
# statements ;;
# esac
#Example:
echo -e "Enter any number[1-7]: \c"
read value
case $value in
"1" )
echo "It is Monday." ;;
"2" )
echo "It is Tuesday" ;;
"3" )
echo "It is Wednesday" ;;
"4" )
echo "It is Thursday" ;;
"5" )
echo "It is Friday" ;;
"6" )
echo "It is Saturday" ;;
"7" )
echo "It is Sunday" ;;
*)
echo "Incorrect input";;
esac
# File realted examples for conditional statements
echo -e "Enter name of the file : \c " # \c is used to stay in the same line. -e is used to make use of the \c.
read file_name
if [ -e $file_name ] #-e flag is used to checck if the file exist or not
then
echo " $file_name found "
else
echo " $file_name does not exist "
fi
#-f flag is used to check if file exist and if it is a regular file or not
#-d flag is used for directory
#-b flag is used to check if it is block special file
#-c flag is used if it is character special file
#-s flag is usd to check if the file is empty
echo -e "Enter name of the file : \c " # \c is used to stay in the same line. -e is used to make use of the \c.
read file_namee
if [ -f $file_namee ] #-e flag is used to checck if the file exist or not
then
echo "It is a regular file"
else
echo "It is not a regular file"
fi
echo -e "Enter any directory: \c"
read direc
if [ -d $direc ]
then
echo "It is a directory"
elif [ -f $direc ]
then
echo "It is not a directory, it is a file"
else
echo "It is neither a directory nor a file"
fi
echo -e "Enter the name of the file: \c "
read fname
if [ -s $fname ]
then
echo "File is empty "
else
echo "File is not empty"
fi