Linux - EnterpriseThis forum is for all items relating to using Linux in the Enterprise.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(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
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)
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.
$ 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
$
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:
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.