1、在当前主机编写脚本文件history_max.sh显示主机中执行频率最高的前5个命令。
history | tr -s ' ' >history_file.test1
[root@node1 shell_code]# vim top_5_commands.sh
#!/bin/bash
cut -d ' ' -f 3 history_file.test1 > history_file.test2
sort history_file.test2 | uniq -c | sort -n | tail -5
2、判断主机是否存在rhel用户,如果存在则设置密码为redhat,如果不存在则创建用户并设置密码。
[root@node1 shell_code]# vim judge_user.sh
#!/bin/bash
id $1 &> /dev/null
if [ $? -eq 0 ]
then
echo $1 is exist
echo 'redhat' | passwd --stdin $1 &> /dev/null
else
useradd $1
echo 'redhat' | passwd --stdin $1 &> /dev/null
fi
[root@node1 shell_code]# bash judge_user.sh rhel
rhel is exist
3、通过设置变量HISTTIMEFORMAT,使得当执行history命令时输出格式如下:[2022-12-25 16:53:42][root]history
HISTTIMEFORMAT="[%F %T][`whoami`]"