SHELL Scripting

Objectives Introduction to Shell Types of Shell Shell Overview with examples Aliases Numeric Comparisons

By:- Kunal Chhajed

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

What is SHELL…?
Definition:The shell is a program that takes your commands from the keyboard and gives them to the operating system to perform OR The shell is the layer of programming that understands and executes the commands a user enters. In some systems, the shell is called a command interpreter.

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

The SHELL
The UNIX user interface is called the shell. The shell does 4 jobs repeatedly: display prompt read command

the shell
process command

execute command

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Types of SHELL
Bourne Shell Developed by Steve Bourne at AT & T Korn Shell Developed by David Korn at AT & T C Shell Developed by Bill Joy for Berkeley Unix
© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Korn Shell
Aliases – it allows shorthand command History – Lets you recall previously command history Editing – Allows you to edit commands in vi style Support Regular Expressions Highly robust secured features Increased Speed of Shell Command Execution

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Shell Overview
Shell commands can be grouped into a file and executed once Shell Provides Control flow:1) if – then elif then fi 2) while do done 3) for do done 4)case esec 5) functions 6) exit 7) break

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Shell Examples…
1) #!/bin/sh echo “Hello World “ //simple example

2) #!/bin/sh // use of variable echo What is your name? read MY_NAME // script pauses to take input echo “My name is $MY_NAME " Where to use Variables ? a) Setting Pathnames $ mypath = ‘actimize/gms/scripts/gaurav/Kunal’ $ cd $mypath; pwd

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

b) Concatenating Variables and String myfile = foo; ext = .c file = $base$ext // concatenation works here

cc –o $base $file // creates executable foo from foo.c Also file = $base’.c’ c) Command Substitution $ mydir=`pwd` echo $mydir

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Interactive Version – Using Variables
#!/bin/sh echo “Enter the pattern to be searched: “ read pname echo “Enter the file to be used: ” read flname echo “Searching Pattern $pname from $flname” grep “$pname” $flname echo “Finished Successfully”

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Special Parameters Used by Shell
Shell Parameter $1, $2,… Significance Positional Parameter representing command line arguments Number of arguments specified in cmd line Name of executed command Complete set of positional parameters as a string Exit status of last command PID of current Shell

$# $0 $* $? $$

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Example
#!/bin/sh echo “Program: $0” echo “The number of arguments specified is $#” echo “The arguments are $*” grep “$1” $2 echo “Done…!!!” Output:$ sample.sh manager emp.lst Program: sample.sh The number of arguments specified is 2 The arguments are manager emp.lst // display records

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Continued…
3) #!/bin/sh // use of loop for i in 1 2 3 4 5 do echo "Looping ... number $i" done

4) #!/bin/sh // while INPUT_STRING=hello while["$INPUT_STRING" != "bye" ] do echo "Please type something (bye to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING" done

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Continued…
5) #!/bin/sh // case echo "Please talk to me ..." while : do read INPUT_STRING case $INPUT_STRING in hello) echo "Hello yourself!" ;; bye) echo "See you again!" break ;; *) echo "Sorry, I don't understand" ;; esac done echo "That's all folks!"

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

output
$ ./talk.ksh Please talk to me ... Hello // user types Hello yourself! what do you think of politics? Sorry, I don't understand bye See you again! That's all folks! $

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Continued…
6) #!/bin/sh // use of function myfunc() { echo "I was called as : $@“ x=2 } ### Main script starts here echo "Script called” x=1 echo "x is $x" myfunc 1 2 3 // arguments passed to function X=2 echo "x is $x"

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

ALIASES
alias command = value
• •

Creates a pseudonym for a command. Try:

alias printk="echo " printk "hello" unalias command
Removes an alias created with alias.

alias -p

Prints list of aliases.

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Numeric Comparison
-eq – Equal to -ne – Not equal to -gt – greater than -ge – greater than or equal to -lt – less than -le – less than or equal to Examples x=5; y=7; z=7.2 $ test $x –eq $y; echo $? // Ouput:- 1 [False] $ test $x –lt $y; echo $? // Ouput:- 0 [True] $ test $z –gt $y; echo $? // Ouput:- 1 [ 7.2 is not greater than 7] $ test $z –eq $y; echo $? // Ouput:- 0 [ 7.2 is equal to 7]

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

END

© Copyright 2002 - 2006 HSBC Software Development India Pvt. Ltd. All rights reserved.

Sign up to vote on this title
UsefulNot useful