LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   How to see function code? (https://www.linuxquestions.org/questions/linux-enterprise-47/how-to-see-function-code-936766/)

saeedamer 03-27-2012 06:10 PM

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)?

cgtueno 03-27-2012 08:40 PM

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.

saeedamer 03-28-2012 06:53 AM

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.

eSelix 03-28-2012 07:15 AM

In bash you can print content of function like this:
Code:

declare -f home

cgtueno 03-28-2012 07:16 AM

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.

saeedamer 03-28-2012 07:31 AM

Quote:

Originally Posted by cgtueno (Post 4638652)
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

saeedamer 03-28-2012 07:42 AM

Quote:

Originally Posted by eSelix (Post 4638650)
In bash you can print content of function like this:
Code:

declare -f home

Hi

I get this error:

Code:

ksh: declare:  not found.

cgtueno 03-28-2012 07:44 AM

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.

saeedamer 03-28-2012 08:48 AM

Thanks everyone for your time and effort.

Quote:

Originally Posted by cgtueno (Post 4638683)
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

catkin 03-28-2012 09:56 AM

Have you tried the set command?

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

saeedamer 03-28-2012 10:38 AM

Thank you for your response -

Quote:

Originally Posted by catkin (Post 4638795)
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.

catkin 03-28-2012 11:07 AM

Quote:

Originally Posted by saeedamer (Post 4638836)
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 ?

colucix 03-28-2012 11:07 AM

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

instrumentpilot 03-28-2012 11:55 AM

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

saeedamer 03-28-2012 12:17 PM

Quote:

Originally Posted by catkin (Post 4638865)
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".


All times are GMT -5. The time now is 01:09 PM.