Linux: Bash Scripting

Bourne Again Shell

This week of learning was my best week yet. Mostly because I am familiar with some Programming Languages and it didn't take a lot of mental effort to learn the rudiments of bash scripting.

A Bash Script

Is a file that contains series of commands and even other scripts. I know that read weird but yes, a bash script can run other scripts (of the same language and other languages).

So this week, I created a bash script that actually does something pretty interesting: a script that sends an email to remind us of our standups daily.

A bash script should ALWAYS begin with a 'SheBang' (#!) followed by the path to the interpreter that should interpret the rest of the lines in the file. In a bash script, the shebang is followed by the path, /bin/bash with no spaces.

#!/bin/bash

Next thing I did was create an array of recipient's email addresses and assign it to variable

TO_array=("123@gmail.com" "456@gmail.com" "789@gmail.com" "abc@gmail.com")

I also created another array of email subjects that will be displayed depending on the day of the week that the script is run. For example, there are no standups on weekends so I want the email subject to be something like "Relax, it's a weekend" as the subject of the email if the script is run on either a Saturday or Sunday. We usually do not have standups on Mondays as well since it is the first day of the week.

email_array=("It's the first sprint so no standup" "Stand up time! Don't stand your team mates up" "Relax, it's a weekend")

Now, how do we ascertain what day of the week this script is run? We use the date command in Linux. When you open your terminal and run date, it returns:

Fri Feb 12 17:46:12 WAT 2021

Nice. But I am only interested in the day of the week. In this case, Fri. So I need to 'extract' Fri from the other data given. To do this, I'll pipe the data and feed it to the awk command. Lastly, assign the result to a variable.

Read more about the awk command here

current_day=date | awk '{print $1}'

Next thing I did was loop through the length of TO_array with a for loop

for email in ${TO_array[@]}
do

done

and display the email subject depending on the day of the week (using an if statement)

if [ $current_day = "Mon" ]

then
    echo $email
    echo ${email_array[0]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email

elif [ $current_day = "Sat" ] || [ $current_day = "Sun" ]
then
    echo $email
    echo ${email_array[2]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email

else
    echo $email
    echo ${email_array[1]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email
fi

echo is one of the most frequently used commands in Linux. It displays a message or output of other commands. The above is how to use a conditional statement in Linux. An alternative to using the if statement is the Case statement.

The if statement above can be written as a case statement as shown below:

case $current_day in
    Mon)
    echo $email
    echo ${email_array[0]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email
    ;;
    Sat || Sun)
    echo $email
    echo ${email_array[2]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email
    ;;
    *)
    echo $email
    echo ${email_array[1]}
    echo "Subject: Cloud School (The Explorers) Standup"|sendmail $email
    ;;
esac

sendmail can be used to transmit emails via SMTP (Simple Mail Transfer Protocol). Configuring sendMail is straightforward, this should help.

To see the full code, check out my GitHub repository: My first Bash Script

Like every other script, bash scripts and its syntax take getting used to. With constant practice, I hope to start writing bash scripts like I write English Language.

Thank you for reading and Cheers!