Shell Scripting

What is a Unix Shell?

A shell is an intermediary program  between an Unix user and the internal working of the operating system. It provides an command line interface, analogous to the Windows DoS for the user to communicate with the Unix kernel.

Kernel is the central module of the operating system which bridges the system hardware to the application software.Image

There are various shells available in Unix environment. The most commonly used shells are Bourne shell (sh) , C shell (csh), TC shell (tcsh), Korn shell (ksh) and  Bourne Again Shell (bash)

Normally in a Unix-based OS two shells C shell and Bourne shell will be installed. Apart from that other shells might also can be installed.

To find out which shell was chosen for you, look at your prompt.

If you have  $ prompt, you’re probably in a Bash, Bourne or  Korn shell.
If you have  % prompt, you’re probably in a C shell.

 

Image

Shell Scripting:

Shell scripting in Linux /Unix is programming with the shell using which you can automate your tasks. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff.

Advantages of using Shell Script:
1. Saves time
2. It is not compiled into a separate executable file as a C program is.
Disadvantages:
1. Not fast in comparison to high level languages.
2. Complex code are very difficult to write using shell script.
3. Shell script files uses .sh extension . Shell Scripts are executed in a separate child shell process , and this sub-shell need not to be of same type as your login shell.

Scripting Basics:

How to find the shell you’re currently logged in?

Image

Type “echo $SHELL” in your command line interface; the respective shell script which is currently in use will be displayed.

A shell can perform operations with commands ranging from simple utility invocations like:
$ ls
to complex-looking pipeline sequences like:
$ ps -ef  | sort  | ul  -tdumb  | lp

When these commands put together in a single bundle and made into an executable file, makes a shell script.

 How to write a simple shell script:

1. Use any editor – a ‘vi’ or ‘mcedit’

2. Lets take an example of ‘vi’.  $ vi helloWorld.sh – here helloWorld is the name of the file you are gonna create

3.  Write a simple script as below

HelloWorld

Lines that are starting with ‘#’ are comment lines. ‘echo’ is a unix command to print some text. $USER specifies the current user variable.

‘exit 0’ specifies the status the command that was executed.

(1) If exit’s return value is zero (0), command is successful.
(2) If exit’s return value is nonzero, command is not successful or some sort of error executing command/shell script.

We can specify any negative value to denote various errors that might potentially arise from our script.

4. Now time to  execute the script. You execute the script with ‘./’ command as follows:

HW_Run

Congrats you just ran your first script

Here are some Vi Editor cheat sheet  to play around the editor

Vi editor has two modes:

1. Command mode

2. insertion mode

Initially vi will start will a command mode. Insertion only begins after the insertion or change command. [ESC] brings back us to command mode from insertion mode.

Command Meaning
Insertion
i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters
Motion
h Move left
j Move down
k Move up
l Move right
w Move to next word
W Move to next blank delimited word
b Move to the beginning of the word
B Move to the beginning of blank delimted word
e Move to the end of the word
E Move to the end of Blank delimited word
( Move a sentence back
) Move a sentence forward
{ Move a paragraph back
} Move a paragraph forward
0 Move to the begining of the line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file
:n Move to nth line of the file
fc Move forward to c
Fc Move back to c
H Move to top of screen
M Move to middle of screen
L Move to botton of screen
% Move to associated ( ), { }, [ ]
Deleting Text
x Delete character to the right of cursor
X Delete character to the left of cursor
D Delete to the end of the line
dd Delete current line
:d Delete current line
Quitting
😡 Exit, saving changes (Colon followed by ‘x’)
:q Exit as long as there have been no changes
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes

To Be continued….

 

Leave a comment