You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 16.04下复用curl模式批量执行GitHub远程脚本方案咨询

Batch Execute Remote GitHub Scripts with a For Loop (Ubuntu 16.04)

Hey there! I get it—repeating curl commands for every single script is tedious. Let's fix that with a flexible for loop that works whether you have 3 scripts or 10. Here are two practical solutions tailored to different scenarios:

1. Explicit Script List (Best for Known Script Names)

If you know exactly which scripts you need to run, define them in an array. This is clean, easy to maintain, and lets you add/remove scripts in seconds.

# Step 1: Define your list of script filenames
SCRIPTS=(
  "init-server.sh"
  "install-deps.sh"
  "configure-services.sh"
  "setup-monitoring.sh"
  # Add or remove lines here as needed
)

# Step 2: Replace this with the RAW URL of your GitHub script directory
REPO_SCRIPT_URL="https://raw.githubusercontent.com/your-username/your-repo/main/scripts"

# Step 3: Loop through each script and execute it
for script in "${SCRIPTS[@]}"; do
  echo "===== Starting execution of $script ====="
  # Use -sSL to keep curl quiet, follow redirects, and show errors if any
  curl -sSL "${REPO_SCRIPT_URL}/${script}" | bash
  # If your script requires sudo, use this line instead:
  # curl -sSL "${REPO_SCRIPT_URL}/${script}" | sudo bash
  echo "===== Finished $script ====="
done

Why this works:

  • The array ${SCRIPTS[@]} safely handles filenames with spaces (though you probably shouldn't use spaces in script names anyway)
  • The echo lines give you clear progress updates so you can track which script is running
  • curl -sSL suppresses unnecessary curl output while still letting the script's own output show up

2. Auto-Detect Scripts (For Numbered/Patterned Filenames)

If your scripts follow a consistent naming pattern (like script-1.sh, script-2.sh) and you don't want to update the array every time, use a loop with existence checks. This skips any scripts that don't exist in the repo.

REPO_SCRIPT_URL="https://raw.githubusercontent.com/your-username/your-repo/main/scripts"

# Loop from 1 to 10 (adjust the upper limit to your needs)
for i in {1..10}; do
  script_filename="script-${i}.sh"
  script_url="${REPO_SCRIPT_URL}/${script_filename}"

  # Check if the script exists on GitHub before running it
  if curl -s --head "$script_url" | head -n 1 | grep -q "200 OK"; then
    echo "===== Executing $script_filename ====="
    curl -sSL "$script_url" | bash
    echo "===== Completed $script_filename ====="
  else
    echo "===== $script_filename not found, skipping ====="
  fi
done

Pro Tips:

  • Pass arguments to scripts: If your scripts need command-line args, add them after bash like this:
    curl -sSL "$script_url" | bash -s arg1 arg2
    
  • Log output to files: To save each script's output for later debugging, redirect to a log file:
    curl -sSL "$script_url" | bash > "${script_filename}.log" 2>&1
    
  • Verify the raw URL: Make sure your GitHub raw URL points directly to the script directory—you can get this by clicking "Raw" on any script in your repo and trimming the filename from the end.

Hope these examples solve your redundancy problem! Feel free to tweak the loop logic or add error handling if you have specific edge cases to cover.

内容的提问来源于stack exchange,提问作者Arcticooling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:21:04