• 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
  • 相关阅读:
    css
    css加号波浪号
    C++对象池
    C++11 智能指针
    C++内存泄漏检测(调试工具)
    JSONP是个嘛玩意?解决跨域问题?
    使用django + KindEditor 开发个人博客系统
    前端文本框插件KindEditor
    jQuery AJAX
    Django ModelForm表单验证
  • 原文地址:https://www.cnblogs.com/cevinchen/p/9410749.html
Copyright © 2020-2023  润新知