What is shell scripting?
There are two broad types of software code: one is compiled and the other is interpreted. Complied programs we have heard of might be Firefox, Libre Office, GIMP or others that “run” on our operating systems (which by the way are also compiled). Interpreted scripts are not “programs” per se because they can not run independently. These rely on a program to get the commands and execute them.
Linux has a lot of commands to choose from even if you are a ‘run of the mill’ user and if you are “root” you have many more at your disposal. All of these commands can be put into a script. A script is just a string of commands to make certain things happen or to get desired information.
So where does the “shell” part come in? BASH (Bourne Again SHell) is the most common shell in Linux and comes in every distribution. The project that I have in mind is a greeting script. The script will allow us to explore several commands and learn a bit more about how Linux documents itself.
The documentation in Linux is called MAN pages, short for manual pages, and you can just type in man and then the command you wish to know more about in the terminal. Like: man date – this would give you information on the date command which we will be using in our script.
Starting off with shell scripting
To begin we need to tell bash to interpolate the script that we are writing. So this needs to go at the top of the page. It is called a hashpling.
Next, I put in a comment that tells about who wrote the script and what it is about.
Date & Time in our shell scripting
Look at the MAN page on date so we have a reference: in a terminal window type in man date this gives you a long list of “switches” that we will be using.
Breaking this all down
date | a command for date and time |
‘+ | single quote and plus sign means add this string |
%A | is the day of the week spelled out – ie: Wednesday |
%d | the 2-digit day of the month |
%B | the 2-digit month of the year |
%Y | the four digit year |
‘ | Single quote ends the string |
%H | Hour on a 24 hour clock (00-24) |
%M | Minutes (0-59) |
%I | Hour on a 12 hour clock |
%p | AM or PM |
Reading in a text file
I’ve gotten sick of sticky notes cluttering my life, so I am using text files to make my life easier for making lists. I have my to do list on one but I need to see it every morning when I turn my computer on. So I need to read the file in to do that. On Linux it easy to do – just cat the file and it will display the contents. That’s it.
User information
I also wanted to know who I was logged in as – seeing that I have several terminal windows open at a time and things can get confusing if you are on multiple machines. To do this we have a command called whoami . This will give us the current user that is logged in.
The shell script so far
We need to make our shell script executable. In the directory that the greeting.sh file is in (let’s say your Documents file).
> cd Documents/(greetings file name) | cd – change directory to |
> chmod 775 greetings.sh | chmod – change/modification 755 makes it execute |
Take the shell script and the text file and make it your own, I encourage you to play around with it and read more on the MAN pages. At first glance, MAN Pages are a bit daunting – here is a good article I wrote on figuring them out. You can also put the commands into your favorite search engine and see what information and examples they have for each command.
In the next few articles I will continue with writing the greeting script with few more modifications to the script itself.
Click here for a zipped up version of the file.