7/28/2019 59940188 Shell Scripting
1/16
SHELL SCRIPT
By
N.Ravikumar
7/28/2019 59940188 Shell Scripting
2/16
What is a Shell?
A shell is an environment in which we can run our commands,programs, and shell scripts.
The shell is often referred to as the UNIX system's commandinterpreter.
echo $0 command will return the type of the shell.
Interactive mode Shell receives input from user and executes it.
7/28/2019 59940188 Shell Scripting
3/16
Jobs ofa Shell
Command Interpreter
Variable creation
I/O redirection(>,>>,
7/28/2019 59940188 Shell Scripting
4/16
Shell Script
Shell script is a list ofcommands stored in a file that getsexecuted without any user intervention (i.e., Non-Interactive).
A shell program runs in 'interpretive'mode. Shell scripts runslower(but powerful) than those written in High level Language.
Non-Interactive Mode
Commonly used shell script naming convention:
filename.sh
7/28/2019 59940188 Shell Scripting
5/16
Variables
A scalar variable holds only one value at a time.
The shell enables you to create, assign, and delete
variables.
Variables are defined as follows:variablename =value
e.gclass=10mx
To access the variableecho $class
7/28/2019 59940188 Shell Scripting
6/16
Cont..
An environment variable is a variable that is available to anyprogram that is started by the shell.
A local variable is a variable that is present within the currentinstance of the shell. It is not available to programs that arestarted by the shell.
A shell variable is a special variable that is set by the shell andis required by the shell in order to function correctly.
7/28/2019 59940188 Shell Scripting
7/16
Naming Convention
The name of a variable can contain only letters ( a to z orA to Z), numbers ( 0 to 9) or the underscore character ( _).
Variable's name can start with alphabets or an
underscore.
e.gValid :
class
_classInvalid:
1classclass@
7/28/2019 59940188 Shell Scripting
8/16
7/28/2019 59940188 Shell Scripting
9/16
7/28/2019 59940188 Shell Scripting
10/16
Arrays
Set of variables.
Syntax:variablename[index]=value
e.g mark[0]=50mark[10]=100
Note: In the above the shelldoes not create blankarraybunch spacesbetween 0and 10.
Array index should be integer only. Accessing the values:
${name[index]}
7/28/2019 59940188 Shell Scripting
11/16
Input/Output
Output(To the screen)- echo (\n,\t,\c,\b)
- printf (C-Language)
Input
- read (shell built-in command)
- To know whether read is a shell built-in command or not interminal type type read
7/28/2019 59940188 Shell Scripting
12/16
7/28/2019 59940188 Shell Scripting
13/16
Parameter Passing
Ability to specify arguments from commandline during theexecution of a shell script is called as parameter passing.
Within our script we can use parameter marker prefixed with
$sign and access the parameter.
e.g#!/bin/shecho "Hello $1"
>>./hello.sh WorldHello world
7/28/2019 59940188 Shell Scripting
14/16
Cont..
$#To count the Total number of positional parameters.
$*To list out all the parameters.
#!/bin/shecho "Total Parameters : $#"printf "List of Parameters : \n $* \n"
7/28/2019 59940188 Shell Scripting
15/16
References
Unix Concepts and Applications,Fourth Edition Sumitabha Das.
Sams Teach YourselfShell Programmin in 24 Hours -Sriranga Veeraraghavan.
http://steve-parker.org/sh/sh.shtml
7/28/2019 59940188 Shell Scripting
16/16
THANK YOU
Top Related