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 how we can interact with user, such as getting input from user during execution of script, or adding flag input to your script, or just accepting arguments for your script from command line.

I will now open my script readin.sh, "Read" command Reads a line from standard input and push it to a bash variable. Here I am reading from standard input and feeding it to my variable Var! I am then echoing Var back. Let's save this script and run it.

Ok! so my script now expects something from me, I will provide it with input hello, and hello will be printed back.

Now, Let's quickly check how we can get hold of argument values within our script. This is not much different than to how we passed arguments to our function in previous chapter, all we have to do is to use dollar symbol with a number, The arguments are stored in variables with a number in the order of the argument starting at 1. $0 is the script name. Let's echo first 3 argument values, save this script and run it with arguments, "hello how areyou". These will print nicely as expected, let's remove an argument at a time and see what consequences does it have. Without a string, echo is just printing a new line without any text.

What if we have dynamic arguments where number of arguments is not known upfront? Bash gives us dollar followed by at symbol, which is an array of all the argument values supplied to the command itself. I can simply iterate through it, and print value of each argument. When I save and run my script, I can supply as many arguments and my script will print each of those. All I am telling you here is how to utilize this feature, the way you use it will depend on your script requirement. Bash also gives is dollar followed by hash ($#) to count the number of arguments passed. When I save my script now and run it, I am told precisely how many arguments were passed on.

To be a bit more professional and making it easier for users to use your scripts, arguments themselves are not always easy or maintainable, Flags and options should be used in those scenarios. When you want to parse command line arguments in a professional way, get opts is the tool of choice. Get opts can easily set shell variables you can use for parsing. Here in my script I have my while statement with my own set of arguments, I and P, I for IP address and P for port number. Then I have an "option" variable, which will get result of the parsed value for my arguments.

I have a "case" command that takes "option" variable, and does pattern matching for option value. In my case I am checking if I is found, then initialize variable IP with value of the argument, which is represented by OPTARG variable. I am repeating the same for argument P, and initializing port variable. I then print my IP address and gathered port number with echo command.

When I save and run this script with dash I or dash p, IP address and port get empty value. However when I supply both the flags with actual IP address and a port number, they are printed on standard output.

I can change my script to add more flags, I will add flags S and M, which will not need any value, when compared to I or P. Let's handle S flag first, if found, then we will print "s flag found". What if a flag is unknown, we can handle that too with question mark as a clause to case statement, if nothing is matched then let's print "cannot understand option" out. We will also add a case for our M flag, if M is matched then we will just print "M flag found". Let's save this script and run it.

First without any arguments, we get the expected result. Then we supply IP address, port number and an S switch. This will print "S flag found", and will also print IP address and port number. If I now add another flag dash K to out command, then we will also got to see ""cannot understand K option".

How can we ensure that if user enter invalid arguments, he or she get a message back from our script. Let's explore that. To my script, I am adding an If condition to check if IP address is empty, then display an error message to the user. Also just as an example I can add another condition utilizing argument count value to check if number of arguments are less than 2! If yes, then print a message. Saving and running this script without any arguments gives us both messages. If I wanted to halt the execution progress of my script on error, I can add exit command within my validation checks. When I save and run my script without arguments, then I get First message, and if I supply minimum required arguments, but does not pass on IP flag then I get second message. However! if I supply valid arguments then my scripts prints IP address and port number properly.

In this chapter, we learned various techniques to interact with users. You should now be able to build on this knowledge and come up with better looking scripts that match your requirements.