带有for的多行ssh命令>>

问题描述 投票:0回答:2

我有一个正在通过bash执行命令的ssh脚本。

FILENAMES=(
  "export_production_20200604.tgz"
  "export_production_log_20200604.tgz"
  "export_production_session_20200604.tgz"
  "export_production_view_20200604.tgz"
)

sshpass -p $PASSWORD ssh -T $LOGIN@$IP '/bin/bash' <<EOF
  for f in "${FILENAMES[@]}"; do
    echo Untar "$f"
  done
EOF

是我执行脚本时,$f为空。

我已经在线查看了多个解决方案来执行多个命令,但是都没有用:

link 1

link 2

...

您能帮我解决吗?


执行:

for f in "${FILENAMES[@]}"; do
    echo Untar "$f"
done

<<EOF EOF之外,工作


在本地:

bash 4.4.20(1)-发布

Remote:

bash 4.2.46(2)-发布



编辑

时间表紧迫,别无选择,我实现了@ hads0m提供的解决方案,可能会帮助遇到相同问题的其他开发人员:

# $1 the command
function executeRemoteCommand() {
    sshpass -p $DB_PASSWORD ssh $DB_LOGIN@$DB_SERVER_IP $1
}

for i in "${!FILENAMES[@]}"; do
    f=$FILENAMES[$i]

    DB_NAME=$DB_NAMES[$i]

    # Untar the file
    executeRemoteCommand '/usr/bin/tar xzvf '$MONGODB_DATA_PATH'/'$TMP_DIRECTORY'/'$f' --strip-components=1'

    # Delete the tar
    executeRemoteCommand 'rm -f '$MONGODB_DATA_PATH'/'$TMP_DIRECTORY'/'$f''

    # Restore the database
    executeRemoteCommand 'mongorestore --host 127.0.0.1:'$DB_PORT' --username "'$MONGODB_USER'" --password "'$MONGODB_PASSWORD'" --authenticationDatabase admin --gzip "'$DB_NAME'" --db "'$DB_NAME'"'
done

我有一个通过ssh执行命令的bash脚本。 FILENAMES =(“ export_production_20200604.tgz”“ export_production_log_20200604.tgz”“ export_production_session_20200604.tgz”“ ...

bash ssh heredoc
2个回答
1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.