shell 可控并发实现

1、实现分析日志 并发查询ip 归属

#!/bin/bash
# access single 2 (ctrl+c)
  trap "exec 99>&-;exec 99<&-;exit 0" 2
# create fifo
  mkfifo tmpfifo
# bond fd to fifo
  exec 99<>tmpfifo
#setup THREAD_NUM 
  THREAD_NUM=100
  for ((n=1;n<=$THREAD_NUM;n++))
    do 
       echo "\n" >&99
    done
#main 
  while read access_num ip
    do
    {
      read -u 99 
       { address=curl --connect-timeout 5 http://ip.taobao.com/service/getIpInfo.php?ip=$ip 2&gt;/dev/null
         ip_address=echo -e "$address"|tr -s ',' '\n'|grep -E '("area"|"city"|"ip")'|tr -d } |tr -s '\n' ' '
         echo "$ip_address \"access_num\": \"$access_num\"" >> ip_address.txt
     #keep THREAD_NUM 
       echo "\n" >&99  
      }&
 }
    done <$1
    wait 
echo "run over "
# unbond fd to fifo
exec 99>&-
exec 99<&-
rm tmpfifo

2、淘宝查询ip归属 API
curl --connect-timeout 5 http://ip.taobao.com/service/getIpInfo.php?ip=$ip

Pingbacks已打开。

Trackbacks

引用地址

评论
发表评论