腳本文件 bash、lua 與 perl

bash

file_start=$1
file_end=$2
port_start=$3
port_end=$4
ip=$5

for num in $(seq -f "%02g" $file_start $file_end)
do

echo 生成第$num個文件

nodes=()

for port in $(seq $port_start $port_end)
do
    nodes+=( "\"$ip:${port}?proxyKey=abc&platform=1&settleType=1&appType=1&ips=.config/ips.txt\"" )
done

# join nodes by comma
nodes_str=$(printf "\n%s," "${nodes[@]}")
nodes_str=${nodes_str:0:-1}

cat <<EOF > /tmp/config-$num.json
{
  "Debug": true,
  "Quite": false,
  "ServeNodes": [
    $nodes_str
  ],
  "DefaultPlatform": {
    "AuthCfg": {
      "Enabled": false
    },
    "MonitorCfg": {
      "Enabled": false
    }
  },
  "Platforms": {
    "1": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
    },
    "2": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
    }
  },
  "CacheCfg": {
    "Type": "none",
    "RedisCfg": {
      "Addr": "127.0.0.1:6379",
      "Password": "",
      "DB": 1
    }
  }
}
EOF
done

lua

local file_start, file_end, port_start, port_end, ip = ...

for i = file_start, file_end do
    local num = string.format("%02d", i)
    print([[生成第]] .. num .. [[個文件]])

    local nodes = {}
    for port = math.floor(port_start), math.floor(port_end) do
        local _node = [["]] .. ip .. [[:]] .. port ..
                          [[?proxyKey=abc&platform=1&settleType=1&appType=1&ips=.config/ips.txt"]]
        table.insert(nodes, _node)
    end
    local node_str = table.concat(nodes, ",\n")

    local file = io.open([[/tmp/config-]] .. num .. [[.json]], "w")
    file:write([[{
  "Debug": true,
  "Quite": false,
  "ServeNodes": [
  ]] .. node_str .. [[,
  "DefaultPlatform": {
    "AuthCfg": {
      "Enabled": false
    },
    "MonitorCfg": {
      "Enabled": false
    }
  },
  "Platforms": {
    "1": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
    },
    "2": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
    }
  },
  "CacheCfg": {
    "Type": "none",
    "RedisCfg": {
      "Addr": "127.0.0.1:6379",
      "Password": "",
      "DB": 1
    }
  }
}]])
    file:close()
end

perl

my ($start, $end, $portl, $portr, $ip) = @ARGV;

foreach $num ($start..$end) {
    $num = sprintf("%02d", $num);
    print "生成第$num個文件\n";

    # 生成節點信息
    my @nodes = ();
    foreach $port ($portl..$portr) {
        push(@nodes, "\"$ip:$port?proxyKey=abc&platform=1&settleType=1&appType=1&ips=.config/ips.txt\"");
    }
    my $node_str = join(",\n", @nodes);

    # 生成文件
    open($fh, '>', "/tmp/config-$num.json");
    my $content = <<EOF;
{
  "Debug": true,
  "Quite": false,
  "ServeNodes": [
    $node_str
  ],
  "DefaultPlatform": {
    "AuthCfg": {
      "Enabled": false
    },
    "MonitorCfg": {
      "Enabled": false
    }
  },
  "Platforms": {
    "1": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
    },
    "2": {
      "AuthCfg": {
        "Enabled": true,
        "Url": "http://xx-api.domain.com/app/auth",
        "CacheTime": 5000
      },
      "MonitorCfg": {
        "Enabled": true,
        "Url": "http://xx-logs.domain.com/report"
      }
  },
  "CacheCfg": {
    "Type": "none",
    "RedisCfg": {
      "Addr": "127.0.0.1:6379",
      "Password": "",
      "DB": 1
    }
  }
}}
EOF
    print $fh $content;
    close $fh;
    print "寫入完成\n";
}

2233

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章