Introduction to Shell in Linux
The Linux is a code that transmits the system commands., Compilers, Editors, linkers and command-line interpreters are important and useful but they are not part of the operating system. We will look briefly at the LINUX command interpreter, called the SHELL, which, although not part of the operating system, makes heavy use of many operating system features and thus serves as a good example of how the system calls can be used. It is also the primary interface between a user sitting at his terminal and the operating system.
Shell has standard input and output as its terminal. Shell is started when a user begins to login. To start a command a dollar sign is typed which indicates the user that the shell is ready to accept the command. For instance, when a user gives the date as input the there is a child process created and a program begins to run as a child process. Only when the child process is terminated after execution the shell gets back to the prompt and types again and after that, it reads the following lines as input.
Following are the different examples:
It prints the current date and time.
The user can specify that the standard output be redirected to a file,
$date > file
The user can specify that standard input can be redirected, as in
$sort <file1> file2
Which invokes the sort program with input takes from file1 and output sent to file2
The pipe helps in connecting the output of a particular program as input to other programs.
$cat file1 file2 file3 | sort>/dev/lp
Which invokes the cat program to concatenate three files and send the output to sort to arrange all the lines in alphabetical order. The output of sort is redirected to the file /dev/lp, which is a typical name for the special character file for the printer.
Types of Shell
SHELL determines the type of shell that a user sees logging in. Today, there is a host of the shell that accompanies any LINUX system, and you can select the one you like the most. Besides the Bourne shell which is the most popular in the LINUX world, the C and Korn shells have also carved out a niche for themselves, because of certain inherent advantages. The C shell is known by the program csh, the Korn shell by ksh and the bourne shell by sh.
If you wish to use any of the above shell types as the default shell, then the variable needs to be assigned accordingly. However, this assignment is made by the system after reading a field in the file /etc./passwd. If you wish to change the setting permanently, then this file needs to be edited. The system administrator usually sets up your login shell while creating a user account, though you can change it whenever you request to.
Keywords are the words, are called reserved words. Following is the list of keywords available in the Bourne shell.
1. Unchanging variables- set keyword
In some applications, a need may arise for variables to have a constant or fixed value. For instants, if we want that a’s variable should always remain 20 and not changed, we can achieve this by saying,
$a = 20
The shell will not permit to change a value when they are created as read-only. Read-only variables can be created by typing read-only at a command prompt.
When there is a need to clear or erase a particular command from the shell, we will use the “unset” keyword as a command.
$a = 20
$echo a // a have no value now so it prints nothing
2. Echo keyword
To print either the value of any variable or words under double quotation.
echo "Hello World!"
- Pwd command
- Ls command
- Mkdir command
3. Cd command: read keyword
The read statement is the shell’s internal tool for taking input from the standard input. Functionally, it is similar to the INPUT statement of BASIC and the scanf() function in C. But it has one or two interesting features, it can be used with one or more variables to make shell scripts interactive. The input supplied through the standard input during interactive is read into these variables. The script emp1.sh uses the statement to take the search string and the filenames from the terminal.
Command – Shell in Linux
#Script : emp1.sh - Interactive version
#The pattern and filename to be supplied by the user
echo "\nEnter the pattern to be searched : \c"
echo "\nEnter the file to be used :\c"
echo "\nSearching for $pname from the $flname\n"
grep "$pname" $flname
echo "\nSelected records shown above"
Run it, and specify the input accordingly
Enter the pattern to be searched: director
Enter the file to be used: emp2.lst
Searching for director from file emp2.lst
Read can also be used to make the script pause without accepting any input from the user. It can simply wait for the <Enter> key to be pressed for program execution to continue (somewhat like the WAIT command of dBASE). You can also condense the above program to some extent by accepting both the arguments in one line; a single read can also be used with two or more variables names.
In a Linux/Unix operating system, the function of the shell is to interpret the command output. It acts as an interface to run the commands. Linux and Unix shell have additional features than a command Interpreter, it acts as a programming language with basic structures like conditional loops, functions, variables, etc. Shell is a powerful environment that can be used to run the commands and works also as a programming language by using the input commands and executing them. Hence shell is widely used Interface due to its features, a Linux/Unix Shell has more advantages than a Windows Shell.
That is all!