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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subroutines in Korn scripts 1

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
How do I use subroutines in a Korn shell script? In particular can I send parameters to a subroutine?

If I have a script like this:

sub1()
{
echo this is in sub1
}

sub2(x)
{
echo this is in sub2
echo $x
}

echo script starts now
sub1
sub2(par)
echo script end

Then the sub1 routine works OK but my guess at sending a parameter (in sub2) doesn't work. In fact it seems that any way I try to call a subroutine with a parameter (or even empty brackets) fails.

Is this possible?

Thanks in advance as usual
 
This is how I'd do it ( live ( old) example of functions )

#!/bin/ksh
BATCH_PROFILES=/u/profiles/batch_users
#
SLEEP_FOR=3

function check_batch_number # One parameter - batch no.
# returns 01-99 if valid, 00 if invalid
{
return $( awk -v b="${1}" '
BEGIN {
b1=int(b+0)
if(b1!=b || (b <1 || b > 99)) b=0
printf(&quot;%02d&quot;,b)
} ' )
}

function batch_files_exist #One parameter - batch no.
{
ALL_OK=1

for B_FILE in tsmn18 tsix18 tsmn19 tsrp02 tsrp10
do
if [ ! -f ${B_FILE}.${1} ]
then
ALL_OK=0
echo &quot;Batch file &quot;${B_FILE}.${1}&quot; not found&quot;
fi
done
if [ $ALL_OK -eq 0 ]
then
sleep ${SLEEP_FOR}
fi

return ${ALL_OK}
} # batch_files_exist


# Get the batch number first
AUTORUN=&quot;&quot;
while [ &quot;${AUTORUN}&quot; = &quot;&quot; ]
do
if [ &quot;${TOBPP_MODE}&quot; = &quot;NORMAL&quot; ]
then
clear
echo &quot;\n\n&quot;
echo &quot; AUTOMATED BATCH PRINTING&quot;
echo &quot; ------------------------&quot;
echo &quot;\n&quot;
echo &quot;BATCH NO. [1-99,Q] \c&quot;
read TOBPP_REPLY
fi

case ${TOBPP_REPLY} in
*[Qq]*)
AUTORUN=&quot;N&quot;
;;
*)
check_batch_number $TOBPP_REPLY
let TOBPP_BATCH_NUMBER=$?

if [ &quot;${TOBPP_BATCH_NUMBER}&quot; -eq &quot;00&quot; ]
then
echo &quot;\n\n\nINVALID BATCH NUMBER\n\n&quot;
sleep ${SLEEP_FOR}
else
# Check to see if batch files exist

batch_files_exist ${TOBPP_BATCH_NUMBER}

if [ $? -eq 1 ]
then
AUTORUN=&quot;Y&quot;
fi
fi
;;
esac
if [ &quot;${TOBPP_MODE}&quot; = &quot;NOINPUT&quot; -a &quot;${AUTORUN}&quot; != &quot;Y&quot; ]
then
AUTORUN=&quot;N&quot;
fi
done etc etc etc ................


Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top