shell脚本--02循环与条件

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 fi done
#!/bin/bash
dos2unix agent.json

master=1.1.1.1
agent=1.1.1.11

for 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
    fi
done

2.嵌套循环

for file in /home/../*.json
do
    echo "$file:"
    for
       ...
    done
done

 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 ""
    fi
done
原文地址:https://www.cnblogs.com/cevinchen/p/9410749.html