Here is a example brash script that simulates grep using the following builtins: .ls .regex
The script following script defines two functions: one which is used to print grep's help message, and the other that actually implements most of grep's main functionality
#!/bin/brashfunction.grepPrintHelp{echo""echo".grep -- a simulation of grep"echo""echo" .grep [options] word ..."echo""echo"Options:"echo" --help -- print help"echo" -h -- suppress file names"echo" -n -- include line numbers"echo" -e word -- specify an additional search pattern"echo" -r -- recursively search directories"echo""echo"Description"echo""echo" Search the specified files for a specified regular expression"echo" print the results -- possibly including the file name and line"echo" numbers based on the options."echo""echo"Default behavior:"echo""echo" If -e option is not specified, then assume the first word on"echo" the command line is the pattern to search for. The rest of"echo" the words are assumed to be file names -- or directory names"echo" If the -e option is specified, only the parameters to -e will"echo" be treated as search expressions"echo""echo" When searching directories, all files in the directory will be"echo" searched -- but recursion will only occur with -r"echo""}function.grep(ropt=0#-rnotspecifiednopt=""#-nnotspecifiedpattern=""#-enotspecified.thisisalistof-evaluesseparatedby\|(both)patternSet=0hopt="-f"#-hnotspecifiedif["$1"="--help"]then.grepPrintHelpreturn1fi##interpretthecommandlineoptions: r,e,h,andn.Optionerequiresaparameter.#whilegetoptsre:hnnamedocase$nameine)if["$pattern"=""]thenpattern="$OPTARG"elsepattern="$pattern\\|$OPTARG"fipatternSet=1;;h)hopt="";;#turnofffilenamer)ropt=1;;#turnonrecursionn)nopt="-l";;#turnonlinenumbers*)echo"Invalid arg"1>&2;;esacdone##Removethe-options,ifany#letshiftcount=OPTIND-1if((shiftcount>=1))thenshift$shiftcountfi##ifno-eoptionsspecified,theconsumethefirstparameterasthepattern#tosearchfor.#if((patternSet==0))thenpattern="$1"shiftfiif[$#=0]then#noparametersweresupplied,soprintthehelpmessage.grepPrintHelpreturn1fi##ifthereisonly1option,andifthatoptionisnotadirectory#thenturnoffthedisplayingoffilenames.Ifyouneedtodisplaythename#andthereisonly1filetosearch,usenul: asasecondfilenametoovercome#thisannoyance.(Iknow,butthisishowtherealgrepworkstoo).#if[$#=1-a!-d"$1"]thenhopt=""fi##Iterateovereachfilenameoptionandhandleitbeingadirectoryornot.#forfin"$@"do##ignorefilenamesbeginingwith.(unlesstheyareinasubdirectorysearch)#case"$f"in.*)continue;;esacif[-d"$f"]then##Ifthecurrentfilenameisadirectory,thenbasedonwhether-r#wasspecifiedornotdoeitherof2things:##A.iterateoverallmembersofthedirectoryandjustgrep#them--if-risnotspecified.##B.recursivelyvisitthewholedirectorytreeandsearcheach#file--ignoringsubdirectoriesastheywillbehandledat#thetoplevel#if["$ropt"!="0"]then.ls-R"$f"|whilereadgdoif[!-d"$g"-a-r"$g"]theneval".regex $hopt $nopt -m '$pattern' '$g'"fidoneelseforgin"$f"/*doif[!-d"$g"-a-r"$g"]theneval".regex $hopt $nopt -m '$pattern' '$g'"fidonefielseeval".regex $hopt $nopt -m '$pattern' '$f'"fidone)
Note that due to the possibility of spaces in file names and directory names, it is advisable to always use double quotes around variable expansions.
Last edit: Lowell Boggs, Jr. 2018-04-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I posted an update to the above example which changed \" to ' in the calls to eval which runs the .regex function on files in subdirectories. This corrected a problem where the pathnames containing backslashes were being misinterpreted as escape sequences. Sigh.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is a example brash script that simulates grep using the following builtins:
.ls
.regex
The script following script defines two functions: one which is used to print grep's help message, and the other that actually implements most of grep's main functionality
Note that due to the possibility of spaces in file names and directory names, it is advisable to always use double quotes around variable expansions.
Last edit: Lowell Boggs, Jr. 2018-04-24
I posted an update to the above example which changed \" to ' in the calls to eval which runs the .regex function on files in subdirectories. This corrected a problem where the pathnames containing backslashes were being misinterpreted as escape sequences. Sigh.