Saturday, July 21, 2012

Tutorial on Parameter Substitution at Runtime


Tutorial on Parameter Substitution at Runtime

Today, we are presenting a very simple & useful thing i.e. Parameter Substitution in Unix at run-time.We must know that mainly there are four types of parameter substitutions which are commonly used in Unix Scripts.The Bourne shell has a handy set of operators for testing and setting shell variables.These are presented below with some quick examples.This will run in almost all the shells but as we all know exceptions are always there , You can use parameter substitution operators in any command line. You’ll see them used with the colon ( : ) operator , checking or setting default valuesLet us know see what are the types of parameter substations
1.${parameter:-value}
This Type of parameter substitution is used if the value is defined already for the parameter,So now it will use that value otherwise use the above one.Means if Variable is undefined or assigned a null string it must get the default value :For Example lets have a scenario where we have ${EDITOR:-/bin/vi}
In this case it will use the default EDITOR value if it defined otherwise it will use vi.We can use this feature in a program which prompts for a file name and use default value if the user simply press enter
echo “Enter file Name : \c”
read filename
f_name=${filename:-emp.lst}
2.${parameter:+ value}
This one will substitute the value if parameter is defined otherwise it will substitute nothing. (just opposite of – option).It simply override the value of parameter, if parameter is null, it will never override.
OPT_MODE=P
echo “Mode : ${ OPT_MODE:+T}”
output : Mode : T
As it was defined it will override its value.This feature can be used to set a variable to the output of a command and echo a message if the variable is no null.
found_file=`ls *log`
echo ${found:+”There are log”}
3.${parameter:=value}
It works similarly for (- option) except that it goes further and makes the assignment to the variable that is evaluated.With = option , we can use parameter substitution with a command without making intermediate assignment.
echo “enter file name : \c”
read filename
grep $pattern ${filename:=emp.lst} # filename is now assigned
X=1; while [ $x –le 10 ] can be combined with while [ ${x:=1} –le 10 ]
4.${parameter : ?value}
If the parameter is not assigned and null , it echoes value and kill the shell.This is quite useful in terminating script if user fails to respond properly to shell directive.
echo “Enter the filename: \c”
read filename
grep <pattern> ${filename:? “No file entered”}
If no file name is entered then message will be displayed and script is also aborted the use of explicit exit command.
NOTE : If you omit the colon ( : ) from the expressions , the shell doesn’t check for an empty parameter. In other words, the substitution will happen whenever the parameter is set. (That’s how some early Bourne shells work: they don’t understand a colon in parameter substitution.).The first substitution ( ${Value=default} ) will leave $Value empty because the variable has been set. The second substitution will set $Value to default because the variable has been set but is empty. The third substitution will leave $Value2set to stuff :
+Value=
Value2=stuff
: ${Value=default}
: ${Value:=default}
: ${Value2:=default}

No comments:

Post a Comment