Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellscripting
More file actions
319 lines (224 loc) · 8.52 KB
/
Copy pathshellscripting
File metadata and controls
319 lines (224 loc) · 8.52 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
#** snippets
#Core concepts
############ ##############
bash_profile --gets executed only when you log in
bashrc -- runs when a new shell is initiated likes of opening a new terminal.
export PATH= $PATH:$HOME/Desktop -- Appending new path to $PATH variable.
alias lZ='ls -alZ | more' --sets alias
bash -- creates a new shell , runs bashrc. { exec bash will consume. the same process ID and do the same thing}
bash_history -- history of what we typed as the current user.
HISTCONTROL-- variable to change the behaviour of bash_history
export HISTCONTROL=$HISTCONTROL:ignorespace --- once set , any command written in the shell after the space will not be tracked by bash_history
bash_logout -- last thing that happens when we issue the log out command . works when we do a clean exit .
#!/bin/bash --simply means that the script needs to be executed in a bash shell
`` -- commands enclosed in back ticks gets executed for eg "TODAYDATE=`date' -- will execute command date and store the value in TODAYDATE."
shopt -s expand_aliases --- turns on shell scripting expanding aliases with in the subshell.
exit status 0 -- good
exit status non-zero -- bad
echo $? -- shows the last exit status .
set -e -- lets the shell know to exit the moment encountered with an error.
expr -- evaluate mathematic expression . expr 2 + 2 gives the value 4.
expr 2 + 2 --addition
expr 10 / 2 -- division
expr 2 \* 4 --- multipication
expr 4 - 2 ---subtraction.
expr \( 2 + 2 \) \* 4 -- grouping to set order of precedence and escaping the brackets using \.
env -- gives the list of all the global variables
set -- gives the list of local variable to shell.
escape sequence -- \ (back slash)
/dev/null redirects the command standard output to the null device, which is a special device which discards the information written to it.
read "variable name " --- reads the interactive input and stores it in the variable.
echo ${master=prabin} -- sets value to the variable and dispalys at the same time in the command line
declare -i NEWVAR=10 --explicitly defines NEWVAR as an integer.
declare -r VAR="this is a string" --- explicity defines the varibale as read only.
**There is a distinction in Linux between shell variables, and environment variables. In Linux, shell variables are only in the current shell, and Environment variables, are in that shell and all child shells.
shell variables are set with e.g. just a=5
environment variables are set with export, export also sets the shell variable**
**Files and directories whose names begin with a dot (.) by default are not displayed in directory listings by the standard command ls. Therefore, they are traditionally used to store settings, preferences, etc..There are also two special directory names in this class: the directory named simply . is an alias for the same directory in which it appears (a self reference), and the directory named .. refers to the parent directory of **
**Best practice make your variables caps and put the values in double quotes and no space between variable and value**
**Enclosing characters in single quotes (') preserves the literal value of each character within the quotes.**
**Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !**
#Array
#############################
Array elements are space seperated
MYARRAY=("first" "second" "third") --- array
echo ${MYARRAY[0]} --- dispaly an array value indexed at first posiiton.
echo ${MYARRAY[*]} --- displays everything out in thte array.
MYARRAY[3]="Fourth" -- appends the array with the new value indexed in the number mentioned.
**code snippet array iteration***
ARRAY=("web1" "web2" "web3")
for INDEX in ${ARRAY[@]} ; do
echo "the server is $INDEX "
done
**code snippet array iteration using count***
NEWARRAY=("prema" "benny" "bittu" "bikku")
COUNT=0
for INDEX in ${NEWARRAY[@]}; do
echo " The name is ${NEWARRAY[$COUNT]}"
#COUNT="$COUNT + 1" --- works as well.
COUNT=`expr $COUNT + 1`
done
#Passing varibale to script at the command line
#####################################################
** code snippet for passing varibales**
USERNAME=$1
PASSWORD=$2
echo "The user name is $USERNAME and password is $PASSWORD"
**
variales passed in from the command line from the first and second position delimited by space will be stored in $1 and $2 respectively.
#The if statement
######################################################
**code snippet guess number
echo "Enter a number between 1 and 5"
read NUMBER
if [ $NUMBER -eq 3 ] ; then
echo "You have guessed the correct number : $NUMBER"
fi
**
**cide snippet for fileexiistance and readable check
FILENAME=$1
if [ -f $FILENAME ] && [ -r $FILENAME ] ; then
echo " the file $FILENAME exist and is readable "
fi
**
*** code snippets for nested if ***
echo "Guess a number between 1 and 3"
read NUMBER
if [ $NUMBER -eq 1 ] 2>/dev/null; then
echo "You have entered #1"
elif [ $NUMBER -eq 2 ] 2>/dev/null; then
echo "You have entered #2"
elif [ $NUMBER -eq 3 ] 2>/dev/null; then
echo "You have entered #3"
else
echo "You havent followed the directions"
fi
**
** 2>/dev/null -- dumps the error to the blackhole.
#Forloop
###################################################
** code snippet for iterating through a listing and cating the output to a file
LISTING=`ls *.sh`
for ITEMS in $LISTING ; do
echo $ITEMS
`cat $ITEMS >> /home/bernard/Scripts/dump.txt`
done
#while loop
**code snippet for display the number of times hello world.
COUNT=1
echo "Enter the dispaly number"
read DISPLAYNUMBER
while [ $COUNT -le $DISPLAYNUMBER ] ; do
echo "Hello World : $COUNT"
COUNT="`expr $COUNT + 1`"
done
#Execution Operator
#################################################
2>/dev/null -- dumps the error to the blackhole.
rm master.txt 2> /dev/null && echo "File exist and was removed" || echo "File doesnt exist and cant be removed" --- the 'and' is executed only when the remove command is true else 'or' is executed
** code snippet demonstrating execution operators.
echo "Enter a number between 1 and 5"
read NUMBER
if [ $NUMBER -eq 1 ] || [ $NUMBER -eq 3 ] || [ $NUMBER -eq 5 ] 2> /dev/null ; then
echo "You have entered an OOD NUMBER "
elif [ $NUMBER -lt 3 ] && [ $NUMBER -gt 1 ] 2> /dev/null ; then
echo "You have entered my favourite number"
fi
#Reading files
#################################
**code snippet for reading file using while
FILENAME=$1
while read -r line ; do
echo " Names : $line"
done < "$FILENAME"
**code snippet to reading file using for
FILENAME=`cat list`
for ITEM in $FILENAME ;do
echo "Name : $ITEM"
done
#File descriptors
##################################
** code snippet for reading and writing using file descriptors
FILENAMEREAD=$1
FILENAMEWRITE=$2
exec 5<$FILENAMEREAD
exec 6>$FILENAMEWRITE
while read -r LINE ; do
if [[ $LINE =~ bittu ]] ; then
echo "i love $LINE " >&6
fi
done <&5
exec 5>&-
exec 6>&-
#IFS and Delimiting
IFS -- internal field seperator ---
** code snippet using IFS
FILENAME=$1
IFS=' '
while read -r processor memory cpu company; do
echo "processor : $processor"
echo "memory : $memory"
echo "cpu : $cpu"
echo "company : $company"
done < $FILENAME
# trap and signals
##########################
trap is a command which helps us to look for the occurance of something likes of events , signals
** code snippet for reading and writing using file descriptors
trap 'echo " - Please press Q to exit"' SIGINT SIGTERM SIGTSTP
while [ "$CHOICE" != "Q" ] && [ "$CHOICE" != "q" ] ; do
echo "MAIN MENU"
echo "---------"
echo "1)DEV"
echo "2)TEST"
echo "3)UAT"
echo "4)PROD"
echo "Q)EXIT"
echo "--------"
echo "Enter your Choice:"
read CHOICE
clear
done
#Debugging
#############################
set -x --start debug
set +x --stop debug
#Error Handling
#################
#!/bin/bash
cd master 2> /dev/null
if [ $? = 0 ] ; then
ls -al
else
echo "Directory doesnt exist"
fi
#Simple functions
####################
Funcions are way of grouping like commands to a single referable name
: -- null command
fucntion () {
echo "Hello World"
}
fucntion
#Function return and exit
** code snippet to show funciton return and exit.
YES=0
NO=1
FIRST=$1
SECOND=$2
THIRD=$3
function complex () {
if [ ! -z "$THIRD" ] ; then
return "$YES"
else
return "$NO"
fi
}
complex
RETURN_VAL=$?
echo "$RETURN_VAL"
if [ $RETURN_VAL -eq 0 ] ; then
echo "We received all the parameters"
else
echo "Usage: complexfucntion.sh [param1] [param2] [param3]"
fi
You can’t perform that action at this time.
