设为首页 加入收藏

TOP

用于替换程序内多个文件的shell脚本
2023-07-23 13:40:04 】 浏览:23
Tags:于替换 程序内 文件的 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."


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇干货!超实用的 Linux 初始化脚本 下一篇聊聊GPU与CPU的区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目