zoukankan      html  css  js  c++  java
  • 17_8_4 shell 加法器+ 递归循环打印 + 查找指定路径下的 包含 给定关键字的 的所有文件 +txt 至统一文件夹 并且 屏蔽grep的无效错误输出

    加法器:

     #!/bin/bash
            add(){
            k=0 
            for a in $*
            do      
            k=`expr $a + $k`
            done
            return $k
            }
            add $*
            echo $?
    

    递归循环打印

         #!/bin/bash
                   show(){
                   for i in `ls $1`
                   do
                      if [ -f $1/$i ] #a.txt不算,只有/Users/rimi/Desktop/a.txt才算
                      then
                      echo "$i"
                      elif [ -d $1/$i ]
                      then
                      echo $1/$i
                      show $1/$i
                      fi
              done
      }
      show $1
    
    

    查找指定路径下的 包含 给定关键字的 的所有文件

     #!/bin/bash
                   show(){
                    k=$(find $1)
                   for i in $k
                   do
            grep -n --color=auto $2 $I >~/t.txt > /dev/null 2>&1     #command > /dev/null 2>&1 屏蔽grep的 无效错误——***is notfile     非常重要   或者 grep -s 参考 博客:17_8_7 shell 指令:grep + find
            if [ $? -eq 0 ]
            then
                    echo $i
            fi
              done
      }
      show $1 $2
    
    
    

    查找并移动到指定目录

     #!/bin/bash
                   show(){
                    tem="/cygdrive/c/Users/Adminstrator/Desktop/temp/"
                    if [ ! -d $tem ]
                    then
                    mkdir $tem
                    fi
                    k=$(find $1)
                   for i in $k
                   do
            grep -n --color=auto $2 $i
            if [ $? -eq 0 ]
            then
                    echo $i
                    mv $i $tem
            fi
              done
      }
      show $1 $2
    
    

    移动所有txt 至统一文件夹

     #!/bin/bash
                   show(){
            k=$(find $1 -name "*.txt")
            for i in $k
            do
                    if [ -f $i ]
                    then
                    mv $i /cygdrive/c/Users/Adminstrator/Desktop/temp/
                    fi
            done
    
      }
      show $1
    
    
    

    多个参数查找

    # 一个参数:关键字   ,当前路径穿件 copy文件
      2 #2个参数:关键字   指定存放 ,当前路径
      3 #3各参数;关键字   指定存放   寻找路径
      4 
      5 s(){
      6         echo “操作开始...”
      7         if [ $# -eq 1 ]
      8         then
      9                 if [ ! -d ./copy ]
     10                 then
     11                 mkdir ./copy
     12                 echo "没有copy的文件夹,正在创建..."
     13                 else
     14                 echo "当前目录已存在copy文件夹..."
     15                 fi
     16         k=$(find ./)
     17         for file in $k
     18         do
     19         grep -n -s $1 $file
     20                 if [ $? -eq 0 ]
     21                         then
     22                         cp $file ./copy
     23                 fi
     24         done
     25         fi
     26 
     27         if [ $# -eq 2 ]
     28         then
     29                 k=$(find ./)
     30                 echo $k
     31                 for file in $k
     32                 do
     33                         grep -n -s $1 $file
     34                         if [ $? -eq 0 ]
     35                         then
     36                         cp $file $2
     37                         fi
     38                 done
     39         fi
     40 
     41         if [ $# -eq 3 ]
     42         then
     43                 k=$(find $3)
     44                 for file in $k
     45                 do
     46                         grep -n -s $1 $file
     47                         if [ $? -eq 0 ]
     48                         then
     49                         cp $file $2
     50                         fi
     51                 done
     52         fi
     53         echo "操作完毕"
     54 }
     55 s $*                                                                     
    
    

    方法二(比上面的方法好)

    copy(){
      2   k=$(find $3)
      3   for file in $k
      4   do
      5           grep -n -s $1 $file
      6           if [ $? -eq 0 ]
      7           then
      8           cp $file $2
      9           fi
     10   done
     11   }
     12 
     14 addCopy(){
     15         echo "开始操作..."
     16         if [ $# -eq 1 ]
     17                 then
     18                 if [ ! -d ./copy ]
     19                         then
     20                         mkdir ./copy
     21                 else
     22                 echo "copy文件夹存在,正在执行性操作"
     23                 fi
     24                 copy $1 ./copy ./
     25         fi
     27         if [ $# -eq 2 ]
     28                 then
     29                 copy $1 $2 ./
     30         fi
     32         if [ $# -eq 3 ]
     33                 then
     34                 copy $1 $2 $3
     35         fi
     36         echo "操作完毕"
     38 }
     39 addCopy $*                                                  
    
  • 相关阅读:
    Shared Memory in Windows NT
    Layered Memory Management in Win32
    软件项目管理的75条建议
    Load pdbs when you need it
    Stray pointer 野指针
    About the Rebase and Bind operation in the production of software
    About "Serious Error: No RTTI Data"
    Realizing 4 GB of Address Space[MSDN]
    [bbk4397] 第1集 第一章 AMS介绍
    [bbk3204] 第67集 Chapter 17Monitoring and Detecting Lock Contention(00)
  • 原文地址:https://www.cnblogs.com/du1991/p/7285088.html
Copyright © 2011-2022 走看看