Unable to run function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to run function
# 8  
Old 08-30-2012
Try this...

if you want to use the functions in your another script please use below code in your script..

Code:
source /PATH/Function_script_name

#For using function give function name

and if you want to use the functions in bash just use..

Code:
. /PATH/Function_script_name

#For using function give function name

# 9  
Old 08-30-2012
It should work just by adding the directory in which the function is to your $FPATH variable. It works in my other environment (Solaris)... And I can just run the function from within a script without having to source it, just not from the command line.
# 10  
Old 08-30-2012
The FPATH variable works in ksh (and only ksh - bash doesn't have it!) provided that:

1.) The files where the functions reside in are mode 4 or 6 to the UID of the calling script. I flag them 644 and give ownership to root:system (on AIX - Linux would be "root:root") usually. They should NOT have the x-bit set! This is important.

2.) Every function should reside in its own file (do NOT bundle functions into files) and the file should be named like the fuction itself. The first executable line in the file should be the function definition:

Code:
$ echo $FPATH
/path/foo:/path/bar

$ ls -l /path/foo/f_foobar
total 112
-rw-r--r--    1 root     system         154 Aug 30 12:04 f_foobar

$ cat /path/foo/f_foobar
# some comment about f_foobar()

f_foobar ()
{
   <here goes your code>
   return 0
}
# EOF f_foobar

3.) You have to be aware, that FUNCTIONS ARE NOT SCRIPTS! You can't just execute them on their own. You have to call them from within your scripts:

Code:
#! /bin/ksh

FPATH=/path/foo:/path/bar ; export FPATH

f_foobar arg1 arg2 arg3 ...

exit 0

I work with a collection of these functions since years and maintain my handy little library with all sorts of solutions for day-to-day-scripting.

I hope this helps.

bakunin

Last edited by bakunin; 08-30-2012 at 08:44 AM..
# 11  
Old 08-30-2012
And directories in FPATH should be in PATH.
# 12  
Old 08-30-2012
Thanks for the comment Bakunin.

As I said, I can run the functions from within a script without a problem provided that I set the FPATH with the correct directory.

However, I can't run the function from the command line using 'ksh functionname', even though I have the same setup on another system running solaris instead of red hat which works fine Smilie

When you test your functions, do you always create a separate script to call that function?
# 13  
Old 08-30-2012
Quote:
Originally Posted by Subbeh
Thanks for the comment Bakunin.

As I said, I can run the functions from within a script without a problem provided that I set the FPATH with the correct directory.

However, I can't run the function from the command line using 'ksh functionname', even though I have the same setup on another system running solaris instead of red hat which works fine Smilie
Have you tried to run with only 'functionname'??
# 14  
Old 08-30-2012
Quote:
Originally Posted by pamu
Have you tried to run with only 'functionname'??
Hmm I feel kinda stupid for not trying that before. But when I switch to a ksh shell (instead of default bash) and I run just the function name, it works.
I don't understand why is doesn't work when I run it from within the bash shell using 'ksh functionname'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Unable to capture value from function

Hi Experts, Am writing a code which need to check for the previous day date and pickup the file as per the previous day date. Problem: Why variable "YDATE" is empty ? O/S: RHEL 5.6 Shell: BASH Desired O/P: ls -lrt /opt/test/user/atsuser.NHU/out/demon.08272017 When I checked the... (3 Replies)
Discussion started by: pradeep84in
3 Replies

2. Programming

Unable to run java in redhat os

I am unable to run java from jdk. it says "cannot execute binary file" I downloaded the jdk again freshly but the problem still persists. All files have execution permission. Both OS and JDK are 64bit. Please help me out. $ pwd /home/XXXXX/apache-tomcat-6.0.18/jdk1.5.0_14/bin $... (2 Replies)
Discussion started by: meetsriharsha
2 Replies

3. Shell Programming and Scripting

Unable to run command after ssh

Hello, I am trying to create a ksh script to login to server and collect gather output of some command to troubleshoot some issue. DATE=`date +%b.%d.%Y.%M.%H` echo " Enter emp id to login to server" read Eid Eid=$Eid echo " Enter hostname of the system" read HOST HOST=$HOST... (2 Replies)
Discussion started by: saurabh84g
2 Replies

4. Programming

unable to use a function to crate a joinable thread

In my program, threads may be created when some events trigger. So I can't create threads on initialization. Theremore,I write a createThread() function to create thread. However, it is blocking at first call and don't run anymore? why? #include <pthread.h> #include <stdio.h> #include... (4 Replies)
Discussion started by: sehang
4 Replies

5. HP-UX

Unable to run some commands in HP-UX

Hi All, I want to get %cpu and %memory utilization for a given process id in HP-UX so am using the following commands 1)TOP -p <PID> am getting error message like Quitting top: pset 26323 doesn't exist,but when am using only TOP command without any options its working fine. 2)ps -e -o pcpu... (5 Replies)
Discussion started by: Ramya_Nm
5 Replies

6. AIX

unable to run at job

Hi All, I m not able to run at job with normal user on AIX system os version is 5300-05-06. I am able to run at job only with root user. When I try to run at job with any other user I am getting error: at: you are not authorized to use at. Sorry. I checked at.deny file, it is... (4 Replies)
Discussion started by: pkatkade
4 Replies

7. Shell Programming and Scripting

unable to return a decimal value from a function

Hi Guys, I am unable to return a decimal value from a function to the main script. I am getting the correct value in the function but when it is returning to the main script using "return" it is coming as 0. Below is my code. read VALUE_V fun_FATHERID fun_SONID fun_TOPID fun_RTYPE <... (2 Replies)
Discussion started by: mac4rfree
2 Replies

8. UNIX for Advanced & Expert Users

Unable to execute a function using trap

I have a script A which calls script B. I wrote a function in script A to be executed when Kill command is issued for script A and I invoke that function using the trap command.The function identifies all child process running under script A (in this case script B) and kills the child process and... (3 Replies)
Discussion started by: smohandass
3 Replies

9. Solaris

Unable to run xclock

Hello. I am trying to run xclock on newly built solaris box - These are the steps I followed: # DISPLAY=localhost:0.0 # export DISPLAY # xclock xclock: not found # cd /usr/openwin/bin # ./xclock Error: Can't open display: localhost:0.0 # Please suggest, what am i doing wrong? Thank... (27 Replies)
Discussion started by: panchpan
27 Replies

10. Shell Programming and Scripting

unable to run a script

thi is (10 Replies)
Discussion started by: angelina
10 Replies
Login or Register to Ask a Question