-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_Using_conditional_statement.sh
More file actions
322 lines (211 loc) · 7.82 KB
/
5_Using_conditional_statement.sh
File metadata and controls
322 lines (211 loc) · 7.82 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Using Conditional Statements
*****************************
Using if ... then ... fi
*************************
**
if experssions
then
command 1
command 2
fi
**
else can be included to define what happens if expression is not true
if expression
then
command 1
else
command 2
fi
**
elif is used to nest a new if statement within the if statement
while if has to be closed with a fi, elif does not need a seperate fi
if expression
then
command 1
elif expression 2
then
command 2
fi
**
#!bin/bash
if [ -d $1 ] #This is a typical test case to see if the input is a directory
then
echo $1 is a directory #THis is the output being displayed on the screen. you can use the cat command to write to a file
elif [ -f $1 ] #This is a typical test case to see if the input is a file
then
echo $1 is a file
else
echo $1 is not a file, nor a directory #If this case is met you will receive a ambigious message. #Is not a directory, you have to write code that does perfectly does error handling
fi
'''
How to run this
create the file, in this case we will call the file *ifdir*
running the file
ifdir /etc/passwd
ifdir /etc
'''
Using && and || are the logical AND and OR
********************************************
use them as a short notation for if ... then ... fi constructions
when using && , the second command is executed only if the first returns an exit code zero
[ -z $1 ] && echo $1 is not defined #This is to check if $1 is empty or not
when using ||, the second command is executed only if the firse command does not return an exit code 0
[ -f $1 ] || echo $1 if not a file
&& and || Examples
#!/bin/bash
[ -z $1 ] && echo no argument provided && exit 2
[ -f $1 ] && echo $1 is a file && exit 0
[ -d $1 ] && echo $1 is a directory && exit 0
#exit 0 meand the operation was successful
#exit 1 means that the operation was not successful
#exit 2 means that the operation was also not successful based on errors
#Exit code 0 Success
#Exit code 1 General errors, Miscellaneous errors, such as "divide by zero" and other impermissible operations
#Exit code 2 Misuse of shell builtins (according to Bash documentation) Example: empty_function() {}
modifying this to catch the other exceptions
#!/bin/bash
[ -d $1 ] && echo $1 is a directory
[ -f $1 ] && echo $1 is a file || echo $1 is not a file, nor a directory # This catches the exception that says if it is a file or a directory
'''
[ -f $1 ] --- condition to check if the input is a file
&& echo $1 is a file --- if that condition returns as true then return this line executes it echoes that the input is a file
|| echo $1 is not a file, nor a directory ---- If the condition is not true it echoes that it is not a file nor a directory,
having in mind that we checked on a previous line if this was a file or a directory
'''
Using For Statements
**********************
for statements are useful to evaluate a range or series
for i in something
do
command 1
command 2
done
for i in `cat/etc/hosts`; do echo $i; done #The back tick here is command substitution
for i in `cat users`; do echo useradd $i; done
for i in {1..5}; do echo $i; done
it is common to use a variable i in a for loop, but any other variable can be used instead
for i in `cat rakers`; do echo useradd $i; done
for i in {200..210}; do ping -c 192.168.122.$i; 2> dev/null && echo 192.168.122.$i is available done
By using the line "2> dev/null" we are redirececting the out put to null, that is it will ortimacally be deleted and not reccorded anywhere, sonce it is not useful
#we may experience quite a different error here as we see that the output is more than we expected
#standard output not error output
for i in {200..210}; do ping -c 192.168.122.$i; > dev/null && echo 192.168.122.$i is available done
#By removing the two from that line we see that what we get is only aresponse when the condition is correct which is the goal.
**
Example with For statement
#!/bin/bash
#Script that counts files
echo which directory do you want to count?
read DIR
cd $DIR
COUNTER=0
for i in *
do
counter=$((COUNTER+1))
#echo I have counted $COUNTERfiles in this directory #Doing it here will count each file and print it to th screen
done
echo I have counted $COUNTER files in this directory #Doing it here will give a summary of all the files counted in the directory location
Using Case statements
*********************
case is used if you\'re expecting specific values
Thee most common examples is in the legacy system V/Upstart nit scripts in /etc/init.d
case $VAR in
yes) #The open braces here shows the end of the first case
echo ok;; #The double semicolon here ends the current open cases for that condition
no|nee)
echo too bad;;
*) #This symbol '*', pronounced as star. represents every other thing
echo try again
;;
esac #This ends the case block.
#!/bin/bash
VAR=$1
case $VAR in
yes)
echo ok;;
no|nee)
echo too bad;;
*)
echo try again;;
esac
:::::::::::::::::::::::::
cd /etc/init.d :::::
vim network :::::
:::::::::::::::::::::::::
Using While and Until
*********************
while is used to execute commands as long as a condition is true
until is used to execute commands as long as a condition is false
while | until condition
do
command
done
while true; do true: done & #This will make your system run till it's maximum cpu capacity. what you wan to do is kill the PID(Process ID)
#!/bin/bash
COUNTER=0
while true
do
sleep 1
COUNTER=$(( COUNTER + 1 ))
echo $COUNTER seconds have assed since started this script
done
#!bin/bash
until users | grep $1 > /dev/null #The output of the script is redirected to the null directory #
#This command will only evaluate to true when the user logs in
do
echo $1 is not logged in yet
sleep 5
done
echo $1 has just logged in
mail -s "$1 has just logged in" root < . #here an email will be sent using the linux internal mail command.
#The mail will be sent to root and it finishes with a '<.' statement
#mail -s hello root
#We should be expecting somethng like this, as the body of the email :
this is the message itself that will be embedded as the body of the email
. EOT #This will end the file
Exercise 5
************
*A customer has exported a long list of LDAP usernames. These usernames are stored in the file ldapusers.
In this file, every user has a name in the format cn=lisa,dc=example,dc=com.
write a script that extracts the username only (Lisa) from all of these lines and write those to a new file.
Based on this new file, create a local user account on your Linux box
*Note: while testing it\'s not a really smart idea to create the users account directly.
Find a solution that proves that the script works, without polluting your system with many usernames
Sample usernames:
cn=lisa,dc=example,dc=com
cn=linda,dc=example,dc=com
cn=laura,dc=example,dc=com
cn=Tyrell,dc=example,dc=com
cn=Leshawn,dc=example,dc=com
'''
vim USERACCOUNTS
USRS="User directory"
names="newfiles.txt"
#!/bin/bash
for i in $USRS;
do
USER=${i%%,*}
USER=${USER#*=}
echo ${USER} >> $names
done
for i in $(cat names)
do
echo useradd $i
done
exit 0
'''
Example Solution
#!/bin/bash
#Extract the user names
for i in $(cat ldapusers)
do
USER=${i%%,*}
USER=${USER#*=}
echo $USER >> users
done
#show that users creation will work
for j in $(cat users)
do
echo useradd $j
done
exit 0