How can we do Shell Scripting

A shell script is a file that contains commands that shell can execute. he commands in the shell script can be any commands you can enter in response to a shell prompt.

Let me explain you how to do shell scripting with some examples :

First of all create a new file with extension .sh . Let us name it as calc.sh . This script work as a calculator to find addition, subtration , multilication, division, of 2 numbers.
Apply these commands on terminal to create it.

$ cd Desktop
$ touch calc.sh
$ vim add.sh

Now go to Insert mode in vim editor by ressing ‘i’ and edit the file so that it look as follows :

read a b
echo a= add
echo b= sub
echo c= mul
echo d= div
echo ch
read ch
case $ch in
a)
let z= $a + $b
echo add= $z
;;
b)
let z= $a - $b
echo sub=$z
;;
c)
let z= $a * $b
echo mul= $z
;;
d)
let z= $a / $b
echo div= $z
;;
*)
echo invalid option
;;
Esac

Then save this file by pressing ‘Esc’ and then ‘:wq!’. THis save your file and let you come out of vim editor to command line.
Next you need to run the script.
For this if the permission of file does not allow you to execute the programe then apply the command :

$ chmod 755 -R calc.sh

And then run the script by command :

$ ./calc.sh

This will prompt you to enter 2 numbers and then what operation you want to apply on it. Your choice will be then executed to form your solution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s