如何在一行上放置echo语句和arp语句的输出?

时间:2015-06-14 14:46:40

标签: linux bash shell output

如何获得输出" echo"和" macaddress"在一条线上? 这就是我所拥有的:

ipRange="192.168.0."
macaddress= arp  | grep -w  "$ipRange$1" | awk '{print $3,$1}' 

ping -c1 "$ipRange$1" > /dev/null
         if [ $? -eq 0 ]; then
echo  "deze host met mac address en ip address is up $macaddress"
else 
echo "het is down"
fi

这是输出:

VirtualBox ~ $ bash test2.sh 149
e0:b9:a5:f8:24:c3 192.168.0.149
deze host met mac address en ip address is up 

2 个答案:

答案 0 :(得分:2)

这应该以单行输出

ipRange="192.168.0."
macaddress=$(arp  | grep -w  "$ipRange$1" | awk '{print $3,$1}')

ping -c1 "$ipRange$1" > /dev/null
         if [ $? -eq 0 ]; then
echo  "deze host met mac address en ip address is up $macaddress"
else 
echo "het is down"
fi

答案 1 :(得分:1)

只需替换macaddress= arp | grep -w "$ipRange$1" | awk '{print $3,$1}'

即可

macaddress=$(arp | grep -w "$ipRange$1" | awk '{print $3,$1}')