通过 bash 脚本从函数调用函数到远程主机不起作用

Call function from function into remote host through bash script is not working

我正在编写一个bash脚本来登录远程主机并通过它调用多个函数。 从 ssh 远程脚本调用 install_prelibrary 并由此调用其他两个函数。 下面是我的脚本:

#!/bin/bash

source ~/shell/config.sh

install_prelibrary () {
  wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb --no-check-certificate > /dev/null 2>&1;
  if [ $? -ne 0 ]; then
                printf "\n\nError downloading Ubuntu Binary file\n\n"
                exit 0;
  else 
                install_cdt
                create_wallet_and_keys
  fi
}

install_cdt(){
   #some commands
}
create_wallet_and_keys(){
   #some commands
}

SCRIPT="$(cat ~/shell/config.sh) ; $(declare -f) ; install_prelibrary"
for i in ${!genesishost[*]} ; do
        printf "\t=========== node ${genesishost[i]} ===========\n\n"
        SCR=${SCRIPT/PASSWORD/${password}}
        sshpass -p ${password} ssh -l ${username} ${genesishost[i]} "${SCR}"
done

config.sh

#!/bin/bash

username=abc
password=abc
genesishost=(192.168.*.*);

当我 运行 这个脚本使用 bash main.sh 时,首先, create_wallet_and_keys 被调用。我不知道为什么?因为我没有在任何地方手动调用此功能。接下来是 install_prelibrary,然后是 install_cdt。从 install_prelibrary 调用 install_cdt 没问题,但 create_wallet_and_keys 给出错误 command not found。为什么 create_wallet_and_keys 不像其他函数那样在远程主机上被调用? 我希望在远程主机上调用的第一个函数是 install_prelibrary,然后从此函数调用其他两个函数。 请指正。

尝试重新排序您的函数

#!/bin/bash

source ~/shell/config.sh

install_cdt(){
   #some commands
}
create_wallet_and_keys(){
   #some commands
}

install_prelibrary () {
  wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb --no-check-certificate > /dev/null 2>&1;
  if [ $? -ne 0 ]; then
                printf "\n\nError downloading Ubuntu Binary file\n\n"
                exit 0;
  else 
                install_cdt
                create_wallet_and_keys
  fi
}

SCRIPT="$(cat ~/shell/config.sh) ; $(declare -f) ; install_prelibrary"
for i in ${!genesishost[*]} ; do
        printf "\t=========== node ${genesishost[i]} ===========\n\n"
        SCR=${SCRIPT/PASSWORD/${password}}
        sshpass -p ${password} ssh -l ${username} ${genesishost[i]} "${SCR}"
done

这样它们将在函数本身的下方被调用。

如果不确切地看到生成的脚本是什么样子,就不可能解决这个问题。

但我会将您的逻辑分解为一个脚本,该脚本被复制到目的地并在那里执行,还有一个简单的脚本进行复制和评估。

#!/bin/bash

script=$(cat <<\____HERE
install_prelibrary () {
  # Notice also refactoring; comments below
  if wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb --no-check-certificate > /dev/null 2>&1; then
    : pass
  else
    rc=$?
    # Write errors to standard error, exit with an actual failure code
    printf "Error downloading Ubuntu Binary file\n" >&2
    exit $rc
  fi
  install_cdt
  create_wallet_and_keys
}

install_cdt(){
   #some commands
}
create_wallet_and_keys(){
   #some commands
}
____HERE
)
SCRIPT="$(cat ~/shell/config.sh); $script; install_prelibrary"
for i in ${!genesishost[*]} ; do
        printf "\t=========== node ${genesishost[i]} ===========\n\n"
        SCR=${SCRIPT/PASSWORD/"$password"}
        sshpass -p "$password" ssh -l "$username" "${genesishost[i]}" "${SCR}"
done

如果您也需要在本地评估函数,让脚本自行读取并不难;或者简单地将代码存储在外部文件中,source 并将其读入变量。