install 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. SCRIPT_PATH="$(pwd)"
  3. echo "$SCRIPT_PATH"
  4. # 进入某个路径,如果文件夹不存在,创建
  5. function cd_dir {
  6. local dir_path=$1
  7. # 检查目录是否存在
  8. if [ ! -d "$dir_path" ]; then
  9. echo "Directory $dir_path does not exist. Creating..."
  10. mkdir -p "$dir_path"
  11. fi
  12. cd "$dir_path" || exit
  13. }
  14. # 安装包检测
  15. function check_and_install {
  16. for package_name in "$@"
  17. do
  18. if ! dpkg -l $package_name &> /dev/null
  19. then
  20. echo "$package_name not found. Installing..."
  21. sudo apt-get update
  22. sudo apt-get install $package_name -y
  23. else
  24. echo "$package_name already installed."
  25. fi
  26. done
  27. }
  28. # 克隆代码
  29. function git_clone {
  30. local repo_url=$1
  31. local target_dir=$2
  32. local max_attempts=3
  33. echo "Cloning $repo_url to $target_dir..."
  34. # 初始化计数器
  35. attempt=1
  36. # 循环尝试克隆
  37. while [ $attempt -le $max_attempts ]; do
  38. # 如果目标目录存在,则删除它
  39. if [ -d "$target_dir" ]; then
  40. echo "Clone successful!"
  41. return
  42. fi
  43. # 克隆代码
  44. git clone "$repo_url" "$target_dir"
  45. # 检查克隆是否成功
  46. if [ $? -eq 0 ]; then
  47. echo "Clone successful!"
  48. # 如果克隆成功,则退出循环
  49. break
  50. else
  51. rm -rf "$target_dir"
  52. echo "Clone attempt $attempt failed."
  53. attempt=$((attempt+1))
  54. if [ $attempt -gt $max_attempts ]; then
  55. echo "Max attempts reached. Continue..."
  56. else
  57. echo "Retrying in 5 seconds..."
  58. sleep 5
  59. fi
  60. fi
  61. done
  62. }
  63. # install env
  64. cd "$SCRIPT_PATH" || exit
  65. cd_dir build
  66. cmake ..
  67. max_retries=3 # 设置最大尝试次数为 3 次
  68. retry_count=0
  69. while [ $retry_count -lt $max_retries ]
  70. do
  71. sudo make install
  72. if [ $? -eq 0 ] # 判断 make 命令的返回值是否为 0,即成功
  73. then
  74. echo "Build succeeded!"
  75. exit 0
  76. else
  77. echo "Build failed! Retrying in 3 seconds..."
  78. retry_count=$((retry_count+1))
  79. sleep 3 # 等待 3 秒再次尝试编译
  80. fi
  81. done
  82. echo "Failed to build after $max_retries retries."
  83. exit 1