Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KSH, Passing more than one variable to a function

Status
Not open for further replies.

macrun95

Technical User
Dec 5, 2005
28
US
How can I pass more than one vatiable to a function? Here is the code. Thanks
SOURCEDIR=/us1/ximport
RESULTDIR=/us1/ximport/result
RPTDATETIME=$(date +%Y-%m-%d_%H.%M.%S)
TEST=NO
OPERATOR=exp
PRACFILE=111111111111111111111111111111111111111111111111111111111111
GPMSDIR=/gpms
PRACNAME='
xpc
thr'


function import_practice
{
# If no source file exists for the practice, write a norecords.txt file to the result directory and exit.
if [[ ! -f ${SOURCEDIR}/target_${PRACNAME}.txt ]]
then
echo '0 records specified' > ${RESULTDIR}/${PRACNAME}_${RPTDATETIME}_norecords.txt
exit 1
fi


# Stream the template .ini file to a copy for the practice, replacing the literals '$PRACNAME', '$SOURCEDIR'
# and $RESULTDIR in the with their corresponding variable values.
sed -e s:'$PRACNAME':${PRACNAME}:g ximport_template.ini |
sed -e s:'$TEST':${TEST}:g |
sed -e s:'$SOURCEDIR':${SOURCEDIR}:g |
sed -e s:'$RPTDATETIME':${RPTDATETIME}:g |
sed -e s:'$RESULTDIR':${RESULTDIR}:g > ${SOURCEDIR}/ximport_${PRACNAME}.ini

# Run Ximport for the practice.
#${GPMSDIR}/ximport ${SOURCEDIR}/ximport_${PRACNAME}.ini

#Delete the source file and .ini file.
#rm ${SOURCEDIR}/target_${PRACNAME}.txt
#rm ${SOURCEDIR}/ximport_${PRACNAME}.ini

#Spool the result files to GPMS printer management.
${GPMSDIR}/mkspl -sc -n260 -p${PRACNAME} -t 'ximport Err Rpt' ${RESULTDIR}/${PRACNAME}_${RPTDATETIME}_errors.txt
${GPMSDIR}/mkspl -sc -n260 -p${PRACNAME} -t 'ximport Warn Rpt' ${RESULTDIR}/${PRACNAME}_${RPTDATETIME}_warnings.txt
${GPMSDIR}/mkspl -sc -n260 -p${PRACNAME} -t 'ximport Stat Rpt' ${RESULTDIR}/${PRACNAME}_${RPTDATETIME}_status.txt
${GPMSDIR}/mkspl -sc -n260 -p${PRACNAME} -t 'ximport Bad Rpt' ${RESULTDIR}/${PRACNAME}_${RPTDATETIME}_bad.txt
}

for practice in $PRACNAME
do
import_practice "$practice"
done
 
You pass a variable as parameter to the function import_practice but you never use it. You work with the variable you defined outside the function.
A variable in a function is referenced like a positional parameter. Passing more than 1 variable is straight forward, the second variable is referenced like the second positional parameter ( $2 ).
This is how your function looks like if it uses the parameter you pass to it:
Code:
function import_practice
{
# If no source file exists for the practice, write a norecords.txt file to the result directory and exit.
if [[ ! -f ${SOURCEDIR}/target_[COLOR=#EF2929]${1}[/color].txt ]]
then
echo '0 records specified' > ${RESULTDIR}/${1}_${RPTDATETIME}_norecords.txt
exit 1
fi


# Stream the template .ini file to a copy for the practice, replacing the literals '$PRACNAME', '$SOURCEDIR'
# and $RESULTDIR in the with their corresponding variable values.
sed -e s:'[COLOR=#EF2929]$1[/color]':[COLOR=#EF2929]${1}[/color]:g ximport_template.ini |
sed -e s:'$TEST':${TEST}:g |
sed -e s:'$SOURCEDIR':${SOURCEDIR}:g |
sed -e s:'$RPTDATETIME':${RPTDATETIME}:g |
sed -e s:'$RESULTDIR':${RESULTDIR}:g > ${SOURCEDIR}/ximport_[COLOR=#EF2929]${1}[/color].ini

# Run Ximport for the practice.
#${GPMSDIR}/ximport ${SOURCEDIR}/ximport_[COLOR=#EF2929]${1}[/color].ini

#Delete the source file and .ini file.
#rm ${SOURCEDIR}/target_[COLOR=#EF2929]${1}[/color].txt
#rm ${SOURCEDIR}/ximport_[COLOR=#EF2929]${1}[/color].ini

#Spool the result files to GPMS printer management.
${GPMSDIR}/mkspl -sc -n260 -p${1} -t 'ximport Err Rpt' ${RESULTDIR}/[COLOR=#EF2929]${1}[/color]_${RPTDATETIME}_errors.txt
${GPMSDIR}/mkspl -sc -n260 -p${1} -t 'ximport Warn Rpt' ${RESULTDIR}/[COLOR=#EF2929]${1}[/color]_${RPTDATETIME}_warnings.txt
${GPMSDIR}/mkspl -sc -n260 -p${1} -t 'ximport Stat Rpt' ${RESULTDIR}/[COLOR=#EF2929]${1}[/color]_${RPTDATETIME}_status.txt
${GPMSDIR}/mkspl -sc -n260 -p${1} -t 'ximport Bad Rpt' ${RESULTDIR}/[COLOR=#EF2929]${1}[/color]_${RPTDATETIME}_bad.txt
}
 
Thanks, I see what I was doing wrong and that works for the first positional parameter but it won't pass the second one.
 
Why do you think it will not pass the second one? Do you get any errormessage or wrong results?
Code:
$ cat fnc.sh
function fnc
{
   echo "This is the value of the first parameter: $1"
   echo "This is the value of the second parameter: $2"
}

fnc "one" "two"
$
$ ./fnc.sh
This is the value of the first parameter: one
This is the value of the second parameter: two
$
 
I found the problem. It is the 'exit 1' in the function. It would create the first file and then exit out of the loop and not create the second file. I removed it and now it's working properly. Thanks for your help.
 
The exit command terminates the subshell your script is runnin in. If you need the return code in the loop use the return command instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top