簡體   English   中英

訪問遠程服務器上的導出變量

[英]Accessing export variable on remote server

當我嘗試在 if 循環下訪問遠程服務器上的導出變量時,以下不起作用:

VAR=`grep pattern ./checklist | cut -f7`
echo "Port number to be checked for service is $VAR" #working fine
export VAR
ssh -tTq user@node <<EOF
echo "checking service on \`hostname\`";
echo "export value received is $VAR"
echo \$(ps -ef | grep -v grep | grep port=$VAR | wc -l)
if [ \$(ps -ef | grep -v grep | grep port=$VAR | wc -l) == 0 ];
then
echo "service is DOWN"
elif [ \$(ps -ef | grep -v grep | grep port=\$VAR | wc -l) == 1 ];
then
echo "Service is Up"
else
echo "Multiple instances running of selected service"
fi
EOF

收到的 if 循環輸出與預期不符:

所選服務的多個實例運行 + '[' 1 == 2 ']'

而不是 1 == 1 條件匹配

自己找到了答案。 在上面的問題中,我在 elif 條件下錯誤地錯過了 $VAR 之前的 \\ 。

LL:當通過 ssh 使用導出變量時,應避免使用強/單引號 ' 在遠程主機上執行 cmd。

在調用存在於 << EOF ..... EOF 之間的導出變量時,避免在 under 上使用 \\。 (在上面的例子中 $VAR)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM