我正在尝试使用这段代码(在本地服务器上工作)在 SSH 上的远程服务器上
df |tail -n +2 | awk '{if ($6 == "/var") print $2; }'
我的代码看起来像这样
function dst_request {
# Effectue une requete sur la destination
local RESULT
if [ ${hostssh} ]
then # Destination SHH
RESULT=$( ssh ${userssh}@${hostssh} "eval ${1}" )
else # Destination locale
echo "APPEL LOCAL"
RESULT=$( eval ${1} )
fi
echo ${RESULT}
}
#place utilisée sur le disque distant
dddistantsize=$(dst_request "df")
echo ${dddistantsize}
dddistantsize_n=`echo ${dddistantsize} |tail -n +2 | awk '{if ($6 == "/var") print $2; }'`
echo "dddistantsize_n="${dddistantsize_n}
dddistantsize_mb=$(($dddistantsize_n/1024))
dddistantused=$(dst_request "df")
echo ${dddistantused}
dddistantused_n=`echo ${dddistantused} |tail -n +2 | awk '{if ($6 == "/var") print $3; }'`
echo "dddistantused_n="${dddistantused_n}
dddistantused_mb=$(($dddistantused_n/1024))
dddistantdispo=$(dst_request "df")
echo ${dddistantdispo}
dddistantdispo_n=`echo ${dddistantdispo} |tail -n +2 | awk '{if ($6 == "/var") print $4; }'`
echo "dddistantdispo_n="${dddistantdispo_n}
dddistantdispo_mb=$(($dddistantdispo_n/1024))
dddistantprcused=$(dst_request "df")
dddistantdispo_n=`echo ${dddistantprcused} |tail -n +2 | awk '{if ($6 == "/var") print $5; }'`
echo "Disque distant : "${dddistantused_mb}"Mb"/${dddistantsize_mb}"Mb plein à "${dddistantdispo_n}" (Libre ${dddistantdispo_mb}Mb)"
问题是远程调用给我发回单行结果(而不是本地的多行结果)...并且 awk 无法解析它。
我确切地说远程 df 不支持 --output 选项...
答案1
我找到了解决办法
dddistantdf=$(dst_request "df /var")
#echo $dddistantdf | grep -o '[0-9]* [0-9]* [0-9]* *[0-9]*%'
dddistantsize_n=`echo $dddistantdf | grep -o '[0-9]* [0-9]* [0-9]* *[0-9]*%' | awk '{print $1;}'`
dddistantused_n=`echo $dddistantdf | grep -o '[0-9]* [0-9]* [0-9]* *[0-9]*%' | awk '{print $2;}'`
dddistantdispo_n=`echo $dddistantdf | grep -o '[0-9]* [0-9]* [0-9]* *[0-9]*%' | awk '{print $3;}'`
dddistantdispo_prc=`echo $dddistantdf | grep -o '[0-9]* [0-9]* [0-9]* *[0-9]*%' | awk '{print $4;}'`
在英语服务器或法语服务器上工作;)