Shell Scripting
Shell Scripting
By N.Ravikumar
What is a Shell?
A shell is an environment in which we can run our commands, programs, and shell scripts.
Interactive mode Shell receives input from user and executes it.
Jobs of a Shell
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
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
e.g
Input/Output
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
http://steve-parker.org/sh/sh.shtml
THANK YOU