v3_addServer.sh内容
cat v3_addServer.sh
#!/bin/bash
uplist=ipList
main_2()
{
while read noteline
do
if [ -z "`echo $noteline | grep -v ^$`" ]; then
continue
fi
commentflag=${noteline:0:1}
if [ "${commentflag}" == "#" ]; then
continue
fi
PlatformId=`printf "$noteline" | awk '{print $1}'`
ServerId=`printf "$noteline" | awk '{print $2}'`
ServerIP=`printf "$noteline" | awk '{print $3}'`
DbIp=`printf "$noteline" | awk '{print $4}'`
echo $PlatformId $ServerId $ServerIP $DbIp
done<$uplist
}
#主程序
main_2
ipList内容
cat ipList
# 01id 02id 测试1ip 测试2ip
2 1 192.167.33.216 172.31.252.133
2 2 192.167.33.43 172.31.252.134
2 3 192.167.33.164 172.31.252.142
2 4 192.167.33.180 172.31.252.141
2 5 192.167.33.197 172.31.252.140
2 6 192.167.33.106 172.31.252.139
2 7 192.167.33.185 172.31.252.138
2 8 192.167.33.97 172.31.252.136
2 9 192.167.33.187 172.31.252.137
执行sh v3_addServer.sh可以正常打印ipList中的内容
sh v3_addServer.sh
2 1 192.167.33.216 172.31.252.133
2 2 192.167.33.43 172.31.252.134
2 3 192.167.33.164 172.31.252.142
2 4 192.167.33.180 172.31.252.141
2 5 192.167.33.197 172.31.252.140
2 6 192.167.33.106 172.31.252.139
2 7 192.167.33.185 172.31.252.138
2 8 192.167.33.97 172.31.252.136
2 9 192.167.33.187 172.31.252.137
现在往v3_addServer.sh 中的main_2()添加read,进行读取外部指令的操作:
main_2()
{
while read noteline
do
if [ -z "`echo $noteline | grep -v ^$`" ]; then
continue
fi
commentflag=${noteline:0:1}
if [ "${commentflag}" == "#" ]; then
continue
fi
PlatformId=`printf "$noteline" | awk '{print $1}'`
ServerId=`printf "$noteline" | awk '{print $2}'`
ServerIP=`printf "$noteline" | awk '{print $3}'`
DbIp=`printf "$noteline" | awk '{print $4}'`
echo $PlatformId $ServerId $ServerIP $DbIp
echo "确认信息:"
read cmdName
case $cmdName in
yes|YES)
#checkStatus
echo "执行checkStatus函数"
;;
no|NO)
echo "退出."
;;
esac
done<$uplist
}
再次执行v3_addServer.sh
sh v3_addServer.sh
2 1 192.167.33.216 172.31.252.133
确认信息:
2 3 192.167.33.164 172.31.252.142
确认信息:
2 5 192.167.33.197 172.31.252.140
确认信息:
2 7 192.167.33.185 172.31.252.138
确认信息:
2 9 192.167.33.187 172.31.252.137
确认信息:
没有读取到read cmdName这一行,而且打印出,也只打印了 1 3 5 7 9行的,这是为什么的?
... done<$uplist
这是重定向的意思,把 uplist 文件的内容作为标准输入。一次循环里有两个
read
。read
意思是从标准输入读取一行,所以read noteline
读取奇数行read cmdName
读取偶数行。所以下面这个吧