现在的位置: 首页 > 综合 > 正文

shell脚本 操作日志文件 && 读取文件 && 查询进程ID

2017年09月11日 ⁄ 综合 ⁄ 共 1935字 ⁄ 字号 评论关闭

1、查询30天前的日志文件并删除

find /g2ihslog/xmlgen/ -mtime +30 –exec rm {} \;

    通过文件名中包含的日期查询:

#文件中包含的日期字符串
DEL_DAY=`date -I -d '-30 day'`"-00"
#日志文件夹
logdir="/g2ihslog/*/"
#遍历文件夹下的文件
for n in `ls $logdir` ; do
    #取出文件名中的日期
    file_dt=`echo $n | awk -F "." '{print $3}'`
    #若日期为30天前,则删除
    if [[ -n "$file_dt" && "$file_dt" < "$DEL_DAY" ]];then
        rm -f $n
    fi;
done

2、读取文件,对每一行进行处理

      linux shell逐行处理文本的12种方法

 
方法1、cat $filename|while read line 
   例如:
function while_read_line 
         { cat $filename|while read line 
           do 
              echo "$line" 
             :                        #这行什么都不做,返回值0 ,在此处对变量进行的操作不起作用,出了while循环就失效
             done
          } 

 

方法2:while read $filename from bottom  #建议采用此方法读取
   例如:
function while_read_line_bottom 
         { 
           while read line 
          do 
            echo "$line" 
               : 
            done < $filename 
          } 

 

方法3:while_line_line_bottom 
  例如:
function while_line_line_bottom 
          {  
           while line line   #用line命令替换read 
            do 
              echo "$line" 
               : 
             done < $filename 
            } 

 

方法4:cat $filename|while line=`line` 
   例如:
function cat_while_line_line 
        { 
          cat $filename | while line=`line` 
            do 
              echo "$line" 
            : 
           done 
} 

 

方法5:cat $filename |while line line 
   例如:
function while_line_LINE 
        {  
        cat $filename |while line line 
         do 
           echo "$line" 
          : 
          done 
           } 

 

方法6:while line=`line`from the bottom 
   例如:
 function while_line_line_bottom 
        { 
        while line=`line` 
         do 
          echo "$line" 
        : 
        done < $filename 
       } 

 

方法7:cat $filename |while line=$(line) 
   例如:
 function while_line_line_cm 
         { 
          cat $filename |while line=$(line) 
          do 
           echo "$line" 
          : 
              done 
           } 

 

方法8:while line=$(line)from the bottom 
   例如:
function while_line_line_bottom_cm 
        { 
        while line=$(line) 
         do 
         echo "$line" 
       done<$filename 
        } 

 

方法9:while read line 
    例如:
 function while_read_line_fd 
        {    
          exec 3<&0   #将所有内容重定向到新文件描述符3来关闭文件描述符0 
          exec 0<$filename  #标准输入文件描述符为0,标准输出文件描述符为1,标准错误为2. 
         while read line    #3以后就可以配给普通文件。 
         do 
           echo "$line" 
          done 
          exec 0<&3 
       } 

方法10:while line=`line` 
   例如:
function while_line_line_fd 
        { 
         exec 3<&0 
         exec 0<$filename 
         while line=`line` 
         do 
          echo "$line" 
         done 
          exec 0<&3 
         } 

 

方法11:while line=$(line) 
  例如:
 function while_line_line_cm_fd 
       { 
        exec 3<&0 
        exec 0<$filename 
        while line=$(line) 
         do 
          print "$line" 
         done 
        exec 0<&3 
         } 

 

方法12:while line line 
   例如:
function while_line_line_fd 
      { 
       exec 3<&0 
       exec 0<$filename 
       while line line 
        do 
          echo " $line" 
        done 
       exec 0<&3 
       }

3、查看进程ID

#-v 表示排除 -i表示忽略大小写
ps -ef | grep -v grep | grep -i ihs-${name} | awk '{print $2}'

抱歉!评论已关闭.