1. Print message with a time and type.
2. Colorful prints
3. Remove color code
4. Variable in variable name (indirect reference)
5. Report system statistics
6. Regex
1. Print message with a time and type.
function msg { msgtype=$1 shift echo `date +"%Y%m%d %H:%M:%S"` $msgtype - "$@"}# stay in line while displaying, useful for count downfunction msgInLine { msgtype=$1 shift echo -n -e "\r"`date +"%Y%m%d %H:%M:%S"` $msgtype - "$@"}2. Colorful prints
#!/bin/bash INVT="\033[7m"; NORM="\033[0m"; BOLD="\033[1m"; BLINK="\033[5m"; UNDR="\033[4m"; BLACK_F="\033[30m"; BLACK_B="\033[40m";RED_F="\033[31m"; RED_B="\033[41m"; GREEN_F="\033[32m"; GREEN_B="\033[42m"; YELLOW_F="\033[33m"; YELLOW_B="\033[43m";BLUE_F="\033[34m"; BLUE_B="\033[44m"; MAGENTA_F="\033[35m"; MAGENTA_B="\033[45m";CYAN_F="\033[36m"; CYAN_B="\033[46m"; WHITE_F="\033[37m"; WHITE_B="\033[47m";clear echo -e "+=============================================+" echo -e "| ${BOLD}${CYAN_F}Batch 13 Deployment Script${NORM} |" echo -e "+=============================================+" echo -e "|${BOLD}Component Revision${NORM} |" 3. Remove color code
If you do not remove the color code, in certain terminal, you will have below error:$ for i in `ls`; do cp $i ${i}_bak; donecp: cannot stat `\033[0m\033[01;32mwebinterface0\033[0m': No such file or directorycp: cannot stat `\033[01;32mwebinterface1\033[0m': No such file or directorySo, before we copy the file, we should remove the color code from the original string:for i in `ls`; do echo $i; j=`echo $i | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"`; echo $j; cp $j ${j}_bak; done4. Variable in variable name (indirect reference)
for j in TSEC OTC ESTT KWAY; do INF_NAME=INF_NAME_"$j" OUF_NAME=OUF_NAME_"$j" msg INFO "Input File Name: $KWAY_INF/${!INF_NAME} msg INFO "Output File : $KWAY_OUF/${!OUF_NAME}done5. Report system statistics (sample: memory)
/usr/bin/sar -r | egrep ':00:'
6. Regex - note, don't put "" around your regex expression. Batch 3.2 has this update.
p_yyyymmdd='^(19|20)[0-9]{6}$'if [[ ! "$yyyymmdd" =~ $p_yyyymmdd ]]; then result="USAGE - yyyymmdd format"fi