you can manually load the functions by:
. /path/to/functional/script
Then it will load that script into the current environment, then the second script can access those functions.
To automate it, simple add at the bottom of your ~/.bashrc
. /path/to/functional/script
that should fix your issue.
I must be misunderstanding the issue or the error is in ssh command.
Here is my test:
File: function_source.sh
#!/bin/bash
function testcall {
echo "now here I am"
}
file do_something.sh
#!/bin/bash
source ./function_source.sh
echo -e "This is called from within the script:\r\n"
testcall
echo -e "End Script - Any further output is from the function call:\r\n\r\n"
Script file do_something.sh loads the source file of function_source.sh and then calls the function testcall, that is in function_source.sh.
ssh %server% "./do_something.sh; testcall"
Output:
-ssh %server% "./do_something.sh; testcall"
This is called from within the script:
now here I am
End Script - Any further output is from the function
call:
bash: line 1: testcall: command not found
Now if I reference function_source.sh is .bashrc with the below line added to the end of .bashrc:
. ./function_source.sh
Now I run the same command:
ssh %server% "./do_something.sh; testcall"
and the output is:
-ssh %server% "./do_something.sh; testcall"
This is called from within the script:
now here I am
End Script - Any further output is from the function call:
now here I am
The only way referencing the file in .bashrc does not work is if you have the below in your .bashrc:
case $- in
*i*) ;;
*) return;;
esac
If you do have that code in your .bashrc, then when you run a command using ssh, it will not load your .bashrc enviroment, and therefore will not load your source file.
Note: This is why you do not just use standard bashrc and script file, you either write them from scratch yourself, or you read every line and know what ever line does.
To cover off the other setups, loading the source file first, then calling the function - Note: I have removed the reference to the source in bashrc:
command:
ssh %server% ". ~/function_source.sh; testcall"
Output:
-ssh %server% ". ~/function_source.sh; testcall"
now here I am
and finally, loading the source file in bashrc, and just calling the function using ssh.
command:
ssh %server% "testcall"
Output:
-ssh %server% "testcall"
now here I am
Let me know if you need any more examples.
Note: I used a windows Command Prompt, with a modified Prompt of -, to run the ssh commands, the %server% variable was used for privacy reasons, and ssh keys were used to authenticate.
To ensure I cover off on everything, I moved to a ssh in one of my linux vm's, so I can ssh to localhost as terdon suggested.
I also modified function source:
-cat function_source.sh
#!/bin/bash
function testcall {
echo -e "now here I am\r\n\r\n"
testanother
}
function testanother {
echo -e "this is another function\r\n\r\n"
}
If I run the command:
ssh localhost "$(typeset -f testcall); testcall"
output:
-ssh localhost "$(typeset -f testcall); testcall"
now here I am
However, if I run the command:
ssh localhost "$(cat function_source.sh); testcall"
Output:
-ssh localhost "$(cat function_source.sh); testcall"
now here I am
this is another function
You would only need to do this, if you did not have write access to the server you were ssh to, and needed to load the functions into the environment each time.