shell脚本 回顾 小练习

1.把/OPT目录下(包含子目录)下所有后缀为“.sh”的文件后缀变更为“.shell”
2.将A、B、C目录下的文件A1、A2、A3文件改名为A4、A5、A6
3.如何在vi模式下将文件中的aa字符串批量改成bb

 

1.

#!/bin/bash
dir=/hanzhao
files=`find /hanzhao/ -name '*.sh'`
for file in $files
do
# echo $file
 filename=${file%.*}
# echo $filename
 mv $file ${filename}.shell
done

2.

#!/bin/bash
    file=`ls /[ABC]/A[123]`
    for i in $file        
        do
        num=${i##*A}
        let num1=num+3 
       # echo $num1
       # num2=${i%A*} 
       # echo $num2 
        mv $i ${i%A*}A$num1
    done

3.

直接用vi打开文件,之后再读模式下 直接输入 :%s#aa#bb#g

 4备份每天的日志

#!/bin/bash
cp /opt/lampp/logs/access_log /opt/bak_access_log-`date +%Y%m%d%H%M`
>/opt/lampp/logs/access_log

 5.启动停止 lampp

#!/bin/bash
start=0
pidnum=`ps aux|grep "/opt/lampp/*"|wc -l`
echo ${pidnum}
if [ ${pidnum} -gt 1 ]
then 
echo 'lampp is start'
read -p 'Do you wish to stop lampp? y or n:  ' yn
case $yn in
[Yy]* )start=1;;
[Nn]* )exit;;
 * )echo 'Please answer Y or N'
exit;;
esac
else
echo 'lampp is stop,doing start'
read -p 'Do you wish to start lampp? y or n:  ' yn
case $yn in
[Yy]* )start=0;;
[Nn]* )exit;;
 * )echo 'Please answer Y or N'
exit;;
esac
fi

if [ ${start} -eq 1 ]
then ps aux|grep "/opt/lampp/*"|grep -v "grep"|awk '{print $2}'|xargs kill -9
echo 'lampp is stop'
else 
/opt/lampp/lampp start
echo 'lampp is start'
fi

 

 5.根据进程号查询启动路径

#!/bin/bash
pid=`ps -ef|grep "httpd"|grep -v "grep"|awk '{print $2}'`
echo ${pid}
for every_pid in ${pid}
do 
exe_path=`ls -l /proc/${every_pid}|grep "exe ->"|awk '{print $NF}'`
echo "exe_path:"${exe_path}
done

 6.杀死 进程

ps -ef |grep "httpd"|grep -v "grep"|awk '{print$2}'|xrags kill -9

 7.条件查询文件

awk -F","  '{if($1==1||$2>34&&....)print}'  a.txt

 8.awk 求平均值:

awk '{NR>1}BEGIN{sum=0;num=o}{if($1==2){sum+=$2;num+=1}}END{print(sum/num)}' hanzhao

 

posted @ 2016-11-24 23:06  明镜止水丨  阅读(655)  评论(0编辑  收藏  举报