1.修改文件中的IP
$ ./ip.sh '8.8.8.1'
#!/bin/bash # localIP=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep 8|awk '{print $2}'|tr -d "addr:") for ip in $(cat master.json |grep ip |sed 's/:/ /g'|awk '{print$2}' |sed 's/"//g'|sed 's/,//g');do if [ ${ip} == "1.1.1.1" ] then sed -i "s/${ip}/${localIP}/g" master.json elif [ ${ip} == "1.1.1.2" ] then sed -i "s/${ip}/$1/g" master.json fidone
#!/bin/bashdos2unix agent.jsonmaster=1.1.1.1agent=1.1.1.11for ip in $(cat agent.json |grep ip |sed 's/:/ /g'|awk '{print$2}' |sed 's/"//g'|sed 's/,//g');do if [ ${ip} == ${master}] then echo change master sed -i "s/${ip}/$1/g" agent.json elif [ ${ip} == ${agent} ] then echo change agent sed -i "s/${ip}/$2/g" agent.json fidone
2.嵌套循环
for file in /home/../*.jsondo echo "$file:" for ... donedone
3.判断
for file in /home/.../* if [ -d "$file" && "$file"=="taskserver" ] then echo "$file means localhost is server" elif [ -d "$file" && "$file" == "taskmaster"] then echo "" elif [ -d "$file" && "$file" == "env_agent"] then echo "" fidone