Addcomment

Mar, 08/03/2005 - 15:22

Addcomment

Inviato da Marcoski 0 commenti

<br />#!/bin/bash<br /><br />#WRITTEN BY Marco 'Marcoski' Trognoni<br />#usage [OPTIONS -k, -w KEYWORD, -e EXTENSION] [DIRPATHNAME] [TEXTFILE]<br />#options:<br />#    -k              perform keyinsensitive search<br />#    -w KEYWORD      perform search excluding all match of keyword<br />#    -e EXTENSION    perform search on file with EXTENSION<br /><br />#global var for pathname<br />dirpath="";<br />filepath="";<br /><br />#global var for keyword and extension<br />keyword="";<br />extension="";<br /><br />#global var for options<br />option=0;     #option: 1-> only -w, 2-> -w && -e, 3-> only -e<br />kopt=0;<br />opthelp=0;<br /><br />#global error<br />error="";<br /><br />#arguments handling<br />while [[ "$1" != "" ]]; do<br />       #looking for dirpath<br />      if [[ -d $1 ]]; then<br />              dirpath=$1;<br />       #looking for filepath<br />     elif [[ -f $1 ]]; then<br />            filepath=$1;<br />      #looking for -k options<br />   elif [[ "$1" == "-k" ]]; then<br />             kopt=1;<br />   #looking for keyword<br />      elif [[ "$1" == "-w" ]]; then<br />             keyword=$2;<br />               shift<br />             option=1;<br /> #looking for extension<br />    elif [[ "$1" == "-e" ]]; then<br />             extension=$2;<br />             shift<br />             if [[ "$option" == "1" ]]; then<br />                   option=2;<br />         else<br />                      option=3;<br />         fi<br />        fi<br />        shift<br />done<br /><br />#check for filepath or dirpath error, force option to 0 and opthelp to 1 printing help msg<br />if [[ -z $dirpath ]]; then<br />     error="error: no such directory";<br /> opthelp=1;<br />        option=0;<br />fi<br />if [[ -z $filepath ]]; then<br />        error=$error" error: no such text file";<br />  opthelp=1;<br />        option=0;<br />fi<br /><br />#check kopt to choose which tipe of find test to use "-name" or "-iname"<br />if [[ "$kopt" == "1" ]]; then<br />  ftest="-iname";<br />else<br /> ftest="-name";<br />fi<br /><br />case "$option" in<br />       0)<br />                #if opthelp -> error detected<br />             if [[ "$opthelp" == 1 ]]; then<br />                    echo $error<br />                       echo "<br />`basename $0`: usage [OPTIONS -k, -w KEYWORD, -e EXTENSION] [DIRPATHNAME] [TEXTFILE]<br /> options:<br />         -k              perform keyinsensitive search<br />             -w KEYWORD      perform search excluding all match of keyword<br />             -e EXTENSION    perform search on file with EXTENSION<br />"                      <br />                  exit<br />              else<br />              #else scan for all files in $dirpath and script does action<br />                       for filename in $dirpath/*; do<br />                            cat $filepath $filename > .tmp<br />                            mv .tmp $filename<br />                 done<br />              fi<br />        ;;<br />        1)<br />                #search by !KEYWORD and script does action<br />                for filename in `find $dirpath/* -not $ftest "$keyword"`; do<br />                      cat $filepath $filename > .tmp<br />                    mv .tmp $filename<br />         done<br />      ;;<br />        2)<br />                #search by !KEYWORD && EXTENSION and script does action<br />   <br />          for filename in `find $dirpath/* -not $ftest "$keyword" -a $ftest "*.$extension"`; do<br />                     cat $filepath $filename > .tmp<br />                    mv .tmp $filename<br />         done<br />      ;;<br />        3)<br />                #search by EXTENSION and script does action<br />               for filename in `find $dirpath/* $ftest "*.$extension"`; do<br />                       cat $filepath $filename > .tmp<br />                    mv .tmp $filename<br />         done<br />      ;;<br />        <br />esac<br /><br />echo "files changed succesfully"<br /><br />


In: