You are on page 1of 16

SHELL SCRIPT

By N.Ravikumar

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 command interpreter.




echo $0 command will return the type of the shell.

Interactive mode Shell receives input from user and executes it.

Jobs of a Shell


Command Interpreter Variable creation I/O redirection(>,>>,<,<<). Pipeline ('|')

Shell Script


Shell script is a list of commands stored in a file that gets executed without any user intervention (i.e., NonInteractive). A shell program runs in 'interpretive' mode. Shell scripts run slower(but powerful) than those written in High level Language. Non-Interactive Mode Commonly used shell script naming convention: filename.sh

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.g class=10mx

To access the variable echo $class

Cont..


An environment variable is a variable that is available to any program that is started by the shell.

A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell.

A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly.

Naming Convention


The name of a variable can contain only letters ( a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _). Variable's name can start with alphabets or an underscore. e.g Valid : class _class Invalid: 1class class@

Arrays


Set of variables. Syntax: variablename[index]=value mark[0]=50 mark[10]=100


Note: In the above the shell does not create blank array bunch spaces between 0 and 10.

e.g

Array index should be integer only. Accessing the values: ${name[index]}

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 in terminal type type read

Parameter Passing


Ability to specify arguments from commandline during the execution 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/sh echo "Hello $1" >>./hello.sh World Hello world

Cont..


$# To count the Total number of positional parameters. $* To list out all the parameters. #!/bin/sh echo "Total Parameters : $#" printf "List of Parameters : \n $* \n"

References


Unix Concepts and Applications, Fourth Edition Sumitabha Das.

Sams Teach Yourself Shell Programmin in 24 Hours Sriranga Veeraraghavan.

http://steve-parker.org/sh/sh.shtml

THANK YOU