-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-scripting-1prac.txt
More file actions
243 lines (143 loc) · 3.82 KB
/
shell-scripting-1prac.txt
File metadata and controls
243 lines (143 loc) · 3.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
shell scrpting
---------------
command line argument
$1 $2 ...$9 , $11
read # read through we can pass the argument
#Create user & group
#!/bin/bash
user_group="avinna" # user_group=$2
if [ "$user_group" == "avinna" ]
then
echo "user group $user_group exists !!"
else if [ "$user_group" == "devops" ]
then
echo "Admin of server"
else
echo "invalid group"
fi
----------------------------
#check dir & if not create the dir
#! bin/bash
config_dir=$1
if [ -d "config" }
then
config_files=$(ls config)
echo "reading config dir contents: $config_files"
else
echo "config dir doesn't exists, createing one..."
mkdir config_dir
touch "$config_dir/data.txt"
touch "$config_dir/data2.txt"
fi
------------------------------------
# read throug create user & paaswd
#!/bin/bash
read -p "please enter your name" name
echo "welcome to devops $name "
--------------------------------
#! /bin/bash
echo -n "welcome to devops youtube channel"
read -p "enter your username: " user
read -p "enter your password: " passwd
if [ "$passwd" == "avinna123" ]
then
echo 'Login successsful !!!"
else
echo " login failed !!"
fi
--------------------------------------
$1 - first argument
$2 - second argument
$3 - third argument
$* - all parameters arguments shows
$# - number of arguments
$? - last command result
Loops
while
for
until
select
for loop [ - - - ]
vi for.sh
#!/bin/bash
for param in $#
do
echo "parameter: $param"
done
------------------------
#! /bin/bash
echo "num of params $#"
for param in $* # for i in [ condition ]
do
if [ -d "$param" ]
then
echo "executing script into dir"
ls -l $param
fi
echo "parameter: $param"
done
----------------------------
while loop while cond
---------
while condition
do
done
#!/bin/bash
sum=0
while true
do
read -p "enter a number: " num
if [ "sum" = "q" ]
then
break
fi
sum=$((sum + num))
echo "Sum of numbers: $num"
done
-------------------------------
function sum(){
sum=0
while true
do
read -p "enter a number: " num
if [ "sum" = "q" ]
then
break
fi
sum=$((sum + num))
echo "Sum of numbers: $num"
done
}
sum
------------------------------------
#!/bin/bash
function create_file(){
file_name=$1
touch $file_name
echo "file : $file_name is created"
}
create_file test.sh
create_file abc.yaml
create_file data4.txt
----------------------------
# vi /opt/scripts/os-log-alert.sh
#!/bin/bash
#Set the variable which equal to zero
prev_count=0
count=$(grep -i "`date --date='yesterday' '+%b %e'`" /var/log/messages | egrep -wi 'warning|error|critical' | wc -l)
if [ "$prev_count" -lt "$count" ] ; then
# Send a mail to given email id when errors found in log
SUBJECT="WARNING: Errors found in log on "`date --date='yesterday' '+%b %e'`""
# This is a temp file, which is created to store the email message.
MESSAGE="/tmp/logs.txt"
TO="[email protected]"
echo "ATTENTION: Errors are found in /var/log/messages. Please Check with Linux admin." >> $MESSAGE
echo "Hostname: `hostname`" >> $MESSAGE
echo -e "\n" >> $MESSAGE
echo "+------------------------------------------------------------------------------------+" >> $MESSAGE
echo "Error messages in the log file as below" >> $MESSAGE
echo "+------------------------------------------------------------------------------------+" >> $MESSAGE
grep -i "`date --date='yesterday' '+%b %e'`" /var/log/messages | awk '{ $3=""; print}' | egrep -wi 'warning|error|critical' >> $MESSAGE
mail -s "$SUBJECT" "$TO" < $MESSAGE
#rm $MESSAGE
fi