我正在编写一个脚本,稍后将其添加为cronjob,因此该脚本具有函数,并且在其中使用here文档在远程服务器上执行命令
有一个变量"result"在这个脚本中,它将告诉主机的状态
我需要相同的变量"result">
我该怎么做呢
one(){
ssh_cmd="$(cat <<-EOF
echo --------------------------------------------------------------
echo "Checking testapp Status on Domain Controller --> host = slave1 "
echo --------------------------------------------------------------
result=($(/opt/jboss/web/bin/jboss-cli.sh --connect controller=10.0.0.4:9990 --commands='ls /host=slave1/server-config=testapp' | grep 'status=' | awk 'FNR%2' | sed -r 's/.{7}//'))
echo ----------------
echo "${result[@]}"
echo ----------------
if [ "${result[@]}" != "STARTED" ];
then
echo --------------------------------------
echo "Starting the server"
echo --------------------------------------
/opt/jboss/web/bin/jboss-cli.sh --connect controller=10.0.0.4:9990 --commands='/host=slave1/server-config=testapp:start'
else
echo --------------------------------------
echo "Server is running"
echo --------------------------------------
fi
EOF
)"
ssh -t root@someserver "$ssh_cmd"
}
echo $result
if [ $result == value];
then
two (run function two)
else
exit
需要相同的变量"result">
那么你需要发送它,没有其他方式。您可以使用ssh
的输出发送它:
....
# output the value.
echo "unique_string_with_result=$result"
EOF
)"
# get ssh output to a file
ssh ..... | tee tempfile.txt
# filter the output and extract the value
result=$(sed -n 's/unique_string_with_result=//p' tempfile.txt)
}