How to export the function in ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to export the function in ssh
# 8  
Old 03-08-2012
When you run ssh, you get a brand new, independent shell.

You set variables in it the exact same way you would locally: VARNAME="..."

You create functions in it the exact same way you would locally:
Code:
function_name
{
...
}

There's no "exporting" of anthing, it does exactly what you tell it to, no more, no less.

When you want to send big blocks of code over ssh, and make sure your code does exactly what it looks like, it can be useful to do something like this:

Code:
ssh username@host exec /bin/bash -s "$ARG1" "$ARG2" <<"EOF"
function_name
{
        echo "inside function"
}

echo "ARG1 was $1"
echo "ARG2 was $2"

function_name
EOF

The -s argument, followed by "$ARG1", "$ARG2", etc. lets you set the $1 $2 ... variables for code running on the remote server. (It works on any shell, not just bash.) Nothing else at all from the local side carries over, giving you precise control.

<<"EOF" instead of <<EOF prevents any expansion whatsoever from happening inside the here-document, guaranteeing that everything in it gets sent over raw, dollar-signs and quotes and whitespace and anything else purely unmangled.

An unfortunate side-effect of this is that your remote shell code won't be able to use read. Standard input has been redirected from the contents of the here-document, you can't read from the terminal.
This User Gave Thanks to Corona688 For This Post:
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

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

3. Shell Programming and Scripting

[bash] running a function remotely using ssh

Hi all. I need a bash script to run a function remotely. I think it should be similar to the following but can't figure out the exact syntax. Here is the script: #!/bin/bash function nu () { for var in {0..5}; do printf "$var, "; done; echo } ssh host "$(typeset -f); nu" ... (9 Replies)
Discussion started by: ziguy
9 Replies

4. Shell Programming and Scripting

Export a function to another function

is there a way to export a function to another function? i'm using #!/usr/xpg4/bin/sh in the first line of my script. firstfunc () { echo $1 is nice } secondfunc () { firstfunc Nancy } when i run a similar scenario to the above, i get: /usr/xpg4/bin/sh: firstfunc: not found (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

Run recursive function with variables over SSH

Hi, I don't know if you can help or if this is even possible, but I am trying to run the following function over an ssh and (depending on the itteration I choose) keep getting unexpected token or undefined symbol errors. The function is: killtree() { typeset parent=$1 typeset child... (1 Reply)
Discussion started by: RECrerar
1 Replies

6. Shell Programming and Scripting

korn shell - export function??

Hi, I have a question. I know that if I want to access any variables defined in a calling script (script1.ksh) in a called script (script2.ksh) I need to export that variable in the calling script to make it available in the called script. But what about the functions in the calling script? ... (1 Reply)
Discussion started by: dips_ag
1 Replies

7. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

8. Shell Programming and Scripting

ssh running on function

i have a problem regarding running an ssh command while inside "function" script structure #!/usr/bin/bash func1(){ script=$1 ssh user@server "$script" } cat script.txt | while read line do func1 $line done exit 0 the problem is when ssh ran,... (1 Reply)
Discussion started by: ryandegreat25
1 Replies

9. Shell Programming and Scripting

Run function from script over ssh (BASH)

Hi, Is there any cleaver way to run function from the bash scrip over ssh? For example: #!/bin/bash #Function 1 FN1 () { ls -l } #Main run ssh user@host FN1 exit 0 Yeah, I know it will not work, but I'm asking how to make it to work :) I'm suspecting that it would be... (1 Reply)
Discussion started by: columb
1 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