install.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. function cd_dir {
  3. local dir_path=$1
  4. # 检查目录是否存在
  5. if [ ! -d "$dir_path" ]; then
  6. echo "Directory $dir_path does not exist. Creating..."
  7. mkdir -p "$dir_path"
  8. fi
  9. cd "$dir_path" || exit
  10. }
  11. # 定义一个函数,用于执行安装脚本
  12. execute_install_script() {
  13. if [ -e "install.sh" ] && [ -x "install.sh" ]; then
  14. echo "Executing install.sh in $PWD ..."
  15. chmod +x install.sh
  16. # ./install.sh
  17. fi
  18. }
  19. # 安装包检测
  20. function check_and_install {
  21. for package_name in "$@"
  22. do
  23. if ! dpkg -l $package_name &> /dev/null
  24. then
  25. echo "$package_name not found. Installing..."
  26. sudo apt-get update
  27. sudo apt-get install $package_name -y
  28. else
  29. echo "$package_name already installed."
  30. fi
  31. done
  32. }
  33. # 定义函数,根据路径执行install.sh脚本
  34. run_install() {
  35. echo "------------------Installing $1------------------"
  36. now_path=$(pwd)
  37. dir="$1" # 获取install.sh所在的目录名
  38. sh_name=$2
  39. cd "$dir" || exit # 进入该目录
  40. ./"$sh_name" # 执行配置的安装脚本
  41. cd "$now_path" || exit # 回到上一级目录
  42. }
  43. check_and_install g++ gcc make autoconf automake libunwind-dev libpcl-dev libopencv-dev libpcap-dev
  44. # 逐行读取文件内容,根据=后面的值执行相应操作
  45. while read line
  46. do
  47. var=$(echo "$line" | cut -d ':' -f 2) # 提取=后面的值
  48. if [ "$var" = "ON" ]
  49. then
  50. key=$(echo "$line" | cut -d ':' -f 1) # 提取=前面的键
  51. sh_name=$(echo "$line" | cut -d ':' -f 3) # 提取=前面的键
  52. if [ -d "$key" ]
  53. then
  54. run_install "$key" "$sh_name" # 根据路径执行install.sh脚本
  55. else
  56. echo "dir not exist:$key"
  57. fi
  58. elif [ "$var" != "" ]
  59. then
  60. echo "Unknow status: $var"
  61. fi
  62. done < config.txt
  63. exit