LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Password
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.

Notices


Reply
  Search this Thread
Old 03-27-2012, 06:10 PM   #1
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Rep: Reputation: 0
How to see function code?


Hi there

Suppose I write a function in a shell script like this:

Code:
home(){
  cd $ORACLE_HOME
}

Now is it possible for me later to see what is the function code (contents of function code)?
 
Old 03-27-2012, 08:40 PM   #2
cgtueno
Member
 
Registered: Jul 2003
Posts: 363

Rep: Reputation: 50
yes

(1)
The exit status of a shell script/command
can be used to return a value to the calling
entity

eg.

ls filex
echo $?
(in this case returns 0 or 1 depending on success or failure)

In a shell script you can assign a return value to a variable:
call_script_function
return_cd = $?

and then refer to it as $return_cd
(eg. echo $return_cd )

(2)
You can end the processing within a function with
return <value>

#!/bin/sh

## example function
func_1()
{
if <condition>
return 1
else
return 2
fi
}

## example main body that calls example function
func_1
if [ "$?" -eq "1" ]; then
echo "condition 1"
elif [ "$?" -eq "2" ]; then
echo "condition 2"
else
echo "unknown return value"
fi


Hope that assists

C.

Last edited by cgtueno; 03-27-2012 at 08:40 PM. Reason: typo
 
Old 03-28-2012, 06:53 AM   #3
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Thank you for your time but this is not what I am looking for.

I want to see the code of the function - not the return code/value.

For example, if the function "home" was defined like this [by someone else and lets assume that I don't know how it was defined]:

Code:
home(){
  cd $ORACLE_HOME
}
Then how can I check the code of this function? I want to know what is inside the "{" and "}" of the function?

Hope my question is clear now.
 
Old 03-28-2012, 07:15 AM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
In bash you can print content of function like this:
Code:
declare -f home
 
Old 03-28-2012, 07:16 AM   #5
cgtueno
Member
 
Registered: Jul 2003
Posts: 363

Rep: Reputation: 50
Nods

interpreting:

If you look at a script that makes a call to a function,
you want to be able to see the "source code/script" that
makes up that function (given that the function is not
defined locally in the script file that calls the function)

Correct ?

eg. If you have a script that looks like say

#!/bin/sh
.
## function xyz is not defined in this file
##
.
echo "calling function xyz"
xyz
.
.
<end-of-file>

You want to know how to track down the code behind the function 'xyz'
(eg the function xyz is defined in a file in /home/userx which is
specified in the shell's environment search path so it can find it;
or it's an executable in /bin or /etc and you want to locate the source)

Correct ?

(sorry not meaning to sound dense - just a bit confused by your explaination)

C.
 
Old 03-28-2012, 07:31 AM   #6
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by cgtueno View Post
Nods

interpreting:

If you look at a script that makes a call to a function,
you want to be able to see the "source code/script" that
makes up that function (given that the function is not
defined locally in the script file that calls the function)

Correct ?
C.
Yes! I want to know/see the "source code/script" of the function.

In our unix env, there are many functions that are used on daily bases but I don't exactly know what they do. So I want to look at the source code. I believe these functions are defined in some ."sh" file which is executed automatically when user logs in.

Regards

Last edited by saeedamer; 03-28-2012 at 07:43 AM.
 
Old 03-28-2012, 07:42 AM   #7
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by eSelix View Post
In bash you can print content of function like this:
Code:
declare -f home
Hi

I get this error:

Code:
ksh: declare:  not found.
 
Old 03-28-2012, 07:44 AM   #8
cgtueno
Member
 
Registered: Jul 2003
Posts: 363

Rep: Reputation: 50
Excellent !

$ env
to dump the shell's environment variables
one of which is

tada !

PATH=/usr:/bin/:usr/local/bin:.

when you execute a command, the shell searches the local directory in which you execute the command, and then searches PATH for the command

The command can be a script file, or executable.

Is that the holy grail for which you seek ?

C.

Last edited by cgtueno; 03-28-2012 at 07:47 AM. Reason: typo - was terribly excited - communication is actually taking place
 
Old 03-28-2012, 08:48 AM   #9
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks everyone for your time and effort.

Quote:
Originally Posted by cgtueno View Post
Excellent !

$ env
to dump the shell's environment variables
one of which is

tada !

PATH=/usr:/bin/:usr/local/bin:.

when you execute a command, the shell searches the local directory in which you execute the command, and then searches PATH for the command

The command can be a script file, or executable.

Is that the holy grail for which you seek ?

C.
No this is not the "holy grail for which I seek?" :-)

hmm... looks like I have failed to described my question well :-(


OK let me try once again. When I login to my server, I use many functions that I have not defined in my env but they are there. These are not unix-built-in functions for sure. I use some of these functions to set my env when I intend to work with a different database. So, for example, if I execute following command (which is a function defined somewhere), all relevant env variables are set to required values automatically:

Code:
$# both following commands display nothing because they are not set yet
$ echo $ORACLE_HOME
$ echo $ORACLE_SID

$# when I run this, both the above variables are set to appropriate values
$ DEV12
$ echo $ORACLE_HOME
/u01/app/oracle/product/10.2.0/db_1
$ echo $ORACLE_SID
DEV12
$
So I guess the function "DEV12" does this:

Code:
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
export ORACLE_SID=DEV12
And it has been defined somewhere like this perhaps:

Code:
DEV12() {
   export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
   export ORACLE_SID=DEV12
}
So now my question is, is there a way to know the source code of this function?

Is there a command in unix that will display the source code - like:

Code:
$ some_cmd DEV12
DEV12() {
   export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
   export ORACLE_SID=DEV12
}
I hope I have made my question clear this time.

Thanks

Last edited by saeedamer; 03-28-2012 at 08:49 AM.
 
Old 03-28-2012, 09:56 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Have you tried the set command?

EDIT: just like that, with no options or arguments.

Last edited by catkin; 03-28-2012 at 09:59 AM.
 
Old 03-28-2012, 10:38 AM   #11
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Thank you for your response -

Quote:
Originally Posted by catkin View Post
Have you tried the set command?

EDIT: just like that, with no options or arguments.
I just tried this after reading your response. To test, I defined a function [named it "home"] in a .sh file, executed the .sh and then called the function. And ehwn I issued the "set" command, the only thing that appeared in the output related the function I defined is below:

Code:
_=home
The output does not show the source code of the function which is:

Code:
export ORACLE_HOME=/su01/app/oracle/product/10.2.0/db_1
echo $ORACLE_HOME
So "set" did not do the job.
 
Old 03-28-2012, 11:07 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by saeedamer View Post
To test, I defined a function [named it "home"] in a .sh file, executed the .sh and then called the function. And ehwn I issued the "set" command, the only thing that appeared in the output related the function I defined is below ...
When you say you "executed" the .sh file, did you "source" it? If not, it cannot change the calling shell.

When you wrote "In our unix env, there are many functions that are used on daily bases but I don't exactly know what they do. So I want to look at the source code. I believe these functions are defined in some ."sh" file which is executed automatically when user logs in", how do you know about these functions?

What is the output of type DEV12 ?
 
Old 03-28-2012, 11:07 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If functions are set in the environment, you should see them using the set command, as suggested by catkin above. In alternative you can try
Code:
type DEV12
If you don't see any relevant output, maybe the code you're looking for hasn't been defined as an environment shell function (please notice that functions must be exported using
Code:
export -f function_name
in order to be available to the users' environment). Which system and shell is this, anyway

Last edited by colucix; 03-28-2012 at 11:09 AM.
 
Old 03-28-2012, 11:55 AM   #14
instrumentpilot
Member
 
Registered: May 2006
Posts: 34

Rep: Reputation: 2
saeedamer, I'm guessing your DEV12 is an alias. If you type alias on the command line then you should see it printed in the output.

Let us know,
Michael Cunningham
 
Old 03-28-2012, 12:17 PM   #15
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
When you say you "executed" the .sh file, did you "source" it? If not, it cannot change the calling shell.

What is the output of type DEV12 ?
I am not sure what you mean by "did you source it". I did this:

Code:
$ chmod 777 env.sh
$ . ./env.sh
env.sh contains my function called "home".

Quote:
What is the output of type DEV12 ?
Code:
$ type home
home is a function.
$
I did not "export -f home".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Specify address for a function in Relocatable Code raghu2383 Programming 20 08-05-2008 09:14 PM
function call in kernel code vishalbutte Programming 1 02-15-2006 01:32 PM
source code for read() function ? Mike Davies Linux - Software 1 11-03-2004 02:10 PM
error function in c code shams Programming 3 08-06-2004 04:00 AM
C code for killall function Linh Programming 3 08-01-2003 11:34 PM

LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration