Tuesday, January 1, 2013

Dereferenced Passing Parameter Mechanism in Shell Script Function

The below code snippet shows how to pass the parameters to the function in shell script. If we used $1,$2,,,$n with in the function
$1 says first argument of this function
$2 says second argument of this function
.
.
.
$n says nTh argument of this function.


#!/bin/bash
add()
{
 x=$1;
 y=$2;
 z=`expr $x + $y`;
 echo $z
}
sum=`add 10 20`
echo "sum x=10 y=20 x+y=$sum"


OUTPUT:

-bash-3.2$ sh passing_parameter_mech
sum x=10 y=20 x+y=30

sum=`add 10 20`
   calling the function add with passed two parameters 10 and 20 and return value is assigned into variable sum here need to put it both side of function call `(tilde) sign.
x=$1 
  values of 10 is assigned into the variable x
y=$2
  values of 20 is assigned into the variable y

we can call x and y as dereferenced variables in shell function.

No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...