机房有关机的服务器,需要每一台设备的设备号,用脚本实现输出每台设备的设备号。
1.关机设备列表
cat num.txt
8-16
19-23
26-32
37-40
43-46
56-60
62
64
68-69
71
73
76-77
80
84
87
89-95
2.输出每台设备的设备号 test.sh
#!/bin/bash
for line in `cat num.txt` #逐行读取文件
do
OLD_IFS="$IFS" #定义一个变量为默认IFS,环境变量$IFS,用于内部字段分隔符 参考:http://blog.itpub.net/27181165/viewspace-775820/
IFS="-" #还原IFS为默认值
arrj=($line)
IFS="$OLD_IFS" #还原IFS为默认值
a=${arrj[0]} #取数组第一个数
b=${arrj[1]:-${arrj[0]}} # “:-”是三目运算符
for((i=$a;i<=$b;i++));
do
echo $i
done;
done
[root@localhost ~]# ./test.sh | xargs >>num5.txt
[root@localhost ~]# cat num5.txt
8 9 10 11 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30 31 32 37 38 39 40 43 44 45 46 56 57 58 59 60 62 64 68 69 71 73 76 77 80 84 87 89 90 91 92 93 94 95
[root@localhost ~]# sed -e 's/ /;/g' num5.txt
8;9;10;11;12;13;14;15;16;19;20;21;22;23;26;27;28;29;30;31;32;37;38;39;40;43;44;45;46;56;57;58;59;60;62;64;68;69;71;73;76;77;80;84;87;89;90;91;92;93;94;95