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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)