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

#!/bin/bash
# Author: [王思扬]
# Description: [Used to replace multiple files in the program.]

start_time=$(date +%s)
# Directory for storing new files, backup files and program directory
NewFileDir=/home/test/new/
BacKFileDir=/home/test/bak/
ProDir=/home/test/program


# Set colors for text
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Backup function
backup() {
  # Create backup directory with current date
  mkdir -p $BacKFileDir$(date +%F) &>/dev/null
  echo -e "${GREEN}Backup directory created successfully.${NC}"

  # Copy each new file to backup directory
  for i in $(ls $NewFileDir); do
    cp -f $(find $ProDir -name $i) $BacKFileDir$(date +%F) &>/dev/null
    echo -e "${GREEN}File $i backed up successfully.${NC}"
  done
}

# Update function
update() {
    files=($(ls $NewFileDir))
    for i in "${files[@]}"; do
        paths=($(find $ProDir -name $i))
        for path in "${paths[@]}"; do
            cp -f $NewFileDir$i $path
            if [ $? -eq 0 ]; then
                echo -e "${GREEN}File $i updated successfully.${NC}"
            fi
        done
    done

}

# Rollback function
rollback() {
  # Find backup directory with current date
    backupDir=$BacKFileDir$(date +%F)
    if [ -d $backupDir ]; then
      for i in $(ls $backupDir); do
        find $ProDir -name $i | while read path; do
          cp -f $backupDir/$i $path
          if [ $? -eq 0 ]; then
            echo -e "${GREEN}File $i rolled back successfully.${NC}"
          fi
        done
      done
    fi
}

# Main function
while :; do
  echo -e "${YELLOW}Please select an operation:${NC}"
  echo -e "${YELLOW}  1. Backup${NC}"
  echo -e "${YELLOW}  2. Update${NC}"
  echo -e "${YELLOW}  3. Rollback${NC}"
  echo -e "${YELLOW}  4. Quit${NC}"
  read -r -p "$(echo -e ${YELLOW}Enter your choice:${NC} )" choice

  case $choice in
  1)
    backup
    ;;
  2)
    update
    ;;
  3)
    rollback
    ;;
  4)
    echo -e "${YELLOW}Goodbye!${NC}"
    exit 0
    ;;
  *)
    echo -e "${RED}Invalid choice, please try again.${NC}"
    ;;
  esac
done

end_time=$(date +%s)
echo "Script completed in $(($end_time - $start_time)) seconds."


热门相关:地球第一剑   公关女孩儿   寂静王冠   豪门闪婚:帝少的神秘冷妻   别那么骄傲