Introduction To Shell Programming
Kernel and Shell
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.
- 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.
- While the Kernel is the innermost part of an operating system, a shell is the outermost one.
Types of Shell
There are two main shells in Linux:
- Bourne Shell:
The prompt for this shell is $ and its derivatives are listed below:
- POSIX shell also is known as sh
- Korn Shell also knew as ksh
- Bourne Again Shell also knew as bash (most popular)
- Bourne shell (sh)
- C shell:
The prompt for this shell is %, and its subcategories are:
- C shell also is known as csh
- Tops C shell also is known as tcsh
Command To View Supported shell types by system
What Is Shell Scripting?
- SHELL SCRIPTING is writing a series of commands for the shell to execute.
- It can combine lengthy and repetitive sequences of commands into a single and simple script, which can be stored and executed anytime.
- This reduces the effort required by the end user.
steps in creating a Shell Script
- Create a file using a vi editor (or any other editor). Name script file with extension .sh
- Start the script with #! /bin/sh
- Write some code.
- Save the script file as filename.sh
- For executing the script type bash filename.sh
“#!” is an operator called shebang which directs the script to the interpreter location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell.
Let’s create a small script –
Adding shell comments
Commenting is important in any program. In Shell programming, the syntax to add a comment is
There are some characters that are evaluated by the shell in a special way. They are called shell metacharacters or “wildcards.”
These characters are neither numbers nor letters.
the *, ?, and [ ] are used for filename expansion.
The >, >>, and | symbols are used for standard I/O redirection and pipes.
To prevent these characters from being interpreted by the shell they must be quoted.
What are Shell Variables?
Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information and they can used by the shell only.
For example, the following creates a shell variable and then prints it:
echo “what is your name?”
echo “How do you do, $name?”
echo “I am $remark too!”
- Write a shell script that prints “Shell Scripting is Fun!” on the screen
- Store the output of the command “hostname” in a variable. Display “This script is running on _.” where “_” is the output of the “hostname” command.
- Write a shell program that adds two numbers.
- Write a shell program that takes 2 integer values from user and then show the sum of values.
- Operating System-Functions and History
- Generations of Operating System
- Functions of an Operating System
- Components of Operating System
- Types of Operating System
- Services of Operating System
- Properties of Operating System
- Processes in Operating System
- Process Scheduling in Operating System
Lab Practice Task
- Introduction to Linux Ubunto
- Installation with virtual Box
- Writing Linux Commands
- Navigation in File System and Directory Management in Ubunto using CLI
- File Handling and I/O Redirection In Ubunto
- File Access Permission in Linux
- Text Processing Tools and Basic System Configuration Tools in Linux
- Package Management in Linux
- How to manage processes in Linux
- Compiling and Executing C++ programs in Linux
- System Calls
- Introduction To Shell Programming