Bash scripting is a powerful tool to automate sysadmin tasks, manage system resources and more.
Let’s learn to Bash it through our daily #FromZeroToHacker challenge.
Table of contents |
Introduction |
What have I learnt today? |
Stats |
Resources |
Introduction to bash scripting for beginners
Bash is a scripting language that runs within the terminal on most Linux distros. Shell scripts are a series of bash commands, combined to achieve complex tasks that let us automate sysadmin tasks such as system administration tasks, manage the system resources, and other interesting stuff.
What have I learnt today?
Our first simple bash scripts
Let’s create a simple bash script!
First, we need to add the shebang (#!/bin/bash
), as every bash script always starts with it. Then, let’s add a simple bash function, such as echo "Hello World"
.
- Create a new file, for example, with nano:
nano example.sh
- Add the shebang
#!/bin/bash
- Also, add a function such as “echo “Hello World”.
- Close nano and make the file executable with
chmod +x example.sh
. - Run the file with
./example.sh
There you have it. A simple bash script.
But there is more! We can add more commands after the first one. Let’s add whoami
and id
, and run the code once more.
#!/bin/bash echo "Hello World" whoami id
As you can see, we can chain commands and all of them will execute (printing their output) one after another.
Variables
Variables are a great way to store data. Think that a variable is a box that you can put anything inside (a string of text, a number…) and you can invoke it anytime.
Let’s modify our code:
#!/bin/bash target_IP="192.53.6.2" echo $target_IP
We can also debug (outputting extra text from a code to view what’s wrong with it with the intention to fix it) with bash -x ./<FILENAME>
. For example, let’s debug our example.sh
file:
Now we can see the code line by line and its output. A great way to find out what’s wrong with the code and fix it 🙂
By the way, you are not limited to using one variable when printing a text. You can concatenate multiple variables like this:
#!/bin/bash target_IP="192.53.6.2" target_port=22 echo "The SSH service is at $target_IP:$target_port"
Parameters
We have seen variables before, but they were specified before running the code. But what if we want to change the IP or the port? We need to open the code, modify it, and then run the code again. Too much.
We can use parameters. When running the code, we can set up the value of a variable in the command. For example:
#!/bin/bash name=$1 echo $name
Now, if we run the code, passing down a string of text as a name, the code will check the first parameter passed on the command, and use it to set up the value of name
(name=$1
)
If we want to fetch a second argument, we would use $2
. For a third, $3
and so on.
But this results in a poor experience. You have to remember what is the first parameter, the second, the third, etc.
We can do something better: read <VARIABLE>
.
This function waits for the user’s reply and stores its value into a variable:
#!/bin/bash echo Enter your IP read target_IP echo Enter your port read target_port echo "You have been hacked at $target_IP:$target_port" echo "No, just kidding :)"
Way better than remembering each parameter, right?
Arrays
Arrays store multiple values in one variable. They are a list of variables, instead of one variable.
Let’s have the array grocery_list=('pineapple' 'tomatoes' 'lemons')
.
To echo the value you are interested in, you need to use the index (starting with 0):
echo "${grocery_list[0]}"
echoes “pineapple”echo "${grocery_list[1]}"
echoes “tomatoes”echo "${grocery_list[2]}"
echoes “lemons”
The syntax is a bit confusing, but you get the point, right?
We can manipulate the array too!
Remove values with unset <ARRAY_NAME>[<INDEX>]
. For example, unset grocery_list[1]
.
We deleted the second (index 1) value, but the other values are still working.
But we can re-set the value again with <ARRAY_NAME>[<INDEX>]='<NEW_VALUE>'
. For example, “grocery_store[1]=’kiwis’.
Conditionals
Right now, the all code in the script will run from top to bottom. But what if we want to run a piece of code only if some conditions are met? Enter the Conditionals.
For example:
count=10 if [ $count -eq 10 ] then echo "True" else echo "False"
We put the conditional statement between a pair of brackets ( [ ] ) and if the condition(s) is met, the code inside then
is run. If not, the code inside the else
. We also need to end the if statement with fi
(if reversed).
In the code above, we declare the count
with the value of 10, and the line in the if statement compares the value of count
to the integer 10.
Some comparisons:
-eq
compares if two values are equal or not. If it is, the condition becomes true.-ne
compares if two values are equal or not. If they are not, the condition becomes true.-gt
compares if the left value is greater than the value on the right, returning true if it is.-lt
compares if the left value is less than the value on the right, returning true if it is.-ge
compares if the left value is equal or greater than the value on the right, returning true if it is.
Summary
Today we bashed through the lesson, learning:
- Bash syntax
- Variables
- Using parameters
- Arrays
- Conditionals
Stats
From 52.873th to 51.850th.
Here is also the Skill Matrix: