In the previous article on Linux Shell Scripting Language, we had already covered following points:
- What is a Shell?
- Types of Linux Shell- Bourne Shell, C Shell, Korn Shell and the BASH Shell (Bourne Again Shell).
- Shell Programming - The Basics
- Writing the First Shell Script - Hello World
In this article, we would write some more basic shell scripts that will give you much more comfort while writing more complex scripts. Please note that, this article is an extension of my previous article "Getting Started - Linux Shell Scripting Language".
So, without much a do, lets start the show.
1. Fun with the $USER
In the first script of this article, we will make use of the shell variable $USER. It stores the information about active user of the system. Just observe the following script that displays IP Address Details to its user.
Remember, whenever you try to create a script, it should have an extension .sh and make it executable before you run it.
Script is nothing but a number of commands pipelined together. Shell Scripts are just great for concise file-system operations. When you need Better functionality, better robustness, better performance and better efficiency then you may adopt a more full-featured language.
If you are acquainted with programming languages like C or Python or Perl, then you can learn Shell Scripting with much more comfort.
2. An Interactive Script
In the second script of this article, we expect the user to take part in the communication. This script will take texts from the user, save it in some variables and using those variables, something is displayed back on the screen for the user.
3. Build a Pyramid of Stars
Most of us might have done this program in C language. In the similar fashion, using for loops, we can display a pyramid of stars (*s) using Shell scripts. Go through the program and you will find the commands self explanatory.
4. Make It Colorful
Shell scripts commonly used ANSI escape codes for color output. Following table shows Numbers representing colors in Escape Sequences.
To make use of these colors in your scripting, use following syntax:
echo -e "\033[<code>m <Your Text>"
echo -e "\033[32m Your Own Linux...!"
Not only this, but you can make your text more fancy by giving them some text attributes. The following table summarizes numbers representing text attributes in Escape Sequences.
5. Compress a File/Folder
Let us now create a script that will compress a file/directory specified by the user. This is yet another interactive script which will ask the user to input the exact file/directory name (along with extension if any), compress it and output a compresses version of the same with the name 'archive.tar.gz'.
6. Email Alert!
Time to create a script that will send you an email when disk utilization exceeds some pre-determined threshold. If you are a web administrator, then this script will prove to be pretty essential for you. Check out the script:
You need to make a few changes in the script, replace the field 'USERNAME@domain.com' with your Email ID and it's done.
That's all for this article! There are no boundaries when it comes to writing scripts and programming stuffs, you can create as many scripts as you want. If you like this article or you have some suggestions, fell free to comment and stay tuned for some more articles on Linux Shell Scripting Language. Happy Scripting!