githubclone.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. # This script is created by Bing Chat
  3. # 你可以使用以下修改后的 shell 脚本来自动尝试从 github.com 克隆仓库,如果失败了,再尝试使用不同的加速地址来克隆仓库。这个脚本会先尝试从 github.com 克隆仓库,如果失败了,再尝试使用我之前提到的加速地址来克隆仓库,直到成功为止。每个地址都会尝试克隆 3 次。如果克隆失败,脚本会输出友好的错误提示。这个脚本接受两个外部参数:$1 是用户名,$2 是仓库名。
  4. # 获取外部参数
  5. USERNAME="$1"
  6. REPO="$2"
  7. # 生成仓库 URL
  8. REPO_URL="https://github.com/$USERNAME/$REPO.git"
  9. # 加速地址列表
  10. MIRRORS=(
  11. "https://ghproxy.com/https://github.com"
  12. "https://github.com.cnpmjs.org"
  13. "https://github.com"
  14. "https://hub.fastgit.org"
  15. "https://gitclone.com"
  16. "https://gh.api.99988866.xyz"
  17. "https://github.zhlh6.cn"
  18. "https://toolwa.com/github"
  19. )
  20. for mirror in "${MIRRORS[@]}"; do
  21. # 生成加速后的 URL
  22. mirror_url="${REPO_URL/https:\/\/github.com/$mirror}"
  23. # 尝试克隆仓库
  24. for i in {1..3}; do
  25. echo "Trying to clone from $mirror_url (attempt $i)"
  26. if git clone "$mirror_url"; then
  27. echo "Successfully cloned from $mirror_url"
  28. exit 0
  29. else
  30. echo "Failed to clone from $mirror_url (attempt $i)"
  31. fi
  32. done
  33. done
  34. echo "Failed to clone the repository after multiple attempts. Please check your network connection and try again later."