How to get the text of a single shell function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the text of a single shell function?
# 1  
Old 01-13-2010
How to get the text of a single shell function?

I want to pass the text of a bash shell function to a remote machine in such a way that I can execute that function on the remote machine. For example, I can do this in a script

ssh some_machine "tfunc () { echo Hello from \$(uname -n) ; } ; tfunc ; "

and the function tfunc() will be executed on the remote machine. I'd like to be able to define the function in my current shell and then use that definition in the ssh command, as in

function tfunc { echo Hello from $(uname -n) ; }
ssh some_machine " <tfunc text here> ; tfunc ; "

The set() command (with no arguments) prints all the variables and functions known the shell, but this is too much. I'd like to be able to get just the text of the function I want to pass. Since the set() command knows the function text it seems like it must be available, but I haven't been able to figure out how to do this. Does anyone know if this is possible? Thanks.

Mark E. Hamilton

Last edited by MarkHamilton; 01-13-2010 at 06:34 PM..
# 2  
Old 01-13-2010
Quote:
Originally Posted by MarkHamilton
I want to pass the text of a shell function to a remote machine in such a way that I can execute that function on the remote machine. For example, I can do this in a script

ssh some_machine "tfunc () { echo Hello from \$(uname -n) ; } ; tfunc ; "
Yes. It works almost exactly as you've given it, even. All I did was change some_machine to a real hostname.

You don't need to put it in a function, either. Just give it "echo Hello from \$(uname -n)" and it goes.

I don't see a way to make set list only the variables and functions you want. It either lists everything, or sets options, set being what it is. If you know the function is define all on one line like you've done, you can grep for them easily.

Last edited by Corona688; 01-13-2010 at 06:39 PM..
# 3  
Old 01-13-2010
It's not really available. You can't export functions.

Why can't you just run the code with ssh?

Like:
Code:
ssh some_machine "\$(uname -n)"

# 4  
Old 01-13-2010
The actual function I want to pass to the remote machine is much more complex. What I posted was just a simplified example of my problem. I want to use the function both in the shell script and on the remote machine, and if I define it in multiple places (once for my shell script to use and once in the ssh call) it becomes much more difficult to maintain; changes to one might not get made in the other.

Also, unfortunately, my function is not a single line, but even when it is set() turns it into multiple lines;

Code:
% function tfunc { echo Hi ; }
% set
...
tfunc ()
{
    echo Hi
}


Last edited by MarkHamilton; 01-13-2010 at 07:08 PM..
# 5  
Old 01-13-2010
If you have ssh, why don't you put what you have to run in a script, scp that, and then run it - via ssh?
# 6  
Old 01-13-2010
That's going to get pretty difficult, and could break in lots of interesting ways depending on shell, shell version, shell options, and the robustness of your re-escaping.

Have you considered putting the relevant function in a script file, to share that file with the servers? Your script could sftp it over first, then call it with ssh like
Code:
ssh server_name 'source ./script_file ; function_name'

Or you could just arrange to have the script file sourced in the server's .bashrc or what have you.

[edit] crosspose. scottn beats me again! Smilie
# 7  
Old 01-13-2010
Yes, I could put the function in a separate script and scp it to the remote machine, then execute it there. The approach I was asking about would be much simpler, however. Nothing external to the script itself would be required.

FWIW, this example is similar to what I was thinking about:

Code:
% func_str="function tfunc { uname -n ; }"
% eval $func_str
% tfunc
my_machine
%  ssh some_machine "${func_str} ; tfunc ; "
some_machine
%


Thanks.

Last edited by MarkHamilton; 01-13-2010 at 07:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

A single statement without main function in c

A sample.c file is written with only one single statement. main; Segmentation fault occurred when executed that file. Any statement other than main; is written, for example unix; then it won't compile. why is this behaviour ! (2 Replies)
Discussion started by: techmonk
2 Replies

2. Shell Programming and Scripting

Avoid single line output from function

I am new to shell scripting and wished to get few things clarified. While calling functions within shell script, output comes out as single line irrespective of the no of echos or newlines I tried within function + the echo -e used to invoke function ( as instructed online) : #!/bin/sh inc() {... (1 Reply)
Discussion started by: RMath
1 Replies

3. Shell Programming and Scripting

Induct two functions in a single function

echo -en "Enter the logmsg=" {D1234,S1234 | rohit singh} read logmsg logmsg1=${logmsg%%|*}; echo $logmsg1 |sed "s/$/,/g"|sed 's/*$//'>pre-commit.config logmsg_len2=$(echo ${logmsg} | awk '{print $2}'); logmsg_suffix="|"; length() { cat "pre-commit.config"| awk -F'' '{for(i=1;i<NF;i++) {... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

4. UNIX for Dummies Questions & Answers

extract text between two words on a single line

Hi Guys, Can someone help me with a way to extract text between two words on a single line. For example if the file has below content I want to extract all text between b and f inclusive of b and f. Aparently sed does this but does it line by line and I guess it cannot read word by word. ... (11 Replies)
Discussion started by: krishnaux
11 Replies

5. UNIX for Dummies Questions & Answers

appending in a single text file

actually, i have to grep the value of cpu usage from six differrent servers and it has to generated in a single report( in a single text or excel file). i have used the below command for grepping the value. top -n 1 > arun.txt cat arun.txt | grep 'init' | cut -c 49-53 > output1.csv like... (2 Replies)
Discussion started by: arunmanas
2 Replies

6. 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

7. Shell Programming and Scripting

Create multiple text file from a single text file on AIX

Hi I need to create multiple text files from onc text file on AIX. The data of text files is as below: ********************************************** ********************************************** DBVERIFY: Release 10.2.0.4.0 - Production on Tue Nov 10 13:45:42 2009 Copyright (c) 1982,... (11 Replies)
Discussion started by: lodhi1978
11 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

Have several text files and want to merge into a single

Hello, I have several files that begin with db. in my directory and I would like to first take it from a specific word starting from $TTL until the end of the contents then do the same all the way down the directory then merge them into one txt file. Is this possible? I am using cygwin with... (4 Replies)
Discussion started by: richsark
4 Replies
Login or Register to Ask a Question