Dear Unixmen Community, We are pleased to announce shell scripting series for our valued readers, who may not have a sound understanding of Shell Scripting Language. By the end of this series you should be able to write shell scripts, very fluently for all possible test cases. In this tutorial you will know what is a shell, what are the types of shell, what is a shell script, How to write a shell script, prerequisite for writing shell script, writing and executing your first shell script.
What is (Unix) Shell?
Unix Shell commonly called as Shell is a command-line-interpreter which is native to Unix/Linux Operating System. The user interacts with shell directly by entering commands or a series of command, for the shell to interpret and execute it. All modern shells has inbuilt support for wildcards, pipes, variables, iteration, conditional operations, command substitution with proper documentation. In simple language shell can be understood as an interface that connects users to the Operating System.
Types of Shell
There are various types of Shell. Bourne Shell and C shell are most widely used. Infact Bourne Shell has been used as coding base for several derivatives and shells with additional features.
1. Bourne Shell : sh aka Bourne shell’s standard path in a Linux System is /bin/sh. It was written by Stephen Bourne. It is so famous that on a lot of distribution sh is a symbolic/Hard link for other shells. The License of Bourne Shell is still under discussion.
2. C Shell : csh aka C Shell was written by Billy Joy. The C shell is widely distributed with BSD Unix. C shell introduced many features to accomplish task interactively viz., aliases, history, job control, etc. It is written completely in c programming language. The stable release of tcsh is 6.19. This is released under BSD license.
3. Fish : Fish Stands for Friendly Interactive Shell. It has changed the way of users interacting with the Linux Shell with features like Universal Variables, Helpful Error Messages, Tab Completion, Syntax highlighting, Smart terminal and clipboard handling, searchable command history and what not.It is released under GPL v2. The current stable release is 2.2.0.
4. Bourne-Again Shell: This is the default Shell on most of the Linux Distributions of today (and Mac OS). It was written as a part of the GNU Project. It is written mostly in c programming language and released under GNU General Public License v3+. The current stable release is 4.3.42.
Some other Linux shell are
Debian Almquist shell aka dash
Korn Shell aka ksh
Z shell aka zh
What is Shell Script
A shell script is a program designed to be interpreted by Linux Shell. A shell script can be used for a variety of tasks like program execution, file manipulation, environment setup, etc. For Linux Administrators shell script can be considered as a lifeline to automate their repetitive day-to-day task like Backup, restore, file operations and others. Shell script makes it possible to allow passing multiple commands to Linux shell to be executed automatically without waiting for the user to trigger each command manually. Depending upon requirement you may use the output of one command as an input for the other command, all defined within a single script.Shell Scripting skill is must for a Linux Administrator to save time and resource.
How to write a Shell Script
You should use editors like gedit or vi/vim, nano, Emacs to write shell scripts. Don’t use any IDE as of now. If you do that you will miss a lot of things to learn. For the sake of simplicity, we will use gedit (installed by default on most of the present day Linux distributions). It is important to start a shell script starting with the below line of code.
In the above character sequence ‘#!’ is called shebang. The later part of the above sequence ‘/bin/bash’ pass instruction to run the program /bin/bash. You may replace /bin/bash with your shell path, however, for the sake of simplicity, let it be /bin/bash for now. We can write comments in shell script after ‘#’. The shell ignores anything and everything after ‘#’.You can save shell with any extension or without any extension at all. However, the convention is to save a shell using with extension ‘.sh’ and we will follow the convention, though it makes no difference at least in execution and output.
A shell script must always have the executable bit to run it. Do chmod 755 YOUR_SCRIPT. Never do chmod 777 YOUR_SCRIPT. Just do
$ chmod 755 YOUR_SCRIPT.sh
1. You need a Linux Box. You may have installed GNU/Linux as your primary OS or in Virtual environment on Windows.
2. All the script/command shall work on all Linux distributions hence no distribution war here. Choose what you feel comfortable.
3. Basic knowledge of Linux Operating System and Commands.
4. Never, Never, Never use any IDE (Integrated Development Environment), at this stage. Instead, use editors like vi, nano or gedit.
5. Follow every part of writing and don’t escape any.
Writing your First Shell Script
Open gedit program and write the below piece of code (don’t copy-paste).
#!/bin/bash echo "Hello Unixmen Community!"
What is going on here:echo is a Linux command and it is printing the line “Hello Unixmen Community!” on the standard output.
Once done, save it as anything.sh, say my_first_program.sh. Make it executable as
$ chmod 755 my_first_program.sh
and Run it as
Hurrah! You have written your first Shell program and run it successfully, without any error. Wasn’t it easy?Lets make our second Shell program, that could accept input from the user interactively.
#!/bin/bash echo "Hey there! What is your name?" read a; echo "Hey $a! what is your Favorite Linux Community?" read b; echo -e "That's Nice to know @$a that $b is your favorite Linux Community./n How many years of experience you have in Linux?" read c; echo "That's great. Keep Connected to Unixmen $a. The fun has just started."
What is going on here?
1. echo “Hey there! What is your name?” is printing this line on standard output. The Input of “What is your name?”, is stored in variable a. Command read will read from a file descriptor.
2. echo “Hey $a! what is your Favorite Linux Community?” is printing this line on standard output. The Input of “what is your Favorite Linux Community?”, is stored in variable b.
same goes for other ‘echo’ and ‘read’ line in this script.
Save it as my_second_program.sh and make it executable as you did in first program.
It feels great, when you write something and run it successfully, isn’t it? As said in the above program that the fun has just started, we will take you through the journey of Linux Shell Scripting in my upcoming posts, where you will get a lot to learn and try. Keep connected to Unixmen till then. Don’t forget to give your valuable feedback in the comments Like and share us and give help us get spread. The best is yet to come.
转载本站任何文章请注明：转载至神刀安全网，谢谢神刀安全网 » Linux Shell Scripting part-1: Starting with Linux Shell Scripting