侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 119 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

shell读取txt文件

管理员
2025-08-14 / 0 评论 / 0 点赞 / 0 阅读 / 1249 字
# 新增批量执行功能
run_batches() {
  local accounts=()

  # 读取所有 test-*.txt 文件的行
  for file in test-*.txt; do
    [ -f "$file" ] || continue
    mapfile -t tmp < "$file"
    for line in "${tmp[@]}"; do
      [ -n "$line" ] && accounts+=("$line")
    done
  done

  if [ ${#accounts[@]} -eq 0 ]; then
    error "没有找到任何账号数据(xifan-*.txt)"
    exit 1
  fi

  local total=${#accounts[@]}
  local batch_size="${BATCH_SIZE:-10}"
  local batch_delay="${BATCH_DELAY:-5}"
  local start=0
  local batch_num=1

  log "分批处理模式:总共 $total 个账号,每批 $batch_size 个,批次间延迟 ${batch_delay} 秒"

  while [ $start -lt $total ]; do
    local end=$((start + batch_size))
    [ $end -gt $total ] && end=$total
    local batch=("${accounts[@]:$start:$((end - start))}")

    # 用 /& 拼接(保留行内空格)
    local joined
    local IFS=$'/&'
    joined="${batch[*]}"

    export test_env="$joined"
    log "执行第 $batch_num 批:账号 ${start} 到 $((end - 1)) / 共 $total"

    run_program

    start=$end
    batch_num=$((batch_num + 1))

    # 批次间延迟(最后一批不需要延迟)
    if [ $start -lt $total ] && [ $batch_delay -gt 0 ]; then
      log "等待 ${batch_delay} 秒后处理下一批..."
      sleep "$batch_delay"
    fi
  done
}

0

评论区