Basics of UNIX SHELL Scripting : Part-1

Whenever any newcomer or beginner wants to learn any new programming language like learning unix shell scripting, questions comes to mind is from where do I start? What concepts I need to explore? How much time will it take to learn?

This article will help to newcomers to find answers of these type of questions.

Let’s start with Variable,

  1. How to access shell Variable

A variable is name or symbol representing a value which we want to use in our script. Definition of Variable in Shell scripting is similar to normal variable. Difference is using or declaring it.

There are default UNIX shell variable defined and to display that, run ‘env’ command.

Sample output will be

$ env

HISTFILE=/home/lfl/.history

PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

SHELL=/bin/ksh

HOSTNAME=livefirelabs.com

USER=Thompson

MAIL=/var/spool/mail/lfl

HOME=/home/thompson

HISTSIZE=1000

If one want to display value of single Shell variable, for example ‘HOME’, run below command

$ echo $HOME

home/Thompson

$

Let’s access this $Home variable through Shell scripting and it’s not much of a difference,

#!/bin/ksh

#This shell script example accesses and displays the variable HOME

echo”The value of the shell variable HOME is $HOME”

exit 0

SAMPLE RUN:

$  ./script

The value of the shell variable HOME is ‘/home/thompson’

$

      2.    Use Shell Scripts Arguments

Another important aspect of using Shell Script Arguments. These arguments help to access data through shell scripts. If you aware of the UNIX operating system commands then you must have come across these line Arguments like a simple ‘mv’ command of UNIX

$ mv file1 file2

Now see how we can rename file1 to file2 using this ‘mv’ command. Make sure you assign different variable within script for each arguments as below

#!/bin/bash

#Use of shell script arguments

Var1=$1

Var2=$2

echo “the first shell argument is ‘$Var1’”

echo “the second shell argument is‘$Var2’”

mv $Var1 $Var2

exit 0

SAMPLE RUN:

$ ./script file1 file 2

the first shell argument is‘file1’ the second shell argument is‘file2’

mv file1 file2

    3.   The Shell Script ‘IF-THEN-ELSE’ Statement

Like other programming languages such ‘C’, ‘JAVA’, ‘html’ etc. UNIX shell script also support IF-then-else statement but with below syntax

Syntax:

Make sure you always add fi to end ‘IF’ statement.


if expression

then                                                                                                                                                                 

command1

else                                                                                                                                                                 

command2                                                                                                                                                                    

 fi

The command1 will be executed only if expression evaluates true and if expression evaluates false then command2 will be executed i.e. command1 will be skipped.

Now let’s take an example,

#!/bin/ksh

#This shell script example demonstrates use of the IF-ELSE-THEN statement

A=$1

B=$2

if [ $A -gt $B ] then

echo “$A is greater than $B”

else echo “$B is greater than $A”

fi

exit 0

SAMPLE RUN :

$ ./script 3 5

5 is greater than 3

$

 


(Edit)

Leave a Reply

Logged as admin. Log out »

© 2017 Database ETL. All rights reserved.
Skip to toolbar Log Out