Storing a Function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Storing a Function
# 1  
Old 10-21-2008
CPU & Memory Storing a Function

Hi,

I am running Sco Unix v3.2B. I want to know how can I store a function I create so I can use it later simply by just calling it from the command prompt. Sorry if I am asking such a noob question.

Thanks.
# 2  
Old 10-21-2008
Hammer & Screwdriver Can you be more specific?

People use ALIAS to allow them to type a shortened version of a command or commands.
People will place scripts in a specific area, and then use the PATH logic so as to always find the scripts.

What are you trying to do exactly?
# 3  
Old 10-21-2008
I want to create a function that outputs the last 50 lines of a process's log. So say my function is called "Last50" and I want to see the last 50 lines for a process called "Traffic". I guess the function would have to look at the PID associated with the "Traffic" process, and then look for the corresponding log, and then use the CAT and TAIL functions. But how and where do I store this function so I can use it later? Hope I am clear.

Thanks.
# 4  
Old 10-21-2008
Sounds like you need to write a short shell script, and save it in a directory which is in your $PATH variable (or add the directory to $PATH). If the 'traffic' process always has it's log in the same location, I don't think you would need to search for the pid of the program. In your shell script you would just use something like
Code:
tail -n 50 < /var/traffic_log.log

...where /var/traffic_log.log is your logfile.

If this doesn't make much sense, please give some more information on what you're trying to do.
# 5  
Old 10-22-2008
I guess I can turn that into a function in the following way..

Last50()
{
tail -n 50 < /var/traffic_log.log}

But I believe this function will only be available during the session. Right? How can I permanently make this function available so that any time I am at the command prompt I can just type "Last50", and get the function to run? Is there a special folder or file where I have to put it?

Thanks
# 6  
Old 10-22-2008
It depends on what shell you are using. ksh93 has builtin support for what you are trying to do. From the ksh93 man page:
Quote:
FPATH The search path for function definitions.
The directories in this path are searched
for a file with the same name as the func-
tion or command when a function with the -u
attribute is referenced and when a command
is not found. If an executable file with
the name of that command is found, then it
is read and executed in the current environ-
ment. Unlike PATH, the current directory
must be represented explictily by . rather
than by adjacent : characters or a beginning
or ending :.
# 7  
Old 10-22-2008
my shell is "sh".
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

6. Shell Programming and Scripting

Storing or Using Numeric Output From a Function

Hi all, I'm trying to implement a linear congruential pseudorandom number generator (<http://en.wikipedia.org/wiki/Linear_congruential_generator>), since $RANDOM and /dev/random aren't standardized. I'm referring to the Shell & Utilities volume of POSIX.1-2008, but I'm running into some odd... (3 Replies)
Discussion started by: PehJota
3 Replies

7. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

8. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

9. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question