Introduction To Shell Scripting
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible.
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.
An Operating is made of many components, but its two prime components are –
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
Also Read: How to Install CentOS 8
A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.
The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.
You may use the shell to enter a command to list the files in a directory , such as ls , or a command to copy ,such as cp.
[[email protected] ~]$ ls 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 designed 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 Syntax. 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. Furthermore ,combining a shell with graphical desktop environment 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. Below are the contents of a myscript.sh
#!/bin/bash #Written by DataMounts pwd date
Assgin the Executable Permissions using below Command :
# chmod a+x myscript.sh
Now Execute the Script.
# sh 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 assign the value 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"
Now Execute the script and will get the below output.
# sh myscript.sh What is Your Name? DataMounts My Name is, DataMounts
Kernel is the nucleus of the operating systems, and it communicates between hardware and software. Shell is a program which interprets user commands through CLI like Terminal. The Bourne shell and the C shell are the most used shells in Linux. Shell scripting is writing a series of command for the shell to execute. Shell variables store the value of a string or a number for the shell to read. Shell scripting can help you create complex programs containing conditional statements, loops, and functions.