2021-09-10 18:18:20 +08:00

56 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
cat <<-EOF
#!/bin/bash
#=========================================
# Author : Colben
# Create : $(date +"%F %R")
#=========================================
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
PID_FILE=/tmp/\$(basename \${0%.sh}).pid
if [ -t 0 ]; then
function Print { echo -e "\033[32;1m\$(date +'[%F %T]') \$*\033[0m"; }
function Warn { echo -e "\033[33;1m\$(date +'[%F %T]') \$*\033[0m"; }
function Error { echo -e "\033[31;1m\$(date +'[%F %T]') \$*\033[0m"; exit 1; }
else
#exec &> \${0%.sh}.out
function Print { echo -e "\$(date +'[%F %T] INFO') \$*"; }
function Warn { echo -e "\$(date +'[%F %T] WARN') \$*"; }
function Error { echo -e "\$(date +'[%F %T] ERRRO') \$*"; exit 1; }
fi
function Quit {
local exitCode=\$?
rm -f \$PID_FILE
[ 0 -ne \$exitCode ] && Error Failed to xxxx!
[ -z "\${END:-}" ] && echo && Error Interrupted manually!
Print Succeeded to xxxx.
}
function Func1 {
Print Func1 ...
}
function Func2 {
Print Func2 ...
}
function Main {
[ -e "\$PID_FILE" ] && Error Pid file \$PID_FILE already exists, quit!
echo \$\$ > \$PID_FILE
Print Main ...
Func1
Func2
END=1
}
# Start here
Main
EOF