INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

KSH, Passing more than one variable to a function

KSH, Passing more than one variable to a function

(OP)
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

RE: KSH, Passing more than one variable to a function

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_${1}.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:'$1':${1}: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_${1}.ini

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

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

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

RE: KSH, Passing more than one variable to a function

(OP)
Thanks, I see what I was doing wrong and that works for the first positional parameter but it won't pass the second one.

RE: KSH, Passing more than one variable to a function

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
$ 

RE: KSH, Passing more than one variable to a function

(OP)
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.

RE: KSH, Passing more than one variable to a function

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close