Watching:  

Introduction to Bash Scripting


Author: Subject Coach
Added on: 23rd Jan 2015

 
Please note: You need to login to view this resource

If you are looking for a beginer's course for Bash scripting, This course is very well for you. I've covered some of the most important topics to get you started. Course contents include

  1. Welcome message [Free]
  2. What is Bash
  3. Command line shortcuts and tricks
  4. Getting started with Bash profiles
  5. Bash Expansions
  6. Command Redirection and Piping 
  7. Using Echo and PrintF and how to preventing command expansion
  8. Understanding local and environment variables
  9. Bash script syntax
  10. A basic Bash script
  11. Working with numbers and operators
  12. Working with arrays
  13. Controlling output with awk, grep and cut commands. How to use basic bash color themes with your output.
  14. Working with here document and files
  15. Flow control statements and loops
  16. Introduction to functions
  17. Interacting with user
  18. Closing note

I hope that you will learn heaps from this course, please leave your feedback and improvement suggestions.

 

 

Author: Subject Coach
Added on: 23rd Jan 2015

Please get in touch with your teacher or tutor in case you have a question related to this lesson

None just yet!

In this chapter, we will explore controlling output with grep, awk and cut commands, we will then use what we do in a bash script to send an email. We will also cover, how to colorize output strings. Let's get started.

I have a log file from fail2ban tool, and I copied some lines from that log file into fail2ban.log file. Let me output some lines of this file for you to see. Ok, Question now is, how can I get all the IP addresses that were banned.

Grep can do many cool things, but its out of scope for this course as such. Let's use grep command to output lines with word, ban, in them. Now we can use awk command to print the eightth column of the above output. All we have to do is to feed output of grep command and pipe it to awk command. Let's count which column will be eightth column. So, one, two, three, four, five, six, seven and eight, which infact is the IP address we are interested in.

When we execute this command, we get the list of IP addresses. We can have same IP address repeated on several lines too. We can feed output of awk command and pipe it to Uniq command. uniq command removes duplicate lines from output.

Cut command can be used in similar fashion. We will first feed the output of grep command to TR command, TR command is used to shrink multiple continours white spaces to single white space. We will then pipe output of TR command to cut command, with cut command! I will provide delimiter with dash D option and then with dash F option, I will tell cut command, which column to print. We can pipe command output to uniq command in similar fashion, we did with awk command.

You should also check man pages for tr, cut, grep and awk, to check what other options, or what else is on offer.

Let's go back to our previous command where we used cut to output IP address. You can also print multiple columns by providing a range.

Now let's check how we can colorize our output. Escape sequences set screen attributes, such as bold text, and color of foreground and background. Check out this echo statement. This will print hello world, in bold and blue text. With echo command, dash E option will enable parsing of the escape sequences. The \e[0m option removes all attributes such as formatting and colors.

Attribute codes are 0,1,4,5,7 and 8. Where 0 being none, 1 being bold, 4 being underscore, 5 being blink, 7 to inverse colors and 8 concealed.
Similarly we got text color codes, and numbers 30 to 37 holding value of different text or foreground color you can use.
Then we have background colors, numbers 40 to 47 holding value for different background color you can use.

Let's form a colorized output string with echo command.

We start with normal echo dash E option following by starting double quotes, then I'll provide the escape sequences and use number 1 for bold text, followed by 31 for red text and 40 for black background, don't forget to end these values by appending M. We will then add the string we want to print, followed by [\e0m option to remove all formatting.

Let's copy this command and run it in terminal. We get the expected result. Here I will change a couple of colors to show you how to edit it. Make sure that your attribute code, text color code and background color are seperated by semi colon and followed by M.

Now let's apply colorization to our previous command output. All I will do is to feed output of cut command to Xargs, Xargs is a command oused to build and execute command lines from standard input. I will use printF to print our output enclosed with colors information. This output is a bit not correctly formatted. I will adjust a couple of things to make it pretty.

Now I will email our output from our previous command without colors to myself. Let's open analyze.sh script. First I will add a variable to hold path of the file I will redirect my output to, from my grep, TR and cut commands. Then I will enter the command that we used before, and redirect output to the mentioned file. Then I will add mail command to pick the contents from our output file and use it as message body. Let's save this script and run it. This will send and email to me, and output will look like this.

So we covered a fair bit in this chapter. All the commands used in this video has so much more to offer. Also note that there are multiple way of doing the same thing with bash scripting. In next chapter we will explore reading and writing to files and also learn here documents.