一、压缩指定目录下的文件并删除原文件
#!/bin/bash
ZIP_DAY=7
function zip {
local dir=$1
if [ -d $dir ];then
local file_num=`ls -l $dir |grep "^d"|wc -l`
if [ $file_num -ge $ZIP_DAY ];then
local tar_name=`ls -lt $dir | grep -v *.tar.gz | grep -v total | head -n 1 |awk '{print $9}'`
cd $dir
tar -czvPf $tar_name.tar.gz * --exclude=*.tar.gz >/dev/null 2>&1
if [[ $? -eq 0 ]];then
find $backup_dir/* -type d -exec rm -rf {} ; >/dev/null 2>&1
else
echo "失败"
fi
fi
fi
}
二、过滤文件夹下不需要的文件
#!/bin/bash
filter_array=()
all=`ls $dir | grep .log` //目录下所有的文件
gv=`ls -lt $dir | grep .log | awk '{print $9}' | head -5` //留前五条记录
function filter_array {
declare -a result_list
t=0
flag=0
all_array=(${all// / })
gv_array=(${gv// / })
for a in ${all_array[@]}
do
for g in ${gv_array[@]}
do
if [ "$a" == "$g" ]; then
flag=1
fi
done
if [ $flag -eq 0 ]; then
result_list[t]=$a
t=$((t+1))
else
flag=0
fi
done
filter_array=${result_list[*]}
}
三、远程服务器执行命令
#!/bin/bash
read -p "输入要连接的远程服务器IP:" IP
ssh root@$IP > /dev/null 2>&1 <<EOF
cd /usr/local
ls
exit
EOF
四、centos 7 流量转发
#!/bin/bash
function add {
firewall-cmd --zone=trusted --add-forward-port=port="本地端口":proto=tcp:toaddr= "目标主机ip":toport="目标主机端口" --permanent
firewall-cmd --reload
firewall-cmd --zone=trusted --list-all
}
function remove {
firewall-cmd --zone=trusted --remove-forward-port=port="本地端口":proto=tcp:toaddr="目标ip":toport="目标主机端口" --permanent
firewall-cmd --reload
firewall-cmd --zone=trusted --list-all
}
五、循环检测进程是否被杀
#!/bin/bash
times=10
while(( "$times > 0" ))
do
pid=`ps aux | grep "nginx" | awk '{print $2}'`
if [[ -z $pid ]] ; then
return 0
fi
sleep 1
let "times--"
done