Advance Shell Scripting Training
Advance Shell Scripting Training
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
if [ $# -ne 0 ]
then USAGE INFORMATION
echo "USAGE : $0 without any command line params" COMMAND LINE PARAM VALIDATION
fi
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
#!/bin/ksh
echo “Welcome to the word of scripting”
7
Command separator [semicolon]
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.
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.
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
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}
16
Default Values for Variables
VARIABLE Set and Not Null VARIABLE Set But Null VARIABLE Unset
17
Shell Variables
18
Exporting variables (Change or Set Environment Variable )
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
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.
readonly:
VAR=VALUE
readonly VAR
declare:
declare –r VAR=VALUE
typeset:
typeset –r VAR=VALUE
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
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 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 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
33
Compound comparisons
-a
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o
logical or
These are similar to the Bash comparison operators && and ||, used within double
brackets [[ ]].
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
[[ ${N1:=NULL} = "NULL" ]] && echo "Input cant be null " && exit
[[ $N1 -eq $N2 ]] && echo "Both numbers are same" && exit
36
Example : If statement and its variants
37
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh
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
44
Example : If statement and its variants
45
Example : If statement and its variants
#!/bin/ksh #!/bin/ksh
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
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
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
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
68
until loop - Example
69
for loop
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
done done
72
select loop Example
73
select loop Example
74
Shell Features
75
Command history and history expansion
76
Command history and history expansion
77
Command history and history expansion
78
Command history and history expansion
79
Command history and history expansion
80
Few more history options
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
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
85
Initialization of login shells, non-login interactive shells , non-login non-interactive shells
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
87
Shell Expansions: Brace expansion
88
Shell Expansions: ~ expansion
90
IFS filed splitting
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:
94
Parameter Expansion:
VARIABLE Set and Not Null VARIABLE Set But Null VARIABLE Unset
95
Parameter Expansion:
96
Arithmetic Expansion:
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.
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
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 }
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.
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
}
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:
sleep :
112
wait & sleep - Example
113
Debugging
set –vx
set +vx
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 .
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
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
#!/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
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