Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Function from Profile in Shell Script

Status
Not open for further replies.

manrique83

Programmer
Apr 26, 2005
36
US
I have a function "fnd" in my .profile file. I have shell script check.sh calling function "fnd" but it doesn't recognize the file unless I load the .profile file. I don't have to do this when referencing variables in my .profile file but can't get it to work without loading the file for my function. Is there a character I need to use to call a function similar to when I call variables ($)?

.profile
function fnd {
if [[ $# = 2 ]]; then
find $1 -type f -exec grep -l "$2" {} \;
fi
}
export env=system

check.sh
#!/bin/ksh -f
echo $env
fnd . function

$ ./check.sh
system
./check.sh[20]: fnd: not found
 
I'd try export -f fnd in your .profile
Anyway, have a look at the ENV special variable.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try "[tt]set -ah[/tt]" right before the function definition. "[tt]man set[/tt]" for more information.


 
Thanks for your replies PHV and SamBones.

I tried variations of your recommendations and it still won't work. Again, I can easily avoid this by loading the file containing the function, but we are trying to avoid this. Any help is appreciated.
 
Again, use a $ENV file ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I second PHVs suggestion. Also, you can put your functions in a separate script, say [tt]shell_functions.ksh[/tt], then just source it in the scripts you want to use them in.

Code:
function fnd {
if [[ $# = 2 ]]; then
find $1 -type f -exec grep -l "$2" {} \;
fi
}

Code:
#!/bin/ksh

.  /path/to/shell_functions.ksh

fnd . function

This will still work when you're using cron to launch it too.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top