shell编程练习002(20160227)
来源:互联网 发布:c语言打印糖果图形 编辑:程序博客网 时间:2024/06/06 02:49
1.写一个脚本getinterface.sh,脚本可以接受参数(i,I,a),完成以下任务:
(1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]
(2)当用户使用-i选项时,显示其指定网卡的IP地址;
(3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;
(4)当用户单独使用-a选项时,显示所有的网络接口及其IP地址(lo除外);
执行情况如下
[root@LFG1000105234 ~]# ./getinterface.sh -a
OK
eth0:189.101.0.5 eth1:192.168.1.4 eth2:189.101.7.5 eth3:189.101.8.254 eth4:192.168.0.62 eth5:189.100.1.254 eth6:189.100.2.254
[root@LFG1000105234 ~]# ./getinterface.sh -i eth2
OK
189.101.7.5
[root@LFG1000105234 ~]# ./getinterface.sh -a
OK
eth0:189.101.0.5 eth1:192.168.1.4 eth2:189.101.7.5 eth3:189.101.8.254 eth4:192.168.0.62 eth5:189.100.1.254 eth6:189.100.2.254
[root@LFG1000105234 ~]# ./getinterface.sh -tt
ff
getinterface.sh [-i interface | -I IP | -a]
[root@LFG1000105234 ~]# ./getinterface.sh -I 189
OK
eth0 eth2 eth3 eth5 eth6
(1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]
(2)当用户使用-i选项时,显示其指定网卡的IP地址;
(3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;
(4)当用户单独使用-a选项时,显示所有的网络接口及其IP地址(lo除外);
#! /bin/bashfunction Print_Help(){ echo "getinterface.sh [-i interface | -I IP | -a]"}function Para_Check(){ local paranum=$1 if ([ $paranum = '-a' ] || [ $paranum = '-i' ] || [ $paranum = '-I' ]);then echo "OK" else echo "ff" Print_Help exit fi}function List_Nic(){ echo `ifconfig -a | grep -v "127" | grep -B 1 "inet " | awk '/eth/{print $1;next} /inet/{print $2}' | sed '{N;s/\n//g}'`}function Get_IP(){ if [ $1 == "-i" ];then echo `ifconfig $2 | grep "inet " | awk '{print $2}'` elif [ $1 == "-I" ];then echo `ifconfig -a | grep -B 1 $2 | grep "eth" | awk -F ":" '{print $1}'` else echo "end" Print_Help fi}function main(){ Para_Check $@ Para=$@ if [ $# -eq 1 ];then List_Nic elif [ $# -eq 2 ];then Get_IP $Para else Print_Help fi}main $@
执行情况如下
[root@LFG1000105234 ~]# ./getinterface.sh -a
OK
eth0:189.101.0.5 eth1:192.168.1.4 eth2:189.101.7.5 eth3:189.101.8.254 eth4:192.168.0.62 eth5:189.100.1.254 eth6:189.100.2.254
[root@LFG1000105234 ~]# ./getinterface.sh -i eth2
OK
189.101.7.5
[root@LFG1000105234 ~]# ./getinterface.sh -a
OK
eth0:189.101.0.5 eth1:192.168.1.4 eth2:189.101.7.5 eth3:189.101.8.254 eth4:192.168.0.62 eth5:189.100.1.254 eth6:189.100.2.254
[root@LFG1000105234 ~]# ./getinterface.sh -tt
ff
getinterface.sh [-i interface | -I IP | -a]
[root@LFG1000105234 ~]# ./getinterface.sh -I 189
OK
eth0 eth2 eth3 eth5 eth6
0 0
- shell编程练习002(20160227)
- shell 编程实例练习(二)
- shell编程练习
- Shell编程练习
- shell编程练习001(20160226)
- bash shell 编程练习一
- bash shell 编程练习二
- shell编程的小练习
- Linux SHELL编程练习case1
- shell 练习002 (20160228)
- C++编程练习(002)
- 学习shell——编程练习
- 学习shell——编程练习
- shell脚本编程基础1及练习
- shell脚本编程2及练习
- shell脚本练习(一)
- shell脚本练习(二)
- shell脚本练习(三)
- 使用并发编程的挑战
- BroadcastReceiver小结
- Top 100 Best Blogs for iOS Developers
- 河南第六届ACM省赛(River Crossing)
- Swift - 本地数据的保存与加载(使用NSCoder将对象保存到.plist文件)
- shell编程练习002(20160227)
- opencv保存avi视频
- 《Multimedia Signals and Systems-Basic and Advanced Algorithms for Signal Processing》pdf百度云下载
- WPF的TreeView执行ExpandSubtree时抛出异常System.NullReferenceException
- Core Animation 高级理论知识汇总
- Could not load the FreeMarker template named 'select'
- BZOJ3571[HNOI2014]画框
- java (字符串)
- 1044 Shopping in Mars