#! /bin/bashHELP_MESSAGE(){echo"Usage: backupWithDate.sh [OPTIONS]"echo"Options:"echo" -h, --help Show this help message and exit"echo" -v, --version Show the version of the script"echo" -a PATH, --absolute PATH Traverse the txt file in the absolute path"echo" -b PATH, --backup PATH The path of the txt file backup"exit0}VERSION_MESSAGE(){echo"backupWithDate.sh version 1.0"exit0}ABSOLUTE(){DATE_TIME=`date +%Y%m%d%H%M`forfin`find $ABSOLUTE_PATH -type f -name"*.txt"`doif["$HAVE_BACKUP"-eq1];thenif[[$f== *.txt ]];thenFILENAME=`basename"$f"`echo"backup txt file: from $f to ${BACKUP_PATH}/${FILENAME}_${DATE_TIME}"cp${f}${BACKUP_PATH}/${FILENAME}_${DATE_TIME}fielseecho"backup txt file: from $f to ${f}_${DATE_TIME}"cp${f}${f}_${DATE_TIME}fidone}CHECK(){if[["$1"!= /* ]];thenecho"Option should be an absolute path"exit1fiif[!-e"$1"];thenecho"$1 does not exist"exit1fi}if[[$#-eq0||"$1"=="-h"||"$1"=="--help"]];then
HELP_MESSAGE
fiif[[$#-eq0||"$1"=="-v"||"$1"=="--version"]];then
VERSION_MESSAGE
fiABSOLUTE_PATH=""BACKUP_PATH=""HAVE_BACKUP=0for((i=1;i<=$#;i++));dokey="$1"case$keyin
-a|--absolute)ABSOLUTE_PATH="$2"shift;;
-b|--backup)BACKUP_PATH="$2"HAVE_BACKUP=1shift;;
*)echo"Invalid option: $key";;esacshiftdoneif[-z"$ABSOLUTE_PATH"];thenecho"Absolute path is not set"exit1fi
CHECK "$ABSOLUTE_PATH"if[-n"$BACKUP_PATH"];then
CHECK "$BACKUP_PATH"fi
ABSOLUTE