Showing posts with label Linux Shells. Show all posts
Showing posts with label Linux Shells. Show all posts

Monday, 5 August 2019

Unix/ Linux :: What is shell and Shell Scripts

What is Shell

Whenever you login to a Unix system you are placed in a program called the shell. The shell acts as a command interpreter; it takes each command and passes it to the operating system kernel to be acted upon. It then displays the results of this operation on completion on your screen.
There are different Flavour of shell in Unix
Finding out which shell you are using  Information about which shell you are using is held in the SHELL environment variable. The command echo $SHELL displays the value of this variable.This displays the default shell of the system. You can change the shell though in the script you execute.You can identify which shell you are presently using from the last part of the pathname.
ShellDescription
Bourne shellThis is the original Unix shell written by Steve Bourne of Bell Labs. It is available on all UNIX systems.
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts. The default prompt on the Unix for this is $
#!/bin/bsh
C shellThis shell was written at the University of California, Berkeley. It provides a C-like language with which to write shell scripts – hence its name.The default prompt on the Unix for this is %
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/csh
Korn shellThis shell was written by David Korn of Bell labs. It is now provided as the standard shell on Unix systems. It provides all the features of the C shell with a shell programming language similar to that of the original Bourne shell. It is the most efficient shell. Consider using this as your standard interactive shell.The default prompt on the Unix for this is $Below  is written in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/ksh

What is shell Scripts:

A shell script is a collection of command which are executed in a order given. There are conditional statement and looping also available like if ,while which helps in finding if a particular value is greater than another value .
To write any comments in the shell scripts ,it has to be written with # preceded
Example
# Auther of the script is
There are some variables which are set internally by the shell and which are available to the user. They are given below in the table.
NameDescription
$1 – $9these variables are the positional parameters.
$0the name of the command currently being executed.
$#the number of positional arguments given to this invocation of the shell.
$?the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$the process number of this shell – useful for including in
filenames, to make them unique.
$!the process id of the last command run in the background.
$*a string containing all the arguments to the shell, starting at $1.
Shell scripts and functions are both interpreted. This means they are not compiled.

Commands in Shell

All shells have a number of built-in commands which are executed in the shell’s own process like echo ,cd .
When you enter a command, the shell checks to see, if the command is a shell built-in such as echo or cd ,it is directly interpreted by the shell.
if the command begins with a / shell assumes that the command is the absolute path name of an executable error occurs if the executable is not found.
if not built-in and not a full pathname shell searches the directories in the PATH from left to right for the executable.Current working directory may not be in PATH
If PATH is empty or is not set, only the current working directory is searched for the executable
Unix commands are executable binary files located in directories with the name bin (for binary). Many of the commands that you use are located in the directory /usr/bin.
A typical value for the PATH variable might be:
/bin:/usr/bin:/usr/local/utils/bin:$HOME/bin

Friday, 2 August 2019

Types of shells in Linux

There are different types of shells available on any linux machine. Primarily we need to understand about, Bourne shell, Korn shell and bash shell etc. But before getting into types of shell, let's understand a bit more on Shell.

What is SHELL?

shell

It is an interface between user and kernel. It is an interpreter that executes the commands given by the user. Shell is not a part of the kernel but it uses the kernel to execute the commands.
Every shell will do the same tasks or functions but the syntax of commands are different for different shells and provide different built-in functions.
In MS-DOS the shell is command.com and the shell in windows the shell is power shell.

What are the shells available on Linux?

  • Some shells are available in the RHEL are c-shell, bash shell, korn shell etc.,
  • To know the list of shells available on our Linux, the command is as follows.
[root@sys3 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

  • /bin/sh represents Bourne shell which is default shell in solaris.
  •  /bin/bash represents Bourne again shell. This shell is improved version of Bourne shell. This is the default shell in Linux.
  • /bin/dash represents DebianAlmquist shell. It is the default shell in Debian OS.
  • /bin/csh represents c-shell.
  • /bin/tcshtepresents advanced c-shell.

What is “/sbin/nologin”?

From the above output all shells are available in /bin directory. Except for one thing that is “/sbin/nologin”.
  • /sbin/nologin - represents the no shell.
For example I am creating a user for the authentication of FTP server. For this user there is no need to have a shell to download the files. At that situation as an admin we can create a userid with no shell.
This makes possible by using “/sbin/nologin” with attribute –s of “useradd” command. To know about creating the user having noshell click here.
Note: 
This attribute is performed by the administrator only. Because of that it lies on /sbin directory.  /sbin represents super user binary executable files.

How to know the default working Shell?

To know the default shell of the particular Linux operating system the command is,
[root@sys3 ~]# echo $SHELL
/bin/bash

How to go from one shell to another shell?

To change the current working shell simply enter the individual shell command in the terminal.
If you want to change the shell from bash to c- shell simply enter the command as “csh”. To change to bash shell, type "bash" in terminal window.

How to check our current working shell?

To check the current working shell type the command # echo $0.
[root@sys3 ~]#csh
[root@sys3 ~]# echo $0
Csh

How to revert back to the previous shell?

To revert from that shell simply type "exit" on the terminal.
[root@sys3 ~]# exit
exit
[root@sys3 ~]# echo $0
-bash