翼度科技»论坛 云主机 LINUX 查看内容

用于替换程序内多个文件的shell脚本

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
  1. #!/bin/bash
  2. # Author: [王思扬]
  3. # Description: [Used to replace multiple files in the program.]
  4. start_time=$(date +%s)
  5. # Directory for storing new files, backup files and program directory
  6. NewFileDir=/home/test/new/
  7. BacKFileDir=/home/test/bak/
  8. ProDir=/home/test/program
  9. # Set colors for text
  10. GREEN='\033[0;32m'
  11. YELLOW='\033[1;33m'
  12. RED='\033[0;31m'
  13. NC='\033[0m' # No Color
  14. # Backup function
  15. backup() {
  16.   # Create backup directory with current date
  17.   mkdir -p $BacKFileDir$(date +%F) &>/dev/null
  18.   echo -e "${GREEN}Backup directory created successfully.${NC}"
  19.   # Copy each new file to backup directory
  20.   for i in $(ls $NewFileDir); do
  21.     cp -f $(find $ProDir -name $i) $BacKFileDir$(date +%F) &>/dev/null
  22.     echo -e "${GREEN}File $i backed up successfully.${NC}"
  23.   done
  24. }
  25. # Update function
  26. update() {
  27.     files=($(ls $NewFileDir))
  28.     for i in "${files[@]}"; do
  29.         paths=($(find $ProDir -name $i))
  30.         for path in "${paths[@]}"; do
  31.             cp -f $NewFileDir$i $path
  32.             if [ $? -eq 0 ]; then
  33.                 echo -e "${GREEN}File $i updated successfully.${NC}"
  34.             fi
  35.         done
  36.     done
  37. }
  38. # Rollback function
  39. rollback() {
  40.   # Find backup directory with current date
  41.     backupDir=$BacKFileDir$(date +%F)
  42.     if [ -d $backupDir ]; then
  43.       for i in $(ls $backupDir); do
  44.         find $ProDir -name $i | while read path; do
  45.           cp -f $backupDir/$i $path
  46.           if [ $? -eq 0 ]; then
  47.             echo -e "${GREEN}File $i rolled back successfully.${NC}"
  48.           fi
  49.         done
  50.       done
  51.     fi
  52. }
  53. # Main function
  54. while :; do
  55.   echo -e "${YELLOW}Please select an operation:${NC}"
  56.   echo -e "${YELLOW}  1. Backup${NC}"
  57.   echo -e "${YELLOW}  2. Update${NC}"
  58.   echo -e "${YELLOW}  3. Rollback${NC}"
  59.   echo -e "${YELLOW}  4. Quit${NC}"
  60.   read -r -p "$(echo -e ${YELLOW}Enter your choice:${NC} )" choice
  61.   case $choice in
  62.   1)
  63.     backup
  64.     ;;
  65.   2)
  66.     update
  67.     ;;
  68.   3)
  69.     rollback
  70.     ;;
  71.   4)
  72.     echo -e "${YELLOW}Goodbye!${NC}"
  73.     exit 0
  74.     ;;
  75.   *)
  76.     echo -e "${RED}Invalid choice, please try again.${NC}"
  77.     ;;
  78.   esac
  79. done
  80. end_time=$(date +%s)
  81. echo "Script completed in $(($end_time - $start_time)) seconds."
复制代码
来源:https://www.cnblogs.com/jsln/p/17175500.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具