bash scripting basics

Do you want to automate tasks?

search through files?

manage files more efficiently?

I’m sure you do.

Bash scripting is essentially write out a series of commands as you would do in a command line but saved into a text file. his file is then given permission to execute (made executable) using the chmod command. Bash is short for ‘Bourne Again Shell’ and it was the successer to Bourne Shell. Bash scripting is also less resource instensive then GUI as the graphical elements do not have to be loaded.

Bash scripting is also pretty easy to use compared to other programming languages.

what do we use bash for?

  • manipulating files

  • executing repetitive tasks

  • searching through logs/files

  • automating tasks

  • running programs that require many commands at once and we want to use multiple times

When you run the script, the bash interpreter reads and executes each line in the file one by one, automating your tasks. Scripts can include control flow elements like loops and conditional statements to make them more versatile.

Step 1.

open a file using a text editor eg. nano

step 2.

shebang line, which tells your machine what interpreter to use to run the script.

step 3.

it isn’t necessary to write into the command line, the scripts purpose, but as your scripts become more complex, it is very useful

using the ‘echo’ command tells your script to echo the message onto your terminal when the file is executed.

step 4.

exit the file and use the command chmod 755 to set permissions so you can read and execute it. without chmod you do not have permission to execute the script.

the ./ command tells your machine to execute the script

Now that we know how to write a basic bash script what else can we do with it?

Lets try experimenting with variables in our script

variables

variables are important in scripts as they allow us to manipulate and/or store specific values during execution. The basic syntax for a variable is ‘variable name=value’ an example of using variables would be the image below. A $ sign before a name indicates a variable in a script.

when we execute our script ‘$name’ is replaced with puxnet and ‘$blog’ is replaced with skuxxxnet.

We can also use ‘if’ statements in our scripts with variables, which allows us to make decisions based on conditions and tells the script what code blocks to run. the basic syntax for using if variables is

if {condition}; then

# code to execute if condition is true

fi


‘if’ starts the conditional block

‘{condition}’ condition to be run

‘then’ starts the code block to be executed if condition is true

‘fi’ marks the end of the if statement

we can use the else and elif statements to define multplie conditions in our scripts


loops

Loops are useful for automating repetitive tasks. You can execute a block of code multiple times each with potentially different data. The most common loops in bash scripting are ‘for’ ‘while’ and ‘until’.

below is our loop script, the while variable will execute the script as long as the condition is true

we can try this script using the until variable which executes the script as long as the conditions are false. Under our new script, the loop will execute until the user enters yes, essentially the scripts behave the same.

when is bash scripting used?

I like to think of bash scripting as a way of saving a set of commands without having to type them out over and over again.

+

Previous
Previous

Mitre Attack-utilizing the cyber kill chain

Next
Next

splunk