2018/02/10

February 10 2018 Saturday

Weather : cloudly
提示用戶輸入網卡的名字,而後咱們用腳本輸出網卡的ip,須要考慮下面幾個問題:
1.輸入的字符不符合網卡名字規範,怎麼應對?
2.名字符合規範,可是根本就沒有這個網卡怎麼辦?bash

這個答案比較牽強,若是有多個ip呢、?code

[root@Dasoncheng sbin]# cat b.sh 
#!/bin/bash
##attention we will output the ip of network card if users input the name of network card
##Prompt the user to enter the name of the network card, and then we use the script to output the IP of the network card. 
read -p "Please input a name of network card:(for example:eth01)" n
#num=`echo $n |grep "^eth[0-9]*"`
if `echo $n |grep "^eth[0-9]*" &>/dev/null` && `ip add |grep "$n" &>/dev/null`;
then
    ip add |grep -A2 "$n:" |awk '/inet/ {print $2}' |awk -F '/' '{print $1}'
else
    echo "Please input a vailded network card !"
fi
本站公眾號
   歡迎關注本站公眾號,獲取更多信息