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 functions.

We use functions for various reason, one of the most important reason for using and creating functions is, code re-usability. Once you define a function, it can be used again and again. You can invoke the same function as many times as you wish in your program. You can also use functions across different scripts.

Ask yourself! What would you do it you have to teach your computer about a mathematical formula over and over again. You would never be able to finish your program. In bash functions do not return values, Bash functions, unlike other programming languages, do not allow you to return a value to the caller. When a bash function ends its return value is its status, that is either zero for success, or non-zero for failure. To return values however, you can set a global variable with the result, or use command substitution, or you can pass in the name of a variable to use as the result variable.

To write a function, you follow this syntax, function command followed by function name and round brackets, then within curly brackets you specify what code or commands you wish to run.

I will open functions.sh file with nano. I am writing a function show progress, this function will emulate a progress bar. I am using echo with dash NE option, what this does is, it tells echo not to print trailing new lines, and process escape sequences if any of them found. I am also using a sleep command to halt execution by one second. I will finish this script now. To call this function, I will just type show progress without brackets on a new line. I will now save this script and run it. Our progress bar is working OK, and we get the expected result.

Let's now check how I can reuse this function in another script, I will open functions_inc.sh file, to include my previous script function within this script. I can just call that function as per normal now, by adding show progress function name to new line of my script. When I save and run this script, I get the required result.

Let's check how we can pass parameters to our functions, I will open functions.sh file again with nano, and append 2 to show progress function call, now because this is my first argument, I can refer to it within my function with $1, let me update all the references where I want to use this argument value. Alright! let's save this script and run it. Now our sleep method takes 2 as value and halt execution of our script for 2 seconds. When I save and run this script, I get the expected result.

So you see! We learned a fair bit with regards to functions. You can build on this knowledge and explore more by writing your own functions and re-using them.

In next chapter, We will learn how to take input from user and ensuring a response to our script user.