Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Advance Shell Scripting Training

Uploaded by

Mrinal Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Advance Shell Scripting Training

Uploaded by

Mrinal Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 129

Linux - Advanced Shell Scripting

Subhasis Samantaray
Course Content
History of Shells Comparison:
A Basic Script Arithmetic Comparison
Bang line or interpreter line String Comparison
Command separator [semicolon] File test operator
Special Characters Time comparison
Comment line Compound comparisons
Various methods of invoking Shell scripts Programming constructs:
Shell Variables: Command Exit status and control branching
Displaying Currently defined shell variables If statement and its variants
Default Values for Variables Validating the count and content of command line arguments
Un-defining variables Generating Usage messages.
Exporting variables Case statement
Fetching variable values from the user While Loop
Environmental variables until statements
Variable substitution, Break and continue
Command Substitution For Loop
Numeric operations in scripting with bc, expr command and Select Loop
double parenthesis construct Combining Commands with && and ||
Shell Quoting Mechanism: Complete and partial quoting
Special Shell variables
Positional Parameter and use of shift
Predefined parameters
Command exit status

2
Course Content
Shell Features: Working with Signals in Scripts:
Command history and history expansion trap and kill commands
Aliases Preventing Abnormal Termination
Sourcing of shell scripts Temporary Files deletion in signal handlers
Initialization of login shells, non-login interactive shells Wait & Sleep
non-login non-interactive shells Debugging shell scripts
shell expansions: File descriptors
Brace expansion Here documents:
~ expansion Automate FTP
Parameter expansions: DB connectivity scripts
Arithmetic expansions
Command substitution
IFS field splitting
File/Path name expansions
Using the eval command for extra cycle of shell expansions
Function & Return value:
Program Structure
Defining and Calling a Function
Function Parameters and Function Return Values
Functions in Other Files, Recursive function calls.

3
Course Content
Concepts of Awk: Concepts of Sed:
Introduction to awk
The awk programming model Sed Stream Editor –Introduction
records and fields Sed syntax , How does sed Treat files
Arithmetic Expressions Scripts : Basic Script types
Unary arithmetic operators Basic Principles of sed
The Auto increment and Auto decrement Operators Addressing
Assignment Operators Regular expressions meta characters for sed
Conditional expressions Sed commands : Append, Insert , Change,Delete,Substitute
Regular Expressions Replacement pattern examples
And/or/Not
pattern matching System Monitoring tools
Awk programming
AWK Built-in Variables
FS - The Input Field Separator Variable
OFS - The Output Field Separator Variable
NF - The Number of Fields Variable
NR - The Number of Records Variable
RS - The Record Separator Variable
ORS - The Record Separator Variable
FILENAME - The Current Filename Variable"

4
History of Shells

5
A Basic Script

#!/bin/ksh INTERPRETER LINE /BANG LINE


##INFORMATION ABOUT THE SCRIPT ####
## HIGH LEVEL FUNCTIONALITY #### COMMENT /VERSION HISTORY /REVIEW
## Author ,Change /Create Date #### HISTORY
## Review Log if any #### HIGH LEVEL FUNCTIONALITY

if [ $# -ne 0 ]
then USAGE INFORMATION
echo "USAGE : $0 without any command line params" COMMAND LINE PARAM VALIDATION
fi

INPUT VALIDATION /BUSINESS LOGIC


COMMANDS / STATEMENTS /BUSINESS LOGIC CONTROL FLOW /EXECUTION
LOGGING

6
Bang line or interpreter line
 The sha-bang ( #!) at the head of a script tells your system that this file is a set of commands to be fed
to the command interpreter indicated.

 The #! is actually a two-byte magic number, a special marker that designates a file type, or in this case
an executable shell script

 Immediately following the sha-bang is a path name. This is the path to the program that interprets the
commands in the script, whether it be a shell, a programming language, or a utility.

 This command interpreter then executes the commands in the script, starting at the top (the line
following the sha-bang line), and ignoring comments.

Example :

#!/bin/sh
#!/bin/ksh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/usr/awk –f

A simple shell script :

#!/bin/ksh
echo “Welcome to the word of scripting”
7
Command separator [semicolon]

Command separator [semicolon]. Permits putting two or


more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then # Note the space after the semicolon.
echo "File $filename exists."; cp $filename $filename.bak
else
echo "File $filename not found."; touch $filename
fi; echo "File test complete."

8
Terminator in a case option [double semicolon]

case "$variable" in
ch1) echo "\$variable = ch1" ;;
ch2) echo "\$variable = ch2" ;;
esac

9
Special Characters
# Comments
; Command separator [semicolon]
;; Terminator in a case option [double semicolon]
. "dot", as a component of a file/dir name to create hidden files/dirs.
"dot" is also used in case of sourcing / Equivalent to source.
.. "dot“ represents the current directory , and two dots represents the parent directory.
"dot" used in regular expression for character matching.

“ partial quoting [double quote]


‘ Full quoting
, Comma operator , Example : let "VAR2=((VAR1=VALUE1,VALUE2))"
\ escape
/ PATH Separator / Arithmetic operator
` command substitution
: Null command ( No operation )
! Reverse/Negate
* wild card / Arithmetic operator
10
Special Characters
? wild card / character serves as a single−character "wild card" for filename expansion
$ variable substitution / End of line in regular expression
${ } Parameter substitution
$@ Positional parameters
$*
$? Exit status variable
$$ PID of the shell
$! PID of last back ground job
$_ Argument of the previous command
$- Shell Settings
() Command grouping [ Starts a subshell ]
{} Brace expansion / Place holder in find / xargs
[] Test / Array Element / Range of characters
[[ ]] Test
$IFS Internal Field Separators: the set of characters (normally space and tab) which are
used to parse a command line into separate arguments

11
Special Characters
(( )) Integer expansion
| Passes output of previous command to the next one
|| or
&& and
& Run a job in back ground
- Option Prefix/Previuos working directory / Minus
= Equals
+ Plus / Some times used as option flag ( Example : set +vx )
% Modulo
~ Tilde / Home directory
~+ Current working Directory
~- Previous working Directory
=~ Regular expression match
^ Begining of line
Control Characters ( CTRL+C ,CTRL+D,CTRL+Z,CTRL+S,CTRL+Q,CTRL+L,CTRL +M .....)

12
Comment lines
 Comments. Lines beginning with a # (with the exception of #!) are comments and will
not be executed.
# This line is a comment.

 Comments may also occur following the end of a command.


echo "A comment will follow." # Comment here.
# ^ Note whitespace before #

 Comments may also follow whitespace at the beginning of a line.


# A tab precedes this comment.

Comments may even be embedded within a pipe.


initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\
# Delete lines containing '#' comment character.
sed -e 's/\./\. /g' -e 's/_/_ /g'` )

echo "The # here does not begin a comment."

echo 'The # here does not begin a comment.'

echo The \# here does not begin a comment.

echo The # here begins a comment.

13
Various methods of invoking Shell scripts
We can execute the script in different ways
1)<SHELLNAME> <SCRIPT | SCRIPTNAME with Path>

Exacmple :
ksh Welcome.ksh
ksh /opt/proj/app/scripts/Welcome.ksh

2) chmod +x scriptname ; ./scriptname

Exacmple : [ Assume . is not present in path variable ]


chmod +x Welcome.ksh ;./ Welcome.ksh

3) chmod +x scriptname ; scriptname

Exacmple : [ Assume . is present in path variable ]


chmod +x Welcome.ksh ;Welcome.ksh

4) . <PATH OF SCRIPT>
Exacmple : [ This is used to set the environment or to execute the script in the same shell ]
. /opt/project/app/scripts/Setenv.ksh
. Setenv.ksh
. ./Setenv.ksh
5) From vi editor using :! ScriptName

14
SHELL Variables : Displaying Currently defined shell variables

Execute set / printenv / env to check the defined shell variables in your current shell
PS1 This is the main prompt, seen at the command line
PS2 The secondary prompt, seen when additional input is expected. It displays as
">"
PS3 The tertiary prompt, displayed in a select loop
PS4 shown at the beginning of each line of output when invoking a script with the
−x option. It displays as "+"
PWD Present working Directory
OLDPWD Previous working directory
PATH Path variable
TERM Terminal Name
HOME HOME Directory
LOGNAME Login user name
HOSTNAME Host/Server Name
SHELL Shell Name
MAIL Mail File Name

15
Default Values for Variables

${VARIABLE:=VALUE}

•Substitute the value of $VARIABLE, if its defined and value is NOTNULL


•Assign the VALUE to the VARIABLE, if its defined and value is NULL
•Assign the VALUE to the VARIABLE ,if its not set/defined
Example:

16
Default Values for Variables

VARIABLE Set and Not Null VARIABLE Set But Null VARIABLE Unset

${VARIABLE:-VALUE} Substitute VARIABLE Substitute VALUE Substitute VALUE

${VARIABLE-VALUE} Substitute VARIABLE Substitute null Substitute VALUE

${VARIABLE:=VALUE} Substitute VARIABLE Assign VALUE Assign VALUE

${VARIABLE=VALUE} Substitute VARIABLE Substitute null Assign VALUE

17
Shell Variables

currdir=`pwd` RIGHT SYNTAX


currdir = `pwd` WRONG SYNTAX
if test "$currdir" = "/home/john"; then RIGHT SYNTAX
if test "$currdir"="/home/john"; then WRONG SYNTAX
YEAR=2010 RIGHT SYNTAX
YEAR = 2010 WRONG SYNTAX
YEAR =2010 WRONG SYNTAX
DATE=“01 MAY 2010” RIGHT SYNTAX
DATE=01 MAY 2010 WRONG SYNTAX

18
Exporting variables (Change or Set Environment Variable )

To change the environment variable for the current session


execute the following commands based on your current shell :

Syntax for KSH :


VAR=VALUE
export VAR
Syntax for BASH :
export VAR=VALUE
Syntax for CSH :
setenv VAR VALUE

19
Shell Startup Configuration Files For Environment Variable

KSH :
/etc/profile - This default system file is executed during login and sets up
default environment variables
$HOME/.profile - These files are executed every time KSH starts

BASH :
/etc/profile - This default system file is executed during login and sets up
default environment variables
$HOME/.bash_profile - These files are executed every time BASH starts

CSH :
/etc/csh.login - It is executed if C shell is your login shell.
$HOME/.cshrc and $HOME/.login - These files are executed every time C
Shell starts
20
Environmental variables

To display all environment variables and the values :

set / env / printenv

To display the value of any specific shell variable , Execute :

echo $VAR_NAME

Example:
echo $PATH
echo $PS1

21
Variable/Command substitution
Variable substitution :
The name of a variable is a placeholder for its value.
Referencing (retrieving) its value is called variable substitution.
Example :
VAR=VALUE
echo $VAR
command substitution :
command substitution in Unix shells allows a command to be run and its output will
be used on the command line as arguments to another command .
Also command substitution is used in shell script to capture the output of a command
and store it in a variable.

To do the command substitution we can either use :


$(command) or `command`
Example :
TODAY=$(date)
TODAY=`date`
22
readonly Variable

readonly:
VAR=VALUE
readonly VAR

declare:
declare –r VAR=VALUE

typeset:
typeset –r VAR=VALUE

Note : Use typeset –i / declare –i to set the variable type as integer

23
Numeric operations

$ A=5;B=12
$ echo $(( A + B ))
17
$ echo $(( $A + $B ))
17
$ ( echo scale=3 ;echo 5 / 2 )|bc
2.500
$ echo "2.3 + 4.5" |bc
6.8
$ echo "5.0 / 2.1" |bc
2
$ ( echo scale=3 ;echo 5.0 / 2.1 )|bc
2.380
$ expr 2.3 + 4.5
expr: non-numeric argument
$ expr 2 + 4
6
24
Double Parentheses Construct
We can use this construct
To set a value in C style syntax
For Post−increment,Post−decrement ,Pre−increment and Pre−decrement
C−style trinary operator.
This syntax may not work in some of the KSH versions .

#!/bin/bash
echo
(( a = 23 )) ;echo "a (initial value) = $a"
(( a++ )) ;echo "a (after a++) = $a"
(( a-- )) ;echo "a (after a--) = $a"
(( ++a )) ;echo "a (after ++a) = $a"
(( --a )) ;echo "a (after --a) = $a"
(( t = a<45?7:11 )) ;echo $t
a=56
(( t = a<45?7:11 )) ;echo $t
echo

Refer Advance bash shell scripting guide to know more about : The Double Parentheses
Construct
25
Shell Quoting Mechanism: Complete and partial quoting

partial quoting [double quote] :


"STRING" preserves (from interpretation) most of the special characters
within STRING .

Full quoting / Complete quoting [single quote] :


'STRING' preserves all special characters within STRING.

26
Shell Quoting Mechanism: Complete and partial quoting

27
Special Shell variables
Variable Meaning
$0 Name of script
$1 Positional parameter #1
$2 − $9 Positional parameters #2 − #9
${10} Positional parameter #10
$# Number of positional parameters
"$*" All the positional parameters (as a single word)*
"$@" All the positional parameters (as separate strings)
${#*} Number of command line parameters passed to script
${#@} Number of command line parameters passed to script
$? Return value
$$ Process ID (PID) of script
$− Flags passed to script (using set)
$_ Last argument of previous command
$! Process ID (PID) of last job run in background
28
Positinal Parameter and use of shift

The command-line arguments $1, $2, $3,...$9 are positional parameters ,


with $0 pointing to the actual command, program, shell script, or function
and $1, $2,$3, ...$9 as the arguments to the command.

The positional parameters, $0, $2, etc., in a function, are for the function’s
use and may not be in the environment of the shell script that is calling the
function. Where a variable is known in a function or shell script is called the
scope of the variable.

The shift command is used to move positional parameters to the left; for
example, shift causes $2 to become $1. We can also add a number to the
shift command to move the positions more than one position; for example,
shift 3 causes $4 to move to the $1position.Sometimes we encounter
situations where we have an unknown or varying number of arguments
passed to a shell script or function, $1, $2, $3... (also known as positional
parameters). Using the shift command is a good way of processing each
positional parameter in the order they are listed.

29
Command exit status

$? is a special shell variable which is used to store exit status of command.

$? always expands to the status of the most recently executed foreground
command.

If command executed successfully $? returns 0 (Zero)

if exit status returns non-zero value then command is failed to execute
/error occurred during execution .

Example :
$date
$echo $?

30
Arithmetic Comparison

−eq Equal to
−ne Not equal to
−lt Less than
−le Less than or equal to
−gt Greater than
−ge Greater than or equal to

31
String Comparison

= Equal to
== Equal to
!= Not equal to
\< Less than (ASCII) *
\> Greater than (ASCII) *
−z String is empty
−n String is not empty

32
File test operator
−e File exists −t File is associated with a terminal

−f File is a regular file −N File modified since it was last


read
−d File is a directory
−O You own the file
−h File is a symbolic link −G Group id of file same as yours
−L File is a symbolic link ! "NOT" (reverses sense of above
tests)
−b File is a block device −s File is not zero size
−c File is a character device −r File has read permission
−w File has write permission
−p File is a pipe
−x File has execute permission
−g sgid flag set
F1 −nt F2 File F1 is newer than F2 *
−u suid flag set
F1 −ot F2 File F1 is older than F2 *
−k "sticky bit" set
F1 −ef F2 Files F1 and F2 are hard links to
−S File is a socket
the same file *

33
Compound comparisons
-a
logical and

exp1 -a exp2 returns true if both exp1 and exp2 are true.

-o
logical or

exp1 -o exp2 returns true if either exp1 or exp2 are true.

These are similar to the Bash comparison operators && and ||, used within double
brackets [[ ]].

if [[ condition1 && condition2 ]]

if [[ condition1 || condition2 ]]

34
If statement and its variants
if [ condition ] if [ condition ]
then then
Statements …. Statements ….
fi else
if [ condition ]
if [ condition ] then
then Statements ….
Statements …. else
else Statements ….
Statements …. fi
fi fi

if command if [ condition ]
then then
Statements …. Statements ….
else elif [ condition ]
Statements …. then
fi Statements ….
else
if test …… Statements ….
then fi
Statements ….
else
Statements ….
fi

35
Example : If statement and its variants
#!/bin/bash

echo "Enter 2 numbers :"


read N1 N2

[[ ${N1:=NULL} = "NULL" ]] && echo "Input cant be null " && exit

Nchk=`echo $N1 $N2 |tr -d '[:digit:]. \012' |wc -c`

[[ $Nchk -ne 0 ]] && echo "Non numeric Input" && exit

[[ $N1 -eq $N2 ]] && echo "Both numbers are same" && exit

if [ $N1 -gt $N2 ]


then
echo "$N1 >$N2 "
else
echo "$N1 < $N2 "
fi

36
Example : If statement and its variants

37
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh

if grep -li "Qwest" ./* >/dev/null if ls Qwest.txt >/dev/null 2>&1


then then
echo "Qwest is present in the following files" echo “Qwest.txt : file found"
grep -li "Qwest" ./* else
else echo " Qwest.txt : file not found"
echo "Qwest is not present in any of the files" fi
fi
#!/bin/ksh
#!/bin/ksh echo “Enter a number”
if test -f Qwest.txt read num
then if test $num –gt 3
echo "Qwest.txt : File found " then
else echo “$num is greter than 3"
echo " Qwest.txt : File not found " else
fi echo " $num is less than 3"
fi

38
Example : If statement and its variants

39
Example : If statement and its variants

40
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh
VALIDP="Qwest123" #Password
echo "1 - Start the App"
echo "Please enter the password:" echo "2 - Shutdown the App"
read PASSWORD echo "3 - Bounce the App"
if [ "$PASSWORD" == "$VALIDP" ] echo "0 - Exit"
then
echo "ACCESS GRANTED" echo "Enter the choice [0-3]"
read choice
else
echo "ACCESS DENIED!!!!" if [ $choice -eq 1 ]
fi then
echo "Selected Option -- 1"
else
if [ "$choice" = "2" ]
then
#!/bin/ksh
echo "Selected Option -- 2"
else
CURRENT_TIME=`date +"%T"` if [[ $choice -eq 3 ]]
echo " Current time : $CURRENT_TIME "
then
echo "Selected Option -- 3"
if [ $CURRENT_TIME \> 12:00:00 ]
else
then
if [ $choice -eq 0 ]
echo "TIME is greater than 12:00:00"
then
else
echo "Exit -- Selcted choice 0 "
echo "TIME is less than 12:00:00"
else
fi
echo "Invalid Choice"
fi
[[ $CURRENT_TIME \< 23:00:00 ]] && echo "TIME is less than 23:00:00"
fi
fi
fi

41
Example : If statement and its variants

42
Example : If statement and its variants

43
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh

echo "1 - Start the App" echo "1 - Start the App"
echo "2 - Shutdown the App" echo "2 - Shutdown the App"
echo "3 - Bounce the App" echo "3 - Bounce the App"
echo "0 - Exit" echo "0 - Exit"

echo "Enter the choice [0-3]" echo "Enter the choice [0-3]"
read choice read choice

if [ $choice -eq 1 ] if [ $choice -eq 1 ]


then then
echo "Selected Option -- 1" echo "Selected Option -- 1"
else elif [ "$choice" = "2" ]
if [ "$choice" = "2" ] then
then echo "Selected Option -- 2"
echo "Selected Option -- 2" elif [[ $choice -eq 3 ]]
else then
if [[ $choice -eq 3 ]] echo "Selected Option -- 3"
then elif [ $choice -eq 0 ]
echo "Selected Option -- 3" then
else echo "Exit -- Selcted choice 0 "
if [ $choice -eq 0 ] else
then echo "Invalid Choice"
echo "Exit -- Selcted choice 0 " fi
else
echo "Invalid Choice"
fi
fi
fi
fi

44
Example : If statement and its variants

45
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh

# /dev/sda2 is a block device. [[ -f /bin/ifconfig ]] && IP=/bin/ifconfig

device0="/dev/sda2" if [ ! -s /bin/ifconfig ]
if [ -b "$device0" ] then
then echo "ifconfig is not present in /bin or file size is 0 "
echo "$device0 is a block device." fi
fi
[[ -f /sbin/ifconfig ]] && IP=/sbin/ifconfig
# /dev/ttyS1 is a character device.
if [ ! -s /sbin/ifconfig ]
device1="/dev/ttyS1" then
if [ -c "$device1" ] echo "ifconfig is not present in /sbin or file size is 0 "
then exit
echo "$device1 is a character device." else
fi echo "ifconfig found in /sbin/"
fi

IP_ADDRESS=`$IP|grep -i "inet addr:"|cut -d ":" -f2|cut -d " " -


f1 |grep -v "127.0.0.1"`

echo " IP Address = $IP_ADDRESS"

46
Example : If statement and its variants

47
Example : If statement and its variants

48
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh
# $string1 has not been declared or initialized.
if [ -n $string1 ] echo "Enter 2 strings"
then read str1 str2
echo "String1 : is not null."
else
echo "String1 : is null." if [ "$str1" = "$str2" ]
fi
then
# Wrong result. echo "Both the Strings are Equal"
# Shows $string1 as not null, although it was not initialized.
else
echo echo "Strings are different"
# This time, $string1 is enclosed in double quotes fi
if [ -n "$string1" ]
then #!/bin/ksh
echo "String1 : is not null."
else
echo "OS : `uname`"
echo "String1 : is null." echo "Enter the value of str1="
fi read str1
# Correct Result

echo if [ -z $str1 ]
if [ $string1 ]
then
then
echo "String1 : is not null." echo "String Size is 0"
else else
echo "String1 : is null."
fi echo "String Size is Nonzero"
# Correct Result fi

49
Example : If statement and its variants

50
Example : If statement and its variants

51
Flow control - case
case $val in
Choice1)
Statements ………
;;
Choice2)
Statements ………
;;
[1-9][0-9])
Statements ……… // Match only 2 digit numbers
;;
[a-z] | [A-Z] )
Statements ……… // Match only a single alphabet character
;;
Choice3|Choice4)
Statements ………
;;
*)
Statements [ This is default choice ]
;;
esac

52
case : Example
#!/bin/ksh #!/usr/bin/ksh
if [ $# -ne 1 ]
echo "Enter a Number Between [ 1-4 ]" then
read NUM echo "Invalid Arguments::"
echo "Usage:: ./$0 [suomp19j/suomp20j/suomp21j/ALL]"
case $NUM in exit
1) fi
echo "ONE" clear
;; server=`echo "$1" |tr '[:lower:]' '[:upper:]'`
2)
echo "TWO" mqstatus()
;; {
3) echo ""
echo "THREE" printf "%55s\n" "MQSTATUS:$server"
;; echo "----------------------------------------------------------------------------------------------"
4) ssh -q bplapp@$server "cd
echo "FOUR" /export/home/bplapp/BPL_SCRIPTS/MQ;./AllQueueDepth"
;; }
*)
echo "$NUM is not [ 1 - 4 ]" case $server in
exit 1 'SUOMP19J'|'SUOMP20J'|'SUOMP21J')
;; mqstatus
esac ;;
'ALL')
for server in suomp19j suomp20j suomp21j
do
mqstatus
done
;;
*)
echo "Invalid Arguments::"
echo "Usage:: ./$0 [suomp19j/suomp20j/suomp21j/ALL]"
;;
esac

53
case : Example
#!/usr/bin/ksh #!/bin/ksh
# Source the Stop functions which is defined in /opt/proj/Setfunction.ksh #
if [ $# -ne 0 ]
. /opt/proj/Setfunction.ksh then
if [ $# -ne 0 ]
echo "Usage : $0"
then exit
echo "Invalid Arguments : " fi
echo "USAGE: $0"
exit
fi echo "Enter a Number"
clear read Num
printf "%-30s \n" "BPL Process Control "
echo "--------------------------------------------------------------"
echo "1) Complete Stop BPL" case $Num in
echo "0) exit" [0-9])
echo "--------------------------------------------------------------"
echo "$Num :is a One digit Number "
echo "Enter Option :\c" ;;
read opt [0-9][0-9])
case $opt in
1)
echo "$Num :is a two digit Number "
Stop_GUI8001 ;;
Stop_XML8091 [0-9][0-9][0-9])
Stop_XML8081
Stop_XML8071
echo "$Num :is a three digit Number "
Stop_gateway ;;
;; *)
0)
exit
echo "$Num :is greater than 999 or Entered value is not a
;; number "
*) ;;
echo "Invalid Option : Valid options are 0-1" esac
exit
;;
esac

54
case : Example

55
case : Example

56
while loop
while [ condition ] command| while read Variable..
do do
Statements … Statements …
done done

cat Filename| while read Variable.. One liner


do while [ cond ]; do stmt; stmt1; done
Statements …
done Skip lines while reading contents from the
previous command
while read Variable(S) command | while read var
do do
Statements … read var1
done <filename echo $var
done
while [ : ]
do
….stmt…
done

57
while loop - Example

#!/bin/ksh #!/bin/ksh
if [ $# -ne 0 ] if [ $# -ne 0 ]
then then
echo “USAGE: $0” echo “USAGE: $0”
exit exit
fi fi

echo “Enter a Number :“ echo “List of Users present in


read num /etc/passwd File :”
i=0 cat /etc/passwd |while read rec
while [ $i –le $num ] do
do User=`echo $rec|cut –d “:” –f1`
echo $i echo $User
i=$(( $i + 1 )) done
done

58
while loop - Example

59
while loop - Example

60
Break & continue- Example
#!/bin/ksh #!/bin/ksh
if [ $# -ne 0 ] if [ $# -ne 0 ]
then then
echo “USAGE: $0” echo “USAGE: $0”
exit exit
fi fi

echo “List of Users present in /etc/passwd echo “Password file entry for root :”
File :” cat /etc/passwd |while read rec
cat /etc/passwd |while read rec do
do User=`echo $rec|cut –d “:” –f1`
User=`echo $rec|cut –d “:” –f1` if [ “$User” != “root” ]
if [ “$User” = “user9” ] then
then continue
break fi
fi echo $rec
echo $User done
done
echo “---Password file entry for root---”
getent passwd root

61
while loop - Example

62
while loop - Example

63
while loop - Example
#!/bin/ksh
if [ $# -ne 0 ] #!/bin/ksh
then if [ $# -ne 0 ]
echo “USAGE: $0” then
exit echo “USAGE: $0”
fi exit
fi
echo “List of Users present in
/etc/passwd File :” cat config_file|grep –v “^#” |while read rec
while read rec do
do echo $rec
User=`echo $rec|cut –d “:” –f1` done
if [ “$User” = “user9” ]
then
break
fi
echo $User
done< /etc/passwd

64
while loop - Example

65
while loop - Example

66
while loop - Example
#!/bin/ksh #!/bin/ksh
echo “Odd Numbers from 1-50 :” echo “Even Numbers from 1-50 :”
seq 50 |while read num seq 50 |while read num
do do
echo $num read num1
read num1 echo $num1
done done

#!/bin/ksh #!/bin/ksh
echo “Series : 1 5 9 …( Range 1-50)” cat /etc/passwd |while read rec
seq 50 |while read num do
do echo $rec
echo $num read rec1
read num1 done
read num2
read num3
done

67
until loop

until [ condition ] #!/bin/ksh


do if [ $# -ne 0 ]
….Statements … then
done echo “USAGE: $0”
exit
fi

echo “Enter a Number :“


read num
i=0
until [ $i –gt $num ]
do
echo $i
i=$(( $i + 1 ))
done

68
until loop - Example

69
for loop

for var in vaue(S) C-type for loop


do for (( var=0;var<=10;var++ ))
… Statement … do
done ….Statement …
done
for var in `seq 10`
do
….Statement …
done

for var in `command`


do
….Statement …
done

70
For loop - Example
#!/usr/bin/ksh
if [ $# -ne 1 ]
then
echo "Invalid Arguments::"
echo "Usage:: ./$0 [suomp19j/suomp20j/suomp21j/ALL]"
exit
fi
clear
server=`echo "$1" |tr '[:lower:]' '[:upper:]'`
case $server in
'SUOMP19J')
ssh -q bplapp@$server "cd /export/home/bplapp/BPL_SCRIPTS;./monitor_bpl"
;;
'SUOMP20J')
ssh -q bplapp@$server "cd /export/home/bplapp/BPL_SCRIPTS;./monitor_bpl"
;;
'SUOMP21J')
ssh -q bplapp@$server "cd /export/home/bplapp/BPL_SCRIPTS;./monitor_bpl"
;;
'ALL')
for servername in suomp19j suomp20j suomp21j
do
ssh -q bplapp@$servername "cd /export/home/bplapp/BPL_SCRIPTS;./monitor_bpl"
done
;;
*)
echo "Invalid Arguments::"
echo "Usage:: ./$0 [suomp19j/suomp20j/suomp21j/ALL]"
;;
esac

71
select loop -- observe and see How it works 
#!/bin/bash #!/bin/bash

echo "***********************" echo "***********************"


select opt in Start Stop Bounce select opt in Start Stop Bounce Exit
do do
case $opt in case $opt in
Start) Start)
echo "Selected Option - Start" echo "Selected Option - Start"
;; ;;
Stop) Stop)
echo "Selected Option - Stop" echo "Selected Option - Stop"
;; ;;
Bounce) Bounce)
echo "Selected Option - Bounce" echo "Selected Option - Bounce"
;; ;;
*) Exit)
echo "Invalid Choice .. $opt" exit
exit 1 ;;
esac *)
echo "***********************" echo "Invalid Choice .. $opt"
exit 1
# Comment the Break ,If you want to come back ;;
# to main menu untill the slected option is exit esac
break echo "***********************"

done done

72
select loop Example

73
select loop Example

74
Shell Features

Command history and history expansion


Aliases
Sourcing of shell scripts
Initialization of login shells, non-login interactive shells
non-login non-interactive shells
shell expansions:
Brace expansion
~ expansion
Parameter expansions:
Arithmetic expansions
Command substitution
IFS field splitting
File/Path name expansions

75
Command history and history expansion

History is a powerful feature of a shell to track the commands


which are executed .

You can execute history command to check the last commands


which have been recently executed .

How effectively we can use history expansions ???

76
Command history and history expansion

Execute a specific command from the history using !n

If you’ve executed a command earlier, instead of re-typing it


again, you can quickly execute it by using the corresponding
number of the command in the history.

!-3 Execute the last but 2 command from the history


!! To execute the previous command
!5 To execute the command number 5 from the history

77
Command history and history expansion

78
Command history and history expansion

Execute a command with keywords using !string

79
Command history and history expansion

We can replace the string from the previous command


using ^str1^str2^

80
Few more history options

We can display timestamp using HISTTIMEFORMAT

$ export HISTTIMEFORMAT='%F %T '

We can search the history using Control+R


How to repeat the previous command ?

we can use up arrow key


Type !! and press enter
Type !-1 and press enter
Press Control+P then press enter to execute it
Some shells you can press Esc+k , then press Enter

81
Few more history options

We can ontrol the total number of lines in the history using
HISTSIZE
export HISTSIZE=2000
Default history size is 1000
To clear all the previous history execute history –c
To disable the usage of history set the HISTSIZE to 0 ( zero)

82
Alias

alias command is useful to create a 'shortcut' to a command.


You can create aliases for the frequently used commands
This is a shell built-in feature
Example of an alias :
alias tmp=“cd /tmp”
alias c=“clear”
You can execute alias or alias –p to list the aliases which are
already defined
To un-define the alias you can execute “unalais alisasname “
Example to unalias :
unalias c

83
Sourcing of Shell script

This is used when you want to execute the script in the current
shell
This feature can be used to set your environment

To source a script you can execute :


$. /PATH/OF/SCRIPT
[DOT][SPACE][SCRIPT]
OR
$source /PATH/OF/SCRIPT
Example:
$. /opt/bea/wls10/setdomainenv.sh
$source /opt/dbms/setoracle.sh
84
Sourcing of Shell script

85
Initialization of login shells, non-login interactive shells , non-login non-interactive shells

Lets discuss about : Bash shell ( bash )

An interactive login shell is started after a successful login, using /bin/login, by
reading the /etc/passwd file. This shell invocation normally reads /etc/profile and its
private equivalent ~/.bash_profile upon startup.
An interactive non-login shell is normally started at the command-line using a shell
program (e.g., [prompt]$/bin/bash) or by the /bin/su command. An interactive non-
login shell is also started with a terminal program such as xterm or konsole from
within a graphical environment. This type of shell invocation normally copies the
parent environment and then reads the user's ~/.bashrc file for additional startup
configuration instructions.
A non-interactive shell is usually present when a shell script is running. It is non-
interactive because it is processing a script and not waiting for user input between
commands. For these shell invocations, only the environment inherited from the
parent shell is used.
The file ~/.bash_logout is not used for an invocation of the shell. It is read and
executed when a user exits from an interactive login shell.

86
Shell Expansions: Brace expansion

Brace expansion is used to generate stings at the command line or in a


shell script.
The syntax for brace expansion consists of either a sequence specification
or a comma separated list of items inside "{}".
Example:
$echo {-10..10}
$echo {10..-10}
$touch {a,b,c}.txt
$ touch {a,b,c}.{c,cpp,log}
$rm {a,b,c}.{c,cpp,log}
$echo {a,b}{a,b}
To enable brace expansion execute : "set -B“
By default its on in Linux bash(login) shell
To enable brace expansion execute : "set +B“

87
Shell Expansions: Brace expansion

88
Shell Expansions: ~ expansion

This is known as tilde expansion.


The tilde expansion is used to expand to several paths :

~ Expand to the variable $HOME OR home directory of the current user


~usename Expand to the home directory of the given username
~+ Expands to the value of the PWD variable (current working directory )
~- Expands to the value of OLDPWD variable(previous working directory)
If OLDPWD is unset, ~- will not be expanded.
Example : $cd /tmp
$cd /opt
$echo $OLDPWD
$echo $PWD
$echo ~
$echo ~+
$echo ~-
$cd ~
89 $cd ~-
Shell Expansions: ~ expansion

90
IFS filed splitting

IFS is a special shell variable


We can change the value of IFS as per requirment
The Internal Field Separator (IFS) that is used for word splitting after
expansion and to split lines into words with the read built-in command
The default value of IFS is <space><tab><newline>.
If the value of IFS is a <space>, <tab>, and <newline>, or if it is
unset, any sequence of <space>s, <tab>s, or <newline>s at the
beginning or end of the input shall be ignored and any sequence of
those characters within the input shall delimit a field.

91
IFS - EXAMPLE

$ cat CONF
PROD:1521
NP:1531
$ echo $IFS

$cat Readconf.ksh
#!/bin/ksh
IFS=":"
while read DB PORT
do
echo "DB - $DB"
echo "PORT- $PORT"
done<CONF
$ ksh Readconf.ksh
DB - PROD
PORT- 1521
DB - NP
PORT- 1531
92
IFS - EXAMPLE

93
Parameter Expansion:

A parameter is an entity that stores values and is referenced by


a name, a number or a special symbol.

Parameter expansion is the procedure to get the value from


the referenced entity, like expanding a variable to print its value

The format for parameter expansion is ${expression}

94
Parameter Expansion:

Parameter expansion can be modified by using one of the following formats .

VARIABLE Set and Not Null VARIABLE Set But Null VARIABLE Unset

${VARIABLE:-VALUE} Substitute VARIABLE Substitute VALUE Substitute VALUE

${VARIABLE-VALUE} Substitute VARIABLE Substitute null Substitute VALUE

${VARIABLE:=VALUE} Substitute VARIABLE Assign VALUE Assign VALUE

${VARIABLE=VALUE} Substitute VARIABLE Substitute null Assign VALUE

${VARIABLE:? VALUE} substitute VARIABLE error, exit error, exit

${VARIABLE? VALUE} substitute VARIABLE substitute null error, exit

${VARIABLE:+VALUE} substitute VALUE substitute null substitute null

${VARIABLE+VALUE} substitute VALUE substitute VALUE substitute null

95
Parameter Expansion:

96
Arithmetic Expansion:

Shell performs arithmetic expansion while it processes an arithmetic expression and


then substitutes the result.
Shell evaluates the expression and replaces $((expression)) with the result.

Example:
$ echo $(( 10+43))
$NUM1=11
$NUM2=23
$echo $((NUM1+NUM2))
Try This : echo $[100-23+5]

97
Command substitution
command substitution :
command substitution in Unix shells allows a command to be run and its output will
be used on the command line as arguments to another command .
Also command substitution is used in shell script to capture the output of a command
and store it in a variable.

To do the command substitution we can either use :


$(command) or `command`
Example :
TODAY=$(date)
TODAY=`date`

98
Shell Expansions: File/Path expansion
Bash shell support path name expansion.
This is possible using the brace expansion and using wild cards
Example:
$ touch {a,b,c}.{c,cpp,log}
$ls –l {a,b,c}.{c,cpp,log}
$ ls *.{c,log}
$ls –l ?.c
$ls –l [ab].c
$ls –l /home/sxsama2/ {a,b,c}.{c,cpp,log}
$rm {a,b,c}.{c,cpp,log}
$ ls /etc/*.conf
Bash shell supports the following wild cards
* - Matches any string, including the null string
? - Matches any single (one) character.
[...] - Matches any one of the enclosed characters.
99
Shell Expansions: File/Path expansion

100
eval
Assume that the value of a variable is the name of a second variable.

Is it somehow possible to retrieve the value of this second variable from the first one
?

For example,
if a=letter_of_alphabet

and

letter_of_alphabet=z

can a reference to a return z ?


This can be done using eval .
This is called an indirect reference.

eval var1=\$$var2

101
eval - Example

#!/bin/ksh
Val=Var1
Var1=Qwest
echo
echo “Val = $Val"
eval Val=\$$Val
echo "New Val = $Val"

102
function

Functions are blocks of script code that you assign a name to, then reuse
anywhere in your code .
Anytime you need to use that block of code in your script, all you need to do
is use the function name you assigned it (referred to as calling the function).
There are two formats you can use to create functions in bash shell scripts.

function name {
commands
}

name() {
commands
}

103
Function - Example
#!/bin/ksh #!/bin/ksh
sum() Menu()
{ {
Sum=$(( $1 + $2 )) clear
echo “SUM OF $1 , $2 is $Sum” echo “----App Control ----”
} echo “1 – Stop”
echo “2 – Start”
## Main Script ## echo “0 – Exit”
echo “Enter 2 Numbers” echo “Enter the option :”
read num1 num2 read option
sum $num1 $num2 }
sum $1 $2 start()
sum $1 10 {
sum $2 20 echo “Starting the application”
sum 20 10 }

Note : Run the Script like : shutdown()


Script_name 45 55 {
And observe the output echo “Shutting down the application”
}
## Main Script ##
Menu
case $option in
0)
exit ;;
1)
shutdown ;;
2)
start ;;
*)echo “Invalid Choice : $option “
;;
esac

104
Function - Example

105
Function - Example

106
return

The return command allows you to specify a single integer value to define the
function exit status.
Remember to retrieve the return value as soon as the function completes.
Remember that an exit status can only be in the range of 0 to 255.
If you execute any other commands before retrieving the value of the function using
the $? variable,the return value from the function will be lost (remember, the $?
variable returns the exit status of the last executed command).

The second problem defines a limitation for using this return value technique. Since
an exit statusmust be less than 256, the result of your function must produce an
integer value less than 256.

Any values over that returns an error value.

You can’t use this return value technique if you need to return either larger integer
values or a string value.

107
Return - Example
#!/bin/ksh

Check_HawkAgent()
{
P_CNT=`ps -fu username|grep -i "hawkagent"|grep -v grep|wc -l`
return $P_CNT
}

### Main Script ###


THRESHOLD=1
Check_HawkAgent
STATUS=$?
if [ $STATUS -eq 0 ]
then
P_STAT=`echo "\033[7m Not Running \033[m"`
elif [ $STATUS -ne $THRESHOLD ]
then
P_STAT=`echo "\033[7m Process Count Did Not Match \033[m"`
else
P_STAT="Running"
fi

printf "%-35s %-25s \n" “HAWK PROCESS” “$P_STAT”

108
Return Example

109
trap
The trap command allows to specify which signals your shell script can watch for and
intercept from the shell.
If the script receives a signal listed in the trap command, it prevents it from being
processed by the shell, and instead handles it locally.

#!/bin/bash
echo “SCRIPT STARTED"
#trap "echo Don’t Press CTRL+C" SIGINT SIGTERM
trap "echo Don’t Press CTRL+C" 2 15
i=1
while [ $i -le 10 ]
do
echo “Loop Iteration: $i"
sleep 10
#i=$[ $i + 1 ]
i=$(( $i + 1 ))
done
echo “END OF SCRIPT"

110
Trap -Example

111
wait & sleep

wait:

Stop script execution until all jobs running in background


have terminated, or until the job number orprocess ID specified
as an option terminates.

sleep :

It pauses for a specified number of seconds, doing


nothing.It can be useful for timing

112
wait & sleep - Example

### Wait_Ex1.ksh ### ### Wait_Ex1.ksh ###


#!/bin/ksh #!/bin/ksh
echo "Inside ... $0" echo "Inside ... $0"
sleep 15 sleep 15
echo "Done -- $0“ echo "Done -- $0“

### Wait_Ex.ksh ### ### Wait_Ex.ksh ###


#!/bin/ksh #!/bin/ksh
echo "$0 - Started " echo "$0 - Started "
echo "Execute Wait1 .." echo "Execute Wait1 .."
ksh Wait_Ex1.ksh & ksh Wait_Ex1.ksh &
wait $! #wait $!
echo "$0 -- Done" sleep 5
echo "$0 -- Done"

113
Debugging

To set the debug mode ,Execute :

set –vx

To off the debug mode ,Execute :

set +vx

Or you can exacute the script like :

ksh –x ScriptName.ksh
bash –x ScriptName.bash

114
getopts , optargs

Getopts : Retrieve the options and values specified on the shell command
line.

$ cat getopts_ex
#!/bin/bash
# simple demonstration of the getopts command
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done

115
getopts , optargs
$ ./getopts_ex -ab test1 -c
Found the -a option
Found the -b option, with value test1
Found the -c option
$ ./getopts_ex -b "test1 test2" -a
Found the -b option, with value test1 test2
Found the -a option
$ ./getopts_ex -abtest1
Found the -a option
Found the -b option, with value test1
$ ./getopts_ex -d
Unknown option: ?
$ ./getopts_ex -acde
Found the -a option
Found the -c option
Unknown option: ?
Unknown option: ?

Note : Refer Linux Command line and Shell scripting Book for more details

116
Getopts - Example

117
File descriptors

The file descriptors for stdin, stdout, and stderr are 0, 1, and 2,
respectively .

For opening additional files, there remain descriptors 3 to 9 .

It is sometimes useful to assign one of these additional file


descriptors to stdin, stdout, or stderr as a temporary duplicate
link.

For Details Please refer advance Bash shell scripting Book

118
File descriptors - Example
#!/bin/ksh
LOGFILE=logfile.txt
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file "logfile.txt".
# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− #
# All output from commands in this block sent to file $LOGFILE.
echo −n "Logfile: "
date
echo "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−"
echo
echo "Output of \"ls −al\" command"
echo
ls −al
echo; echo
echo "Output of \"df\" command"
echo
df
# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− #
exec 1>&6 6>&− # Restore stdout and close file descriptor #6.

119
File descriptors - Example

120
Timing out

#!/bin/bash
# timing the data entry
if read -t 5 -p "Please enter your name: " name
then
echo "Hello $name, welcome to my script"
else
echo
echo "Sorry, too slow!"
fi

121
Timing out - Example

122
Execute the sql batch jobs using Shell Script.
#!/bin/ksh

DBNAME=SID
USERID=dbuser
PASSWORD=dbpwd

#Validate database ID & PASSWORD @SCHEMA

sqlplus -s /nolog <<EOF


whenever sqlerror exit failure
connect $USERID/$PASSWORD@$DBNAME
exit success
EOF
[ $? -ne 0 ] && { echo "Connection failed ."; exit 1; }

echo "Connection succeeded for user $USERID, SID $DBNAME"

sqlplus -s $USERID/$PASSWORD@$DBNAME @ ./order.sql >./Failed_orders.tmp

123
FTP Automation using HERE DOC

#!/usr/bin/ksh
Download_host=servername.domain.com
ftpuser=appuser
ftppass=password
FTP_DIR=/opt/batch/DWExtract/
GET_FILE=app.current.csv
PROCESS_DIR=/prod/DW/
ftp -n -i $Download_host <<EOF
user $ftpuser $ftppass
bin
hash
cd $FTP_DIR
lcd $PROCESS_DIR
get $GET_FILE
bye
EOF
echo "FTP :File $PROCESS_DIR/$GET_FILE downloaded from $Download_host"

124
Quoting Variables

When referencing a variable, it is generally advisable to enclose its name in


double quotes. This prevents reinterpretation of all special characters within
the quoted string.

#!/bin/ksh
Display()
{
echo $1
}
## MAIN SCRIPT ##
VAR="Use double quote while referencing a variable"
Display $VAR
Display "$VAR"

125
Quoting Variables - Example

126
$RANDOM - To generate random integer
$RANDOM returns a different random integer at each invocation.

Let if you want to shuffle a file content you can use $RANDOM

#!/bin/ksh

if [ ! -s "$1" ]
then
echo "File does not exist / Size is zero : $1"
exit
fi

TMP_FILE=/tmp/Shuf.out.tmp
>$TMP_FILE
cat "$1" |while read rec
do
echo $RANDOM:"$rec" >>$TMP_FILE
done

cat $TMP_FILE|sort -n |cut -d ":" -f2-

rm $TMP_FILE

127
$RANDOM - To generate random integer - Example

128
Thank You !!!!
Any Questions Mail me 

Subhasis.samantaray@centurylink.com
subhasis.samantaray@gmail.com

You might also like