在做日志服务时,经过遇到百个1G以上的文件需要及时删除,暂不考虑lograte滚动日志服务来解决。
先回顾常用命令:
三种数字循环--带次数控制的for循环
##从0开始,步长为2,到10终止------三种格式。
for (( c=0; c<=10; c+=2 )); do echo "Welcome $c times"; done
for i in $(seq 0 2 10); do echo "Welcome $i times"; done
for i in {0..10..2}; do echo "Welcome $i times"; done
#下面是默认步长,为1.
for (( c=0; c<=10; c++ )); do echo "Welcome $c times"; done
for i in $(seq 0 10); do echo "Welcome $i times"; done
for i in {0..10}; do echo "Welcome $i times"; done
for应用的实际例子
1、查看网卡和对应的ip地址信息:
for dev in $(awk -F : '{if(NR>2)print $1}' /proc/net/dev);do printf '%+10s: ' $dev;echo $(ip addr show $dev | awk -F "[ /]+" '/inet /{print $3}'); done
2、查看网卡和对应的MAC地址:
for dev in $(awk -F : '{if(NR>2)print $1}' /proc/net/dev);do printf '%+10s: ' $dev;echo $(ip addr show $dev | awk -F "[ /]+" '/inet /{print $3}') $(ip addr show $dev | awk -F "[ /]+" '/ether /{print $4}'); done
3、关闭3模式下所有服务:
for server in `chkconfig --list | grep 3:on | awk ‘{print $1}’ `; do chkconfig --level 3 $server off ; done
4、10次把日志放到文件中:
for num in `seq 1 10`;do echo $num;cat messages >> test01; done
5、把message日志文件清空:
for VF in `ls /var/log/ | grep message`; do echo 0> /var/log/$VF ; done
6、磁盘热加载--虚机
for i in $(ls /sys/class/scsi_host/); do echo '- - -' > /sys/class/scsi_host/$i/scan; done
7、磁盘热加载--物理主机
for i in $(ls /sys/class/scsi_host/); do echo '- - -' > /sys/class/scsi_host/$i/scan; done
常见简单的判断--不一定是if结构哦
1、与或结构:
$(ip add show dev ens160) && echo "have ens160" || echo "no ens160" ##测试有无ens160网卡,如果有输出have ,如果没有输出no
aa=" ";[[ -z "$aa" ]] && echo "Empty" || echo "Not empty" #判断是否为空
又例如:
[ -o /etc/passwd ] && echo "/etc/passwd is executable" || echo "/etc/passwd NOT is executable" ##返回/etc/passwd NOT is executable
[ -r /etc/passwd ] && echo "/etc/passwd is readable" || echo "/etc/passwd NOT is readable" ##返回/etc/passwd is readable
命令由来:
##以下通过系统中的实例要验证
[localhost@root~]# ll /etc/passwd
-rw-r--r-- 1 root root 1354 Mar 20 11:20 /etc/passwd
[localhost@root~]# [ -o /etc/passwd ]
[localhost@root~]# echo $?
1
[localhost@root~]# [ -o /etc/passwd ] && echo "/etc/passwd is executable"
[localhost@root~]# echo $?
1
[localhost@root~]# [ -o /etc/passwd ] || echo "/etc/passwd NOT is executable"
/etc/passwd NOT is executable
[localhost@root~]# echo $?
0
[localhost@root~]# [ -o /etc/passwd ] && echo "/etc/passwd is executable" || echo "/etc/passwd NOT is executable"
/etc/passwd NOT is executable
[localhost@root~]# echo $?
0
[localhost@root~]# ll /etc/passwd
-rw-r--r-- 1 root root 1354 Mar 20 11:20 /etc/passwd
[localhost@root~]# [ -r /etc/passwd ]
[localhost@root~]# echo $?
0
[localhost@root~]# [ -r /etc/passwd ] && echo "/etc/passwd is readable"
/etc/passwd is readable
[localhost@root~]# echo $?
0
[localhost@root~]# [ -r /etc/passwd ] || echo "/etc/passwd NOT is readable"
[localhost@root~]# echo $?
0
[localhost@root~]# [ -r /etc/passwd ] && echo "/etc/passwd is readable" || echo "/etc/passwd NOT is readable"
/etc/passwd is readable
[localhost@root~]# echo $?
0
总结:
1、&& 表示,&&前后均为真,整体才为真;若任意一边为假,整体则为假;
换句话说:&&前为真 才执行&&后;否则,&&前为假 直接异常退出。
2、|| 表示, || 前后均为假,整体才为假;若任意一边为真,整体则为真;
换句话说:只要||前为真 就不执行||后;否则,||前为假 才执行||后。
3、通过以上规律,结合if [条件];then 真,执行真;else 假,执行假 ;fi,得出上述结构的简单命令;
&& 如果是“前面”,则“后面”
|| 如果不是“前面”,则后面
2、if结构
格式:if [条件];then 真,执行真;else 假,执行假;fi
[root@pyramid0 ~]# [ -r /etc/passwd ] && echo "/etc/passwd is readable" || echo "/etc/passwd NOT is readable"
/etc/passwd is readable
[root@pyramid0 ~]# echo $?
0
[root@pyramid0 ~]# if [ -r /etc/passwd ];then echo "/etc/passwd is readable";else echo "/etc/passwd NOT is readable";fi
/etc/passwd is readable
[root@pyramid0 ~]# echo $?
0
[root@pyramid0 ~]# [ -o /etc/passwd ] && echo "/etc/passwd is executable" || echo "/etc/passwd NOT is executable"
/etc/passwd NOT is executable
[root@pyramid0 ~]# echo $?
0
[root@pyramid0 ~]# if [ -o /etc/passwd ];then echo "/etc/passwd is executable";else echo "/etc/passwd NOT is executable";fi
/etc/passwd NOT is executable
[root@pyramid0 ~]# echo $?
0
后续:函数应用
#!/bin/bash
os_multipath_check (){
proc_num=$(ps -ef | grep multipathd | grep -v grep | wc -l)
#proc_num=`ps -ef | grep multipathd | grep -v grep | wc -l`
if [ "${proc_num}" -gt 0 ];then
echo "service_status=on"
else
echo "service_status=off"
exit 0
fi
}
os_multipath_check
3、结合look
[ttmonitor007@~]$aa=" ";[[ -z "$aa" ]] && echo "Empty" || echo "Not empty"
Not empty
[ttmonitor007@~]$echo $?
0
[ttmonitor007@~]$aa=" ";if [ -z "$aa" ] ;then echo "Empty";else echo "Not empty";fi
Not empty
[ttmonitor007@~]$echo $?
0
[ttmonitor007@~]$aa=" ";if test -z "$aa";then echo "Empty";else echo "Not empty";fi
Not empty
[ttmonitor007@~]$echo $?
0
快速清空日志文件
从资源消耗和处理时长看,重定向清空比删除命令更“经济有效”。
使用for批量执行:for TF in $(ls | grep test);do echo > $TF;done