生产实践:
用户终端操作历史记录文件格式转换
学习技巧:
if语句、for循环及sed、awk使用
脚本内容:
#/bin/bash ############################################################ # $Name: conv_hist # $Version: v1.0 # $Function: convert history file # $Author: Zhihe An # $Copyright (c) https://chegva.com # $Create Date: 2017-07-24 ############################################################ RED='\e[1;31m' redb='\e[5;41m' # 红底闪烁 NC='\e[0m' # 没有颜色 LOGNAME=$1 #BASEPATH=$(cd `dirname $0`; pwd) BASEPATH=$(basename `pwd`) if [ "$1" == "" -o "$#" -gt 1 ] then echo -e "${RED} :) ${NC} Usage: ./`basename $0` [HISTFILE name]"; exit; elif [ ! -f "$LOGNAME" ];then echo -e "${RED} :( ${NC} ${redb} ${LOGNAME} ${NC} does not exist,please check it!"; exit; else echo "================== chegva.com `date +%F\ %T` ===================="; fi #elif [ ! -f "${LOGNAME}" ] # echo "${LOGNAME} does not exist,please check it!"; # exit; #LOGFILE=${LOGNAME}.his #if [ ! -f "$LOGFILE" ]; then # touch ${LOGFILE} #else # >${LOGFILE} #fi # for line in $(<$LOGNAME) do if [[ `echo ${line:0:1}` == "#" ]] then #echo $line LOGTIME=`echo ${line:1}` #echo $LOGTIME LOGLINE=`date -s @${LOGTIME} +'%F %T'` #echo $LOGLINE sed -i "s/$line/$LOGLINE/" ${LOGNAME} fi done awk -v count=1 '{ if (NR%2==1) { print count" '"$BASEPATH"' "$0;count++;} else {print "\033[1;31m" " " $0 "\033[0m"}}' ${LOGNAME} echo "===================" "历史命令记录格式已转换:)" "=========================" exit 0
◎查看效果
◎记录所有登录用户终端操作命令:https://chegva.com/2332.html