-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-script
More file actions
651 lines (482 loc) · 13.6 KB
/
shell-script
File metadata and controls
651 lines (482 loc) · 13.6 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
--> shell is useful to automate our regular tasks,
monitoring several server resources like cpu utilization and memory utilization
Q. How to know how many shell types are supporting by your linux server?
Ans: check /etc->shells file
--> To know which shell we are using
[root@ip-172-31-35-179 ~]# echo $SHELL
/bin/bash
[root@ip-172-31-35-179 ~]# echo $0
-bash
--> Switch to another shell
serverresourcesmonitor.sh
THRESHOLD=80
->df
->free
->top
-->mail
-->hostname
-->ifconfig
step-1) use any editor like vi to write shell script
syntax: vi <script_name>
step-2) after writing shell script set execute permission for your script as follow
syntax: chmod u+x <scrpitFileNAME>
step-3) execute your script as
syntax: sh <script-name>
./<script-name>
. <script-name>
#!/bin/bash
#!-->called as shebang line
------------------------------------------------------------------------CLASS-1 END------------------------------------------------------------------------
# sh -x hello.sh --> running the shell script in debug mode.
->If we want run a particular code in debug mode add set -x and set +x in shell script.
Example:
-------
#!/bin/bash
echo "hello guys"
set -x
echo "gd mrng"
echo "welocme to shell script"
set +x
echo "Today date is"
date
COMMENTS:
---------
# ->Single line comment
<<MLC
......
....... ->Multi-line comment (first and last same name should be there)
.......
.......
MLC
variables;
----------
a=10 (no space in between)
name=venki (no space in between)
# printenv or # env (to list the system defined variables)
Q. is is possible to change system defined variable values?
Ans: yes
Example:
--------
[ec2-user@ip-172-31-35-179 ~]$ echo $HISTSIZE
1000
[ec2-user@ip-172-31-35-179 ~]$ export HISTSIZE=1100 (this is temporary change)
[ec2-user@ip-172-31-35-179 ~]$ echo $HISTSIZE
1100
--> If we want change those values permanantely add those values in .bash_profile for user specifc or add in cd /etc->profile file for all the users.
COMMAND LINE ARGUMENTS:
-----------------------
-->while executing the shell script the values are passed through the command prompt
Example:
-------
[ec2-user@ip-172-31-35-179 ~]$ cat file1.sh
echo "file-name" $0
echo "a value is" $1
echo "b value is" $2
echo "c value is" $3
[ec2-user@ip-172-31-35-179 ~]$ sh file1.sh 11 12 13
file-name file1.sh
a value is 11
b value is 12
c value is 13
$0-->gives file name
$1-->first argument
$2-->second, $3-->third....,,,
$#-->Number of arguments
$*-->All the args (as a one string)
$@-->All the args(Each arg as a one string)
# echo $$ -->Displays process id
# $?-->previous command execution status(0=success)
0--> success
127-->standard error code for command not found
[ec2-user@ip-172-31-35-179 ~]$ cat file1.sh
echo "file-name: " $0
echo "a value is: " $1
echo "b value is: " $2
echo "c value is: " $3
echo "number of args: " $#
echo "all the args using *: " $*
echo "all the args using @: " $@
echo "process ID: " $$
date
echo "previous command execution status: " $?
[ec2-user@ip-172-31-35-179 ~]$ sh file1.sh 1 2 3
file-name: file1.sh
a value is: 1
b value is: 2
c value is: 3
number of args: 3
all the args using *: 1 2 3
all the args using @: 1 2 3
process ID: 4470
Tue Aug 2 12:08:21 UTC 2022
previous command execution status: 0
[ec2-user@ip-172-31-35-179 ~]$ cat file1.sh
echo "file-name: " $0
echo "a value is: " $1
echo "b value is: " $2
echo "c value is: " $3
echo "number of args: " $#
echo "all the args using *: " $*
echo "all the args using @: " $@
echo "process ID: " $$
Date
echo "previous command execution status: " $?
[ec2-user@ip-172-31-35-179 ~]$ sh file1.sh 1 2
file-name: file1.sh
a value is: 1
b value is: 2
c value is:
number of args: 2
all the args using *: 1 2
all the args using @: 1 2
process ID: 4532
file1.sh: line 9: Date: command not found
previous command execution status: 127
Needs to pass 3 args to execute shell script:
if [ $# -eq 3 ]
then
echo "file-name: " $0
echo "a value is: " $1
echo "b value is: " $2
echo "c value is: " $3
echo "number of args: " $#
echo "all the args using *: " $*
echo "all the args using @: " $@
echo "process ID: " $$
Date
echo "previous command execution status: " $?
else
echo "usage sh $0 arg1 arg2 arg3"
fi
----------------------------------------------------------------CLASS-2 END--------------------------------------------------------------------------------
String:
-------
->string is nothing but one or more characters enclosed in a single or double quotes.
Length of string:
----------------
[ec2-user@ip-172-31-35-179 ~]$ cat string.sh
string_var=venki
echo "length of string is :" ${#string_var}
[ec2-user@ip-172-31-35-179 ~]$ sh string.sh
length of string is : 5
[ec2-user@ip-172-31-35-179 ~]$ cat string.sh
string_var='venki potla'
echo "length of string is :" ${#string_var}
[ec2-user@ip-172-31-35-179 ~]$ sh string.sh
length of string is : 11
SubString:
---------
Example:
--------
[ec2-user@ip-172-31-35-179 ~]$ cat string.sh
string_var='venki potla is very good boy'
echo "sub string is :" ${string_var:4} (ignores first 4 characters)
[ec2-user@ip-172-31-35-179 ~]$ sh string.sh
sub string is : i potla is very good boy
echo ${string_var:n1:n2} n1->from starting how many characters ignore
n2-> from there how many characters needs to display
Example:
--------
[ec2-user@ip-172-31-35-179 ~]$ cat string.sh
string_var='venki potla is very good boy'
echo "sub string is :" ${string_var:6:5}
[ec2-user@ip-172-31-35-179 ~]$ sh string.sh
sub string is : potla
Example:
--------
[ec2-user@ip-172-31-35-179 ~]$ cat string.sh
string_var='venki potla is very good boy'
echo "sub string is :" ${string_var:(-3)}
[ec2-user@ip-172-31-35-179 ~]$ sh string.sh
sub string is : boy
Arthimatic operations:
----------------------
Example:
---------
[ec2-user@ip-172-31-35-179 ~]$ cat arthmetic.sh
echo "arithmetic operations"
expr 2 + 3
expr 2 - 3
expr 2 \* 3 (space is mandatory)
expr 10 / 2
expr 20 % 3
[ec2-user@ip-172-31-35-179 ~]$ sh arthmetic.sh
arithmetic operations
5
-1
6
5
2
-> echo "the addition is" `expr 2 + 3`
[ec2-user@ip-172-31-35-179 ~]$ cat aa.sh
expr $1 + $2
[ec2-user@ip-172-31-35-179 ~]$ sh aa.sh 12 13
25
user interaction using read:
----------------------------
[ec2-user@ip-172-31-35-179 ~]$ cat arthmetic.sh
echo "enter a"
read a
echo "enter b"
read b
expr $a + $b
expr $a - $b
expr $a \* $b
expr $a / $b
expr $a % $b
[ec2-user@ip-172-31-35-179 ~]$ sh arthmetic.sh
enter a
2
enter b
3
5
-1
6
0
[ec2-user@ip-172-31-35-179 ~]$ cat aa.sh
echo "please enter your name"
read name
echo "the name you entered is:" $name
[ec2-user@ip-172-31-35-179 ~]$ sh aa.sh
please enter your name
venky
the name you entered is: venky
---------------------------------------------------------------CLASS-3 END----------------------------------------------------------------------------------
ARRAY:
-----
[ec2-user@ip-172-31-35-179 ~]$ cat readex.sh
echo "please enter devops tools"
read -a devopsTools
echo "the tools are:" ${devopsTools[*]}
echo "the 4th value is" ${devopsTools[3]}
[ec2-user@ip-172-31-35-179 ~]$ sh readex.sh
please enter devops tools
git maven nexus sonarqube tomcat docker k8s
the tools are: git maven nexus sonarqube tomcat docker k8s
the 4th value is sonarqube
[ec2-user@ip-172-31-35-179 ~]$ cat read4.sh
echo "favourite color"
read (not passing anything with read so use $REPLY to get that value)
echo "favorite color is :" $REPLY
[ec2-user@ip-172-31-35-179 ~]$ sh read4.sh
favourite color
red
favorite color is : red
[ec2-user@ip-172-31-35-179 ~]$ cat example.sg
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
[ec2-user@ip-172-31-35-179 ~]$ sh example.sg
please enter your name: venki
please enter password: password@123
username is: venki
password is: password@123
[ec2-user@ip-172-31-35-179 ~]$ cat example.sg
read -p "please enter your name: " username
read -sp "please enter password: " password
echo "username is: " $username
echo "password is:" $password
[ec2-user@ip-172-31-35-179 ~]$ sh example.sg
please enter your name: venki
please enter password: username is: venki
password is: venki@123
input and output redirection:
------------------------------
> -->redirect standard output
>> -->append the standard output
< -->standard input
[ec2-user@ip-172-31-35-179 ~]$ cat example.sh
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
date
[ec2-user@ip-172-31-35-179 ~]$ sh example.sh > output.log
please enter your name: venki
please enter password: venki123
[ec2-user@ip-172-31-35-179 ~]$ cat output.log
username is: venki
password is: venki123
Wed Aug 3 05:39:19 UTC 2022 (only output is going to redireted not error)
[ec2-user@ip-172-31-35-179 ~]$ cat example.sh
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
Date
[ec2-user@ip-172-31-35-179 ~]$ sh example.sh > output.log
please enter your name: venki
please enter password: venki123
example.sh: line 5: Date: command not found
[ec2-user@ip-172-31-35-179 ~]$ cat output.log
username is: venki
password is: venki123
FILE DECRIPTORS:
---------------
0 -> standard input
1 -> standard output
2 -> standard error
[ec2-user@ip-172-31-35-179 ~]$ cat example.sh
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
Date
[ec2-user@ip-172-31-35-179 ~]$ sh example.sh > out.log 2>&1
venki
venki123
[ec2-user@ip-172-31-35-179 ~]$ cat out.log
please enter your name: please enter password: username is: venki
password is: venki123 (along with o/p error also saved)
example.sh: line 5: Date: command not found
[ec2-user@ip-172-31-35-179 ~]$ cat example.sh
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
Date
[ec2-user@ip-172-31-35-179 ~]$ sh example.sh 2> err.log 1> output.log
venki
venki123
[ec2-user@ip-172-31-35-179 ~]$ cat output.log
username is: venki
password is: venki123
[ec2-user@ip-172-31-35-179 ~]$ cat err.log (both error and output in different files)
please enter your name: please enter password: example.sh: line 5: Date: command not found
CONDITIONAL STATEMENT:
-----------------------
if syntax:
if [ condition ]
then
.......
.......
.......
.......
else
......
......
......
......
fi
example:
--------
[ec2-user@ip-172-31-35-179 ~]$ cat if.sh
read -p "enter a :" a
read -p "enter b :" b
if [ $a -gt $b ]
then
echo "$a is bigger"
else
echo "$b is bigger"
fi
[ec2-user@ip-172-31-35-179 ~]$ sh if.sh
enter a :21
enter b :78
78 is bigger
Q.write a one shell script that accepts the file name from the user and check the file is existed in current directory or not.?
Ans:
[ec2-user@ip-172-31-35-179 ~]$ cat shell.sh
read -p "enter file name :" filename
if [ -f /home/ec2-user/$filename ]
then
echo "file is existed"
else
echo "file does not existed"
fi
[ec2-user@ip-172-31-35-179 ~]$ pwd
/home/ec2-user
[ec2-user@ip-172-31-35-179 ~]$ ls
err.log example.sh if.sh shell.sh
[ec2-user@ip-172-31-35-179 ~]$ sh shell.sh
enter file name :err.log
file is existed
[ec2-user@ip-172-31-35-179 ~]$ sh shell.sh
enter file name :file1.txt
file does not existed
[ec2-user@ip-172-31-35-179 ~]$ ls
err.log example.sh if.sh shell.sh
[ec2-user@ip-172-31-35-179 ~]$ cat shell.sh
read -p "enter file name :" filename
if [ -f /home/ec2-user/$filename ]
then
echo "file is existed"
echo "file content displaying if exists"
cat $filename
else
echo "file does not existed"
echo "creating a new file with that name"
touch $filename
fi
[ec2-user@ip-172-31-35-179 ~]$ sh shell.sh
enter file name :example.sh
file is existed
file content displaying if exists
read -p "please enter your name: " username
read -p "please enter password: " password
echo "username is: " $username
echo "password is:" $password
Date
[ec2-user@ip-172-31-35-179 ~]$ sh shell.sh
enter file name :file.txt
file does not existed
creating a new file with that name
[ec2-user@ip-172-31-35-179 ~]$ ls
err.log example.sh file.txt if.sh shell.sh
read -p "enter file name :" filename
if [ -f /home/ec2-user/$filename ]
then
echo "file is existed"
echo "the path of file is :" `find -name $filename`
echo "file content displaying if exists"
cat $filename
else
echo "file does not existed"
echo "creating a new file with that name"
touch $filename
fi
-f -->To check file is existed or not
-x -->To execute permission
-r -->read permission
-w -->write permission
----------------------------------------------------------------------------CLASS-4 END---------------------------------------------------------------------
FOR LOOP:
---------
Example:
-------
[ec2-user@ip-172-31-35-179 ~]$ cat for.sh
echo "for loop demo..."
for (( a=1; a<=5; a++ ))
do
echo $a
done
echo "for loop demo is over..."
[ec2-user@ip-172-31-35-179 ~]$ sh for.sh
for loop demo...
1
2
3
4
5
for loop demo is over...
WHILE LOOP:
----------
Example:
-------
[ec2-user@ip-172-31-35-179 ~]$ cat while.sh
echo "while loop demo starts.."
a=1
while [ $a -le 5 ]
do
echo $a
a=`expr $a + 1`
done
[ec2-user@ip-172-31-35-179 ~]$ sh while.sh
while loop demo starts..
1
2
3
4
5
SWITCH:
------