I have a shell script file like this:
#!/bin/bash
CONF_FILE="/tmp/settings.conf" #settings.conf contains OS_NAME="Caine Linux"
source $CONF_FILE
display_os_name() { echo "My OS is:" $OS_NAME }
#using the function locally works fine
display_os_name
#displays: My OS is: Caine Linux
#using the function on the remote host doesn't work
ssh user@host "$(declare -f); display_os_name"
#displays: My OS is:
If I remove the -f and I use just ssh user@host "$(declare); display_os_name" it works but displays these errors and warnings:
bash: line 10: BASHOPTS: readonly variable
bash: line 18: BASH_VERSINFO: readonly variable
bash: line 26: EUID: readonly variable
bash: line 55: PPID: readonly variable
bash: line 70: SHELLOPTS: readonly variable
bash: line 76: UID: readonly variable
If I use ssh user@host "$(declare); display_os_name >/dev/null" to suppress the warnings only the output of the function is suppressed (My OS is: Caine Linux), not the warnings.
Is there a way to run local functions together with sourced local files on a remote SSH host?
typeset -fonly the function is available on the remote host, not the imported settings file.sshis just a connection tool, not a distributed computing framework.2>/dev/nullinstead of>/dev/nullto suppress the error messages rather than the standard output. Of course, it'd be better to fix the source of those error messages.sourceing the file, you could just put its contents into the string sent to the remote host for execution.ssh user@host "$(</tmp/settings.conf); $(declare -f); display_os_name"