top of page
Search

Writing Custom Bash Scripts to Automate Tasks on Linux

  • Writer: SWAPNESH CHOGLE
    SWAPNESH CHOGLE
  • Jun 19, 2023
  • 6 min read

Bash scripting is a convenient way to automate things on any Linux system, and we're going to use it here to automate certain tasks we use all the time.


Bash is a simple language for threading together several different Linux serviceability's. Its simplicity makes it easy for newcomers to produce lots of scripts that would else be enough complicated.


You can do all feathers of effects, if you know the right Bash commands. In this companion, we'll use Bash to automate a many generally used tasks. If we've a specific task we want to do, we'll just use an alias command to call a Bash script and have it run whatever it's programmed to do so that we do not have to run and call the script manually every time from wherever it's saved.



Requirements


To follow along, all you'll need is an Ubuntu or Kali system. Really, any Linux system will do. For installing Kali Linux on your system you can follow our previous article for the steps.


Step 1: Identify Bash Interpreter


To get started, we need to know which Bash interpreter we're using. To do so, use which bash in a terminal window. Learning the location of the interpreter will be useful when writing our first Bash script.

~# which bash

usr/bin/bash

Step 2: Code your first Bash Script


To start a new Bash script, create and open a new Bash file called "bash.sh" or whatever you want to call it. The important thing is that it ends in the "sh" extension. We'll be creating a super basic script to illustrate one easy thing it can.

~# nano bash.sh

In the document, start the first line with a (#!) followed by the location of your Bash interpreter (for me, it's usr/bin/bash). When the program is opened, this will tell it how to interpret the language we're writing the rest of the script in.

#! /usr/bin/bash

Let's create a variable to make something happen. For something simple, we'll just use a STRING that says Swapnesh. You could make it say whatever you want.

STRING="Swapnesh"

Now we want to echo something back. In our example, we'll echo "Don't mess with Ethical HAcker $STRING" where $ indicates a call to a variable, in this case, Swapnesh. What this will do is print the message, swapping out $STRING for whatever your STRING is.

echo "Don't mess with $STRING"

Here's what the script should look like as a whole:

#! /usr/bin/bash
STRING="Swapnesh"
echo "Don't mess with Ethical HAcker $STRING"

Save your script by hitting Control-X on your keyboard to exit, then Y to save the modified buffer, and then Enter to save the file. This will bring you back to the terminal window. To test out the script, use bash bash.sh (or whatever you named it) to call the script. You should see the echoed message appear. Success!

~# bash bash.sh

Don't mess with Ethical HAcker Swapnesh

Step 3: Run Commands & Return their Output


Next, we want to try to create something inside the script that depends on another tool being run. So nano back into the script and replace the echoed text with something that results from something being run.

In our example, let's print the result of a particular command. After echo, use a $ right away, but this time add parentheses right after it. Whatever command we put inside the parentheses will be run when the program is activated, and the result of the command will be what's printed to the user.

You could use something like ifconfig, but to make it easy, we're going with whoami, which will tell the person running the script what user on the system they are currently using.

#! /usr/bin/bash
echo $(whoami)

Save your script again then test out the script with bash bash.sh (or use whatever you called it). As you can see, I'm currently the "root" user, which isn't surprising seeing that my prompts in the terminal end with a # sign.

~# bash bash.sh

root

Step 4: Taking User Input


Open the bash.sh file back up in nano, and we'll create a script that asks the terminal user to input something, which will then be our variable for whatever we want to print out with echo.

For our example, we'll use echo to print to the user "What is your name?" Then, on the next line, we'll add a variable of name with the read command. Whatever they input after being asked the question will be seen by our program. To use the variable, let's use echo to print a statement about the name they inputted. The statement uses $name to indicate it should be swapped with the variable they give us.

#! /usr/bin/bash
echo "What is your name?"
read name
echo "Wow, $name sounds like a punk"

Save your script again then test out the script with bash bash.sh (or use whatever you called it). As soon as it's run, it'll ask the user for their name. What we've done here was added a bit of interactivity. They'll type out their name and hit Enter, and instantly, our program will see the name, use that instead of "$name" in the echo command, and print the statement out for the user.

~# bash bash.sh

What is your name?
Trevor

Wow, Trevor sounds like a punk

We've just created an argumentative Bash script that can take input from the user. However, this is far more useful in other scenarios. For example, putting in a variable to see where a particular interface is, which port an Arduino board is connected to, or to flash a bunch of microcontrollers simultaneously. The first step to that last one is selecting which port they're connected to, so custom Bash tools can be pretty convenient.


Step 5: Use Bash for Terminal Commands


Let's say that we want to learn something about the computer, and we don't want to pipe through a whole bunch of different outputs. Without a Bash script, you could type something like ifconfig to get a whole lot of information about the computer's interface parameters. To narrow it down, you could pipe it into grep and target more specific information. So ifconfig | grep broadcast is a good example.

~# ifconfig | grep broadcast

inet 192.168.1.169 netmask 0xffffff00 broadcast 192.168.1.255

Above, you can see that I've identified my IP address, which is in a string that has the word "broadcast" in it. So if you wanted to find your current IP address on the network with a one-liner, you could use this not to have to dig into a ton of information just to find one piece of data.

Let's try that again, but let's narrow it down to just the IP address. For that, we can use a tiny Bash script. Keep everything the same, but add another pipe to the end, followed by awk '{print $2}' — awk lets us choose a specific word from the output, and print $2 will pull the second item in the output and print it back to us.

~# ifconfig | grep broadcast | awk '{print $2}'

192.168.1.169

What that one-liner does is filter all of the information in ifconfig by "broadcast" using grep, and then the result of that is filtered down to show us the IP address, which is in the second position on the broadcast line that was filtered first.

Now, that's an awfully long command to type out every time you need to see your current IP address on the network. This is where aliasing comes in handy. Aliasing is the best part of developing an excellent Bash script.

To turn a one-liner into a single-word command using aliasing, you would type alias, space, and then the simple command you want to run the one-liner. For this, ipaddress makes sense since it's short but descriptive. Next, without a space, add an equals (=) sign. Right after that, put a double quotation mark ("), copy and paste the one-liner we just used, then add an ending double quotation mark.

~# alias ipaddress="ifconfig | grep broadcast | awk '{print $2}'"

Hit Enter and test it out to see what you get.

~# ipaddress

inet 192.168.1.169 netmask 0xffffff00 broadcast 192.168.1.255

As you can see, that didn't do what we wanted, and that's because the single and double quotation marks are messing it all up. To avoid that, we can simply type echo then put the one-liner inside parentheses with a $ before the first parenthesis to indicate that everything inside the parentheses is the variable we want to be printed.

~# ipaddress="echo $(ifconfig | grep broadcast | awk '{print $2}')"

Hit Enter and test your new alias command out again, and it should work.

~# ipaddress

192.168.1.169

As you can see, knowing about the $ sign to make the command run first before the result is printed got us around the issue of trying to automate the process of typing in a single word and getting your current IP address whenever it first failed.


Happy Bash Scripting!


This has been a brief Bash scripting intro. There a lot of stuff you can do with Bash, but in general, this is a great way to get started automating just about anything you want to do in Linux.


Bash scripting is an incredibly useful skill to learn, and you don't need to know a lot about programming to stitch together a lot of different Linux apps and tools to make something complicated and lengthy.

 
 
 

Comments


bottom of page