forked from codemistic/Non-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA blog on BASH Scripting
284 lines (174 loc) · 7.06 KB
/
A blog on BASH Scripting
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
<h1 align="center">Bash Scripting</h1>
BASH (Bourne Again Shell) is a command-line interpreter for shell scripts. It is widely used by many Unix and Linux distributions. BASH is an acronym for Bourne Again Shell.
BASH is a shell script interpreter that is used to execute commands on Linux and other Unix-like operating systems. BASH is an acronym for Bourne Again Shell.
BASH is a powerful scripting language that can be used to automate many tasks on a Linux or Unix system. In this blog post, we will explore some of the basics of BASH scripting.
We will cover the following topics:
- Creating a BASH script
- Running a BASH script
- Basic BASH scripting constructs
- Loops in BASH
- Conditionals in BASH
- Functions in BASH
- Debugging BASH scripts
Let's get started!
Creating a BASH script
A BASH script is a text file that contains a list of commands to be executed by the BASH interpreter. To create a BASH script, simply create a new text file with the .sh extension.
For example, let's create a simple script that prints the message "Hello, world!" to the screen. To do this, open a new text file in your favorite text editor and type the following:
#!/bin/bash
echo "Hello, world!"
Save the file and close the text editor. You have now created a BASH script!
Running a BASH script
To run a BASH script, simply type the path to the script file at the command prompt.
For example, if the script file is saved in the current directory, you can run it by typing:
./hello_world.sh
If the script file is saved in a different directory, you will need to specify the full path to the script file.
For example, if the script file is saved in the /home/user/scripts directory, you can run it by typing:
/home/user/scripts/hello_world.sh
Basic BASH scripting constructs
Now that we know how to create and run a BASH script, let's take a look at some of the basic scripting constructs.
Comments
Comments are lines in a script that are not executed by the BASH interpreter. Comments are used to add notes or documentation to a script. Comments are prefixed with the # character.
For example, the following script contains a comment:
#!/bin/bash
# This is a comment
echo "Hello, world!"
Variable assignment
Variables are used to store values in a script. Variables can be assigned using the = character.
For example, the following script assigns the value "Hello, world!" to the variable message:
#!/bin/bash
message="Hello, world!"
echo $message
The echo command is used to print the value of a variable to the screen.
Command substitution
Command substitution is used to execute a command and store the output in a variable. Command substitution is performed using the $(command) syntax.
For example, the following script stores the output of the pwd (print working directory) command in the variable dir:
#!/bin/bash
dir=$(pwd)
echo $dir
If-then-else
The if-then-else construct is used to execute a set of commands based on a condition. The condition is evaluated using the test command.
The general syntax of the if-then-else construct is as follows:
if test-command
then
commands
else
commands
fi
For example, the following script will print "Hello, world!" if the value of the variable message is "Hello, world!":
#!/bin/bash
message="Hello, world!"
if test $message = "Hello, world!"
then
echo $message
else
echo "The message is not Hello, world!"
fi
The test command can be used to evaluate many different types of conditions. For a complete list of the conditions that can be evaluated, please see the test(1) man page.
Switch-case
The switch-case construct is used to execute a set of commands based on the value of a variable. The general syntax of the switch-case construct is as follows:
case variable in
pattern1)
commands
;;
pattern2)
commands
;;
*)
commands
;;
esac
For example, the following script will print "Hello, world!" if the value of the variable message is "Hello, world!":
#!/bin/bash
message="Hello, world!"
case $message in
Hello, world!)
echo $message
;;
*)
echo "The message is not Hello, world!"
;;
esac
Loops in BASH
There are two types of loops in BASH: for and while.
The for loop is used to execute a set of commands for each item in a list. The general syntax of the for loop is as follows:
for item in list
do
commands
done
For example, the following script will print each item in the list:
#!/bin/bash
for item in 1 2 3 4 5
do
echo $item
done
The while loop is used to execute a set of commands until a condition is met. The general syntax of the while loop is as follows:
while test-command
do
commands
done
For example, the following script will print the numbers 1 through 5:
#!/bin/bash
i=1
while test $i -le 5
do
echo $i
i=$(($i + 1))
done
Conditionals in BASH
Conditionals are used to execute a set of commands based on a condition. The general syntax of the if-then-else construct is as follows:
if test-command
then
commands
else
commands
fi
For example, the following script will print "Hello, world!" if the value of the variable message is "Hello, world!":
#!/bin/bash
message="Hello, world!"
if test $message = "Hello, world!"
then
echo $message
else
echo "The message is not Hello, world!"
fi
The test command can be used to evaluate many different types of conditions. For a complete list of the conditions that can be evaluated, please see the test(1) man page.
Functions in BASH
Functions are used to group a set of commands together. Functions are declared using the function keyword. The general syntax of a function is as follows:
function function-name {
commands
}
For example, the following script contains a function that prints the message "Hello, world!":
#!/bin/bash
function hello {
echo "Hello, world!"
}
hello
Functions can also take arguments. The arguments are passed to the function using the $1, $2, $3, etc. variables.
For example, the following script contains a function that prints the first argument that is passed to the function:
#!/bin/bash
function print_argument {
echo $1
}
print_argument hello
Debugging BASH scripts
BASH scripts can be debugged using the -x and -v options. The -x option will print each command that is executed by the BASH interpreter. The -v option will print each command before it is executed.
For example, the following script will print each command that is executed:
#!/bin/bash
# This is a comment
echo "Hello, world!"
To debug this script, simply type the following at the command prompt:
bash -x hello_world.sh
The output of the script will be as follows:
+ echo 'Hello, world!'
Hello, world!
The -x and -v options can be combined to print both the commands and the output of the commands.
For example, the following script will print both the commands and the output of the commands:
#!/bin/bash
# This is a comment
echo "Hello, world!"
To debug this script, simply type the following at the command prompt:
bash -xv hello_world.sh
The output of the script will be as follows:
+ echo 'Hello, world!'
Hello, world!
That's it for this blog post! I hope you have learned some of the basics of BASH scripting.