shell - Shell:无法从 ssh 连接获取返回值
问题描述
我有一个脚本 test_server_conn.sh 和两个服务器,即 Serv1 和 Serv2。尝试从 Serv1 执行脚本并与 Serv2 建立 ssh 连接。检查文件是否存在,然后返回一个值。
test_server_conn.sh
#!/usr/bin/env bash
test_file(){
echo "Establishing ssh connection with Serv2."
sshpass -p pswd ssh uid@serv2
retval=""
FILE=/etc/docker
if [ -d "$FILE" ];
then
echo "$FILE is a directory."
retval=0
else
echo "Check and try again. Closing the connection from Serv2."
retval=1
fi
return "${retval}"
}
echo "Execution started from Serv1..."
test_file
retval=$?
if [ "${retval}" -eq 0 ];
then
echo "Directory is ready"
else
echo "Directory is not ready."
fi
脚本执行但未能停止执行。
Actual results
:
Execution started from Serv1...
Establishing ssh connection with Serv2.
<nothing after this and the curser keeps blinking>
Expected results
:
Execution started from Serv1...
Establishing ssh connection with Serv2.
Directory is ready/ Directory is not ready.
不明白为什么这不返回任何东西并进入挂起状态?如何从 ssh 连接返回值而不会遇到任何问题?