shell scripting: calling subroutines | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

shell scripting: calling subroutines

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-21-2001
98_1LE 98_1LE is offline Forum Advisor  
Registered User
 
Join Date: Dec 2000
Last Activity: 21 May 2012, 6:37 PM EDT
Location: Greater Dallas area
Posts: 405
Thanks: 0
Thanked 3 Times in 3 Posts
Computer

How do I define and call a subroutine in a ksh script (or any shell)? I'm writing a script that uses a lot of the same code over and over and I don't want to waste time/space.
Thanks,
Chuck
Sponsored Links
    #2  
Old 02-22-2001
mib mib is offline
Registered User
 
Join Date: Jan 2001
Last Activity: 8 July 2003, 3:54 AM EDT
Location: Calicut
Posts: 228
Thanks: 0
Thanked 7 Times in 4 Posts
in bash shell it is something like this....

function function-name {
shell commands...
}

or just

function-name () {
shell commands...
}

#!/bin/bash

test ()
{
echo Hey I am here.
echo Now exiting function.
}

# Note: function must precede call.

# Now, call the function.

test

exit 0
------------------------------
here is another one which process passing arguments...

#!/bin/bash

func2() {
if [ -z $1 ]
# Checks if any params.
then
echo "No parameters passed to function."
return 0
else
echo "Param #1 is $1."
fi

if [ $2 ]
then
echo "Parameter #2 is $2."
fi
}

func2
# Called with no params
echo

func2 first
# Called with one param
echo

func2 first second
# Called with two params
echo

exit 0
----------------------

another important thing used in functions is return statement, which optionally takes an integer argument, which is returned to the calling script as the "exit status" of the function, and this exit status is assigned to the variable $?


I think korn shell also using same method. I am not sure.




[Edited by mib on 02-22-2001 at 02:24 AM]
The Following User Says Thank You to mib For This Useful Post:
Bill L. (02-14-2011)
Sponsored Links
    #3  
Old 02-22-2001
98_1LE 98_1LE is offline Forum Advisor  
Registered User
 
Join Date: Dec 2000
Last Activity: 21 May 2012, 6:37 PM EDT
Location: Greater Dallas area
Posts: 405
Thanks: 0
Thanked 3 Times in 3 Posts
Thank you very much Worked like a charm!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Scripting: Calling Another Function rymnd_12345 Shell Programming and Scripting 1 02-02-2012 05:40 AM
Calling webservice in shell scripting HemaV UNIX for Advanced & Expert Users 2 04-14-2011 12:52 PM
calling 'n' number of shell scripts based on dependency in one shell script. shashi369 Shell Programming and Scripting 1 11-03-2009 04:31 PM
reading fixed length flat file and calling java code using shell scripting willywilly Shell Programming and Scripting 3 08-04-2009 08:02 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM



All times are GMT -4. The time now is 09:09 AM.