How to see function code?
Hi there
Suppose I write a function in a shell script like this: Code:
home(){ Now is it possible for me later to see what is the function code (contents of function code)? |
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. |
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(){ Hope my question is clear now. |
In bash you can print content of function like this:
Code:
declare -f home |
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. |
Quote:
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 |
Quote:
I get this error: Code:
ksh: declare: not found. |
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. |
Thanks everyone for your time and effort.
Quote:
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 Code:
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1 Code:
DEV12() { Is there a command in unix that will display the source code - like: Code:
$ some_cmd DEV12 Thanks |
Have you tried the set command?
EDIT: just like that, with no options or arguments. |
Thank you for your response -
Quote:
Code:
_=home Code:
export ORACLE_HOME=/su01/app/oracle/product/10.2.0/db_1 |
Quote:
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 ? |
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 Code:
export -f function_name |
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 |
Quote:
Code:
$ chmod 777 env.sh Quote:
Code:
$ type home |
All times are GMT -5. The time now is 01:09 PM. |