2015-06-14 33 views
3

如何在一行上獲得輸出「echo」和「macaddress」? 這是我有:如何在一行放置echo語句和arp語句的輸出?

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 
+0

提供所需的樣本輸出/代碼 – Jahid

+0

@Jahid deze主機遇到mac地址en ip地址是up e0:b9:a5:f8:24:c3 192.168.0.149 –

+0

@Jahid或者如果有可能「de host met mac address: e0:b9:a5:f8:24:c3 en ip address:192.168.0.149 is up「 –

回答

1

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

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

+0

謝謝你的工作 –

+0

用'fgrep'替換'grep -w'也可能是個好主意。試試這個:'echo 1.2.3.4 | grep -qw 1.2.3。 || echo nope' – lcd047

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 
+0

將'grep -w'替換爲'fgrep'也是一個好主意。試試這個:'echo 1.2.3.4 | grep -qw 1.2.3。 ||回聲nope' – lcd047