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 some basic commands to read and write to files. We will also cover here documents briefly.
Let's check what my current working directory is, and then list all the files under my current working directory. There are three files under this folder.

To read and output contents of cat.sh file, I can simply use cat command and feed file directly to it by using < symbol, this will print contents of the file in terminal window. Some times files can be too long and you just want to check the starting lines or some lines at the end of the file. We can use head command, or tail command for that purpose.

Head command reads the first few lines of the file. Similarly we can use tail command to print last few lines of the file.

Our file cat.sh only has few lines, so it make more sense to tell these command to precisely print certain number of lines.

I will now add -1 to tail command. This will print the last line of cat.sh file.
Adding -1 to head command will precisely print first line of cat.sh file.

To create a file, you can use touch command. The touch command is the easiest way to create new, empty files. It is also used to change the timestamps. Let's create text.txt file. To add contents to this file I can use echo command, and redirect the output to text.txt file. I can then append more lines by using >>.

When we check contents of this file, we get what we filled that file with.

Now let's open op.sh file, I will show you how to get here documents work. A here document is a special-purpose code block. It uses a form of IO redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor. In our example we will not use any commands, although you can.

A limit string delineates the command list. The special symbol << precedes the limit string. This has the effect of redirecting the output of a command block into the STDin of the program or command. Provide limit string identified at the end of commands list.

When I execute this shell script it just prints the plain text, however if I would have specified commands, then those commands would have ran one after another. You can also use tabs for indenting your code, but make sure that you put dash symbol after <<, it suppresses leading tabs in the body of the document, remember! this option does not suppress spaces. When I run this script, I get the required result.

In next chapter, we will learn using control statements and looping.