Introduction To Shell Scripting Basics
What is a Shell
A shell is an interpreter in UNIX like Operating system. It takes commands typed by the user and calls the operating system to run those commands. In simple terms a shell acts as form of wrapper around the OS. For example , you may use the shell to enter a command to list the files in a directtory , such as ls , or a command to copy ,such as cp.
[linuxtechi@localhost ~]$ ls
config data pam test
config data pam test
In this example , when you simply type ls and press enter . The $ is the shell prompt , which tells you the the shell awaits your commands.The remaining lines are the names of the files in the current directory.
What is Shell Prompt
The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
What are different types of Shells :
since there is no monopoly of shells , you are free to run any shell as you wish. That's all well and good , but choosing a shell without knowing the alternative is not very helpful. Below are lists of shells available in UNIX/Linux.
1. The Bourne Shell
The Original Unix Shell is known as sh , short for shell or the Bourne shell , named for steven Bourne , the creator of sh. This is available on almost all the UNIX like operating system. The Basic bourne shell supports only the most limited command line editing, You can type the Characters,remove characters one at a time with the Backspace key and Press enter to execute the command. If command line gets messed up , you can press Ctrl-C to cancel the whole command.
2. The C Shell
It is desgined by Bill Joy at the university of california at Berkeley , the C shell was so named because much of its syntax parallels that of C programming language. This shell adds some neat features to the Bourne shell,especially the ability to recall previous commands to help create future commands.Because it is very likely you will need to execute more than one command to perform a particular task,this C shell capability is very useful.
3. The Korn Shell
It is created by David Korn at AT&T Bell laboratories , the korn shell or ksh offers the same kind of enhancements offers by the C Shell , with one important difference: The korn shell is backward compatible with the older Bourne shell Synatx. In UNIX like AIX & HP-UX korn shell is the default shell.
4. Bash ( The Bourne Again Shell)
Bash offers command-line editing like the korn shell,file name completion like the C shell and a lot of other advance features. Many Users view bash as having the best of the Korn and C shells in one shell. In Linux and Mac OS X system , bash is the default shell.
5. tcsh ( The T C Shell)
Linux systems popularized the T C shell ot Tcsh. Tcsh extends the traditional csh to add command line editing,file name completion and more. For example , tcsh will complete the file and directory names when you press Tab key(the same key used in bash). The older C shell did not support this feature.
What is Shell Script :
A Shell Script is a text file that contains one or more commands. In a shell script, the shell assumes each line of text file holds a separate command. These Commands appear for most parts as if you have typed them in at a shell windows.
Why To Use Shell Script :
Shell scripts are used to automate administrative tasks,encapsulate complex configuration details and get at the full power of the operating system.The ability to combine commands allows you to create new commands ,thereby adding value to your operating system.Futhermore ,combining a shell with graphical desktop enviorment allows you to get the best of both worlds.
Creating a First Script :
Create a text file in your current directory with a name myscript.sh , all the shell scripts have an “.sh” extension. First line of a shell script is either #!/bin/sh or #!/bin/bash , it is knows as shebang because # symbol is called hash and ! Symbol is called a bang. Where as /bin/sh & /bin/bash shows that commands to be executed either sh or bash shell. Bleow are the contents of a myscript.sh
#!/bin/bash
#Written by LinuxTechi
pwd
date
#Written by LinuxTechi
pwd
date
Assgin the Executable Permissions using below Command :
# chmod a+x myscript.sh
Now Execute the Script.
# sh myscript.sh
OR
# ./myscript.sh
Above shell script will display the current working directory along with date & time of the linux box.
Note: To execute your any shell script available in current directory you would execute using ./<Script-Name>
Taking Input From a User in the Shell Script.
Read command is used to take inputs from user via keyboard and assgin the vaule to a variable. Echo command is used to display the contents.
Example : Modify the myscript.sh file as shown below
#!bin/bash
echo 'What is Your Name?'
read name
echo "My Name is, $name"
echo 'What is Your Name?'
read name
echo "My Name is, $name"
Now Execute the script and will get the below output.
# sh myscript.sh
What is Your Name?
Linuxtechi
My Name is, Linuxtechi
What is Your Name?
Linuxtechi
My Name is, Linuxtechi
No comments:
Post a Comment