Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function?

calc_pref_avail $database $service

Best regards,
Vishal

Hello Vishal,

Could you please be more specific in your requirement and let us know the input and expected output for same please.

Thanks,
R. Singh

Hi Ravinder,

Below is the code in which there is a function call calc_pref_avail at last:

for service in `$ORACLE_HOME/bin/srvctl status service -d $database | awk '{print $2}'`
do
c="/product/10"
case $ORACLE_HOME in
  */product/10*)
Instance_pref=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*: \)\(.*\)\( AVAIL:.*\)/\2/1;1q'`
Instance_avail=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*AVAIL: \)\(.*\)/\2/g;1q'`;;
*)
calc_pref_avail;;

I need to pass $database and $ service as an argument to this function and suppose below is the way I define my function

calc_pref_avail   $database $service
{

-- code --
return inst_pref;

}

Please help me how to pass an argument to function call and while defining function how to return value and get that variable in the code from where function is called

Best regards,
Vishal

Not knowing what shell you use, this is an excerpt from man bash :

So define it like

calc_pref_avail ()
{
-- code  using $1 and $2  --
return inst_pref;  
}

and use it like

calc_pref_avail   $database $service
1 Like

Hello,

Here is the example for a function which will be called by arguments.

check_file()
{
for y in $*
do
if [[ -f $y ]]
then
rm $y
fi
done
}
### calling function as follows with arguments
check_file ${FILE1} ${FILE2} 

Hope this may help.

Thanks,
R. Singh

1 Like

I need to get two values from this function as below:

    if [ "${line:0:21}" = 'Preferred instances: ' ]; then
      pref=($(echo "${line:21}"|sed 's/,/\n/g'));
    else
      avail=($(echo "${line:21}"|sed 's/,/\n/g'));
    fi

I need values of $pref and $avail from this function.Is there a way to combine these values and return as a 1 value since I understand that a function can return a single value .Thereafter after returning these values I can again get the 2 values in the code from where I called this function

Best regards,
Vishal

echo the two values and assign to / read into caller's variables.

function A () { echo ABC CDE; }
A| { read C D; echo $D: $C; }
E=$(A)

And as an addendum to RudiC's post be aware of this:-

When is a _function_ not a _function_? | Unix Linux Forums | UNIX for Advanced & Expert Users

Note: There are no braces...