-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashScripting
232 lines (135 loc) · 9.27 KB
/
bashScripting
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
## Bash Scripting Cheat Sheet
https://devhints.io/bash
What is bash?
Bash is a scripting language that runs within the terminal on most Linux distros, as well as MacOS.
Shell scripts are a sequence of bash commands within a file, combined together to achieve more complex tasks than simple one-liner and are especially useful when it comes to automating sysadmin tasks such as backups.
This is a few things among many that you will learn in this room:
## Variables
Now we are moving onto variables, in bash these are quite simple and we create them like so:
Where we give the value of Jammy and assign it to the variable name.
Please note that for variables to work you cannot leave a space between the variable name, the ”=” and the value. They cannot have spaces in.
So how would we now use our variable? Well its also very simple.
We have to add a $ onto front of our variable name in order to use it.
If we test this out in our own terminal we get something like this:
$ name="Jammy"
$ echo $name
Jammy
This would output “Jammy” to the screen.
Variables make it much easier to store data and rather than typing out the same thing in multiple places we could simply insert our variable with $var and then declare that to a certain value making it easier to fall back on if you do something wrong and need to change it. So how can we debug our code?
Debugging is a very important part of programming so we should get used to problem solving and fixing errors as early as possible. And bash has a few built in features that make our life simple.
When running at the command line you can do:
bash -x ./file.sh
You can make a simple bash script(now you know some basic syntax) and make something completely wrong. Then step through your program with debug mode and see what it looks like when it throws errors!
This tells you which lines are working and which lines are not. If you want to debug at a certain point you can insert set -x into your script and set +x to end the section like the following:
So lets look at an example. This is our script from earlier being ran with bash -x ./example.sh
You can see its outputting a + for the command and then the output of what that command executed.
If there was an error it would output a - on that line this makes it easy to spot where you have gone wrong so you can fix them.
We can also use multiple variables in something like an echo statement.
You aren't just limited to using 1!'
## Parameters
We will now look at one of the main features of bash and that's using parameters.
We will firstly look at parameters specified using the command line when running the file.
These come in many forms but often have the "$" prefix because a parameter is still a variable.
Lets start by declaring a parameter that is going to be our first argument when running our bash script.
We now run our script with ./example.sh Alex
And sure enough we get returned with “Alex”
So what if we wanted the 2nd argument? Well the process is very simple and we simply add a $2 instead of name=$1
Then run with ./example.sh Alex Tony
What do you think it would return?
And it would return "Tony".
What if we didn't want to supply them like this however, and instead it would let us type in our name in a more interactive way, we can do this using read.
This code will hang after its ran, this gives you the opportunity to type in your name.
And we can see that it worked!
```Answer the questions below
How can we get the number of arguments supplied to a script?
$#
Correct Answer
How can we get the filename of our current script(aka our first argument)?
$0
Correct Answer
Hint
How can we get the 4th argument supplied to the script?
$4
Correct Answer
If a script asks us for input how can we direct our input into a variable called ‘test’ using “read”
read test
Correct Answer
What will the output of “echo $1 $3” if the script was ran with “./script.sh hello hola aloha”
hello aloha```
## Arrays
For this module i suggest you follow along in the attackbox or a standard linux terminal to make it easier to understand.
Arrays are used to store multiple pieces of data in one variable, which can then be extracted by using an index. Most commonly notated as var[index_position].
Arrays use indexing meaning that each item in an array stands for a number.
In the array ['car', 'train', 'bike', 'bus'] each item has a corresponding index.
All indexes start at position 0
item index
car 0
train 1
bike 2
bus 3
Now we have covered this, let's make an array in bash.
The syntax is as follows.
We have the variable name, in our case ‘transport’
We then wrap each item in brackets leaving a space between each item.
transport=('car' 'train' 'bike' 'bus')
We can then echo out all the elements in our array like this:
echo "${transport[@]}"
You can try this in your own terminal and see what it outputs.
Where the "@" means all arguments, and the [] wrapped around it specifies its index.
So what if we wanted to print out the item train.
We would simply type:
echo "${transport[1]}"
because the train is at index position 1.
The last thing we will cover is if we want to change an element, or delete it. If we wanted to remove an element we would use the unset utility.
unset transport[1]
This now removes the train item, if we wanted to we could echo it back out and see that it is indeed gone,
Now lets set it to something else. We can do:
transport[1]='trainride'
If we echo the array then we get:
car trainride bike bus
So we successfully managed to swap out an element in our array!
As a little side project try building on your previous project of a biography maker, include arrays so that you can store multiple names and multiple facts about the person. Then in the next module we can expand even further.
Given the array please answer the following questions
cars=('honda' 'audi' 'bmw' 'tesla')
#Conditionals
When we talk about conditionals it means that a certain piece of code relies on a condition being met, this is often determined with relational operators, such as equal to, greater than, and less than.
We will make a simple "if" statement to check if a variable is equal to a value, we will also make a script that checks if a file exists and that it is writeable, if it is we will write a message to that file, if not writeable it will delete it and make a new one. A Lot of new things will be taught here so pay attention.
If statements always use a pair of brackets and in the case of the [] we need to leave a space on both sides of the text(the bash syntax).
We also always need to end the if statement with fi
Here a variable is being declared as 10 and in the top line of the if statement the variable $count is being compared to the integer 10.
If they are equal then it outputs true, if its false it outputs false. As we know 10 is equal to 10 so it outputs true.
The -eq is one way of doing this, you could also use “=”
Operator
Description
-eq
Checks if the value of two operands are equal or not; if yes, then the condition becomes true.
-ne
Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.
-gt
Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.
-lt
Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.
-ge
Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true.
^^^^^ These are some examples.
So now let's use this to make a little script that compares an input(a parameter) and checks it against a value to check if it's true or not. A guessing game if you will.
Now let's test this in our terminal.
# ./example.sh guessme
"They are equal"
# ./example.sh hi
"They are not equal"
And we can see that it works!
Feel free to play around with these and try making different combinations and using different operators.
Now let's create another script where we will use 2 conditions simultaneously and coming back to a concept we learnt in the first lesson.
Let's begin.
We want to make a script that we will perform on a file given by a parameter.
We then check if it exists and if it has write permissions. If it has write perms then we echo “hello” to it. If it is either non-accessible or doesn't exist we will create the file and echo “hello” to it. Let's begin!
─# ./example.sh hello.txt
─# cat hello.txt
hello
And we can see that it worked!!
The -f checked if the file existed.
The -w checked if the file was writable, without write permissions we wouldn't be able to output our text into the file.
To finish off our little project from the previous task. You can build on your script using an if/else statement.
Test to see if the age is under 18, if it is then echo out their name with "You are not eligible for work" or something along these lines, if they are over 18 then ask them for their job, you can do this with read
Feel free to add anything you like and make it as complicated as you wish and good luck with your project!