用于替换程序内多个文件的shell脚本
#!/bin/bash# Author: [王思扬]
# Description:
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."
来源:https://www.cnblogs.com/jsln/p/17175500.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]