Small search Script in Bash  

 

 

Back to main page

Task  : Small Text Search Script in Shell

Status : Done, still debugging is needed , code will be posted before 25 sept 2007

Problem Statement : 

The objective of this assignment is to examine your familiarity in Unix shell scripting using the bash shell.

Create a script called srchengn that will print the lines matching a pattern from a set of files/folders. Your script should support the following options.

  • Syntax : 
    • srchengn options pattern [ FILE...] 
  • Options: 
    • -f
      •  Searches the contents of the file/s specified by [ FILE...] and displays the line containing the pattern on the standard output and display error message when the file was not found. 
    • -F
      • Search for the string in all the files present in the given directory. 
    • -d
      •  Searches all the files present in the directory/s for the string specified and the result is displayed on the standard output and display error message when the directory was not found 
    • -n
      •  Output along with line numbers in which the string is found 
    • -o
      • Output to a file specified. This option will be used along with  other options like -f/-F/-D etc. 
    • -r/-R
      • Search all files under each directory, recursively; 
    • -i
      • Ignore case distinctions in both the PATTERN and the input files. 
    • -m
      • Finds the frequency of the given string in the given file/directory.
    • -h / -? 
      • Output a brief help message.
  •  Examples
    • $ ./srchengn.sh -f echo 1.sh
      echo "Enter the name and number of the student"
      echo " student name is $nm and the entry number is $no "
      echo "$i $j"
      echo "$k "
      echo "hi $user the number of users logged in are $numusers"
    • $ ./srchengn.sh -f echo 1.sh 2.sh
      1.sh:echo "Enter the name and number of the student"
      1.sh:echo " student name is $nm and the entry number is $no "
      1.sh:echo "$i $j"
      1.sh:echo "$k "
      1.sh:echo "hi $user the number of users logged in are $numusers"
      2.sh: echo " Hi $user u got the required info "
      2.sh:echo " This is ouput from the second file "
    • $ ./srchengn.sh -f echo 1.sh
      echo "Enter the name and number of the student"
      echo " student name is $nm and the entry number is $no "
      echo "$i $j"
      echo "$k "
      echo "hi $user the number of users logged in are $numusers"
    • $ ./srchengn.sh -f echo 1.sh 2.sh -o output
      $ cat output
      1.sh:echo "Enter the name and number of the student"
      1.sh:echo " student name is $nm and the entry number is $no "
      1.sh:echo "$i $j"
      1.sh:echo "$k "
      1.sh:echo "hi $user the number of users logged in are $numusers"
      2.sh: echo " Hi $user u got the required info "
      2.sh:echo " This is ouput from the second file "
    •  

Solution : 

                                               README:
                                 DESCRIPTON OF ASSIGNMENT 1
Basically my program is divided into two parts
     1. Parsing the all arguments and set various flags according
     2. according to flags search in files or directory

  • Variables : some are here
    • ALLARG=$@
    • NUM_ARG="$#"
    • PARSE_ERROR=0
    • ONLY_FILES=0
    • ONLY_FOLDERS=0,
    •  ONLY_PWD=0
    • COUNT_LINES=0
    • COUNT_WORDS=0,
    • OUTPUT_TO_FILE=0 ,
    • OUTPUT_FILE="xyz.out" , 
    • RECURSIVE=0 
    • IGNORE_CASE=0, 
    • GIVE_HELP=0
    • NO_OPTIONS=0,
    • NO_ARG=0,
    • PATTERN="",
    • FILES_FOLDERS="" 
  • If there value is 0 means they are reset and 1 means set
  • we may have many combination of -f -F -d so I am generating and AMB_CASE variable that take care of all cases

value of AMB_CASE

-f-F-d


0

000

Search any way

1

001

treats all file arguments as directories

2

010

search in PWD

3

011

treats all file argument as directories plus include PWD in search

4

100

treats all file arguments as files

5

101

Can't understand

6

110

Can't understand

7

111

Can't understand


  • On ambiguity it through an error..
  • ERROR_GIVE_HELP="you should not put any other arguments after -h/-?"                   #10
  • ERROR_PARSE_ERROR="Options are not properly set"
  • ERROR_NO_ARG="\t `basename $0` options pattern [FILE...]\n\t use `basename $0` -h for

complete help"

  • ERROR_PATTERN="You are not providing any Pattern"                              #13
  • ERROR_FILES_FOLDERS="You are not providing any any file and folder"                    #14
  • ERROR_OUTPUT_TO_FILE="you have not given -o option properly"                           #6
  • ERROR_AMB_CASE15=" please do not use -f and -d at same time : no meaning"
  • ERROR_AMB_CASE16=" please do not use -f and -F at same time : no meaning"
  • ERROR_AMB_CASE17=" please do not use -f , -F and -d at same time : no meaning"
  • ERROR_AMB_CASE18=" -F should be used as alone or use -F -d to make search meaningful"

Error handling
        Many types of error can be generated mean while the execution..
All types of error are having an unique integer associated with it. Variable PARSE_ERROR hold
this identifier and function DISPLAY_ERROR uses it to show what the error...is
 Function list


  • 1. DISPLAY_ERROR(): it show various error occurred in program, it simply compare value of PARSE_ERROR to various error numbers
  • 2. find_in_file() : this is very basis function. It takes only one argument as a files name and read lines, and breaks lines into words. After it we compare with pattern, and display lines according to the formatting options (-i, -n, -m). it uses various flags set by parsing step. If files does not present then it will print an error message.
  • 3. find_in_folder() : this function takes only one argument as a directory. Go in side it and detect all files and directories, if it encounters a file then it calls find_in_file() function to search and if it found a directories it first check whether the -r option is provided or not. If provided then it will call itself with that directory as argument, means it will further search recursively, else it will not go inside that directory.
  • 4. help_message(): it show a big help message , how to use it

Execution of Program : various stages
  1. empty argument checking :first we check that if user is providing no arguments we shoud
     show syntax
  2. Options tracing: at this stages we will compare arguments with -f, -F, and like these other
     options, and enabling and disabling various flags. We stop as soon as we find any invalid
     option
  3. Pattern Extracting: the invalid option found in stage 2 is pattern provided by user. If
     suppose user is not providing any pattern, then we will exit with error that pattern not found
  4. detecting -o and extracting files and folders : now we will search for -o and if present then
     the left side is effectively files and folders and right side is name of output file in which we
     will write search result. If -o and output file is not present then we will print result into
     consol. Any other condition will give a error.
  5. Ambiguity resolver : we will construct AMB_CASE variable from the absence and
     presence of -f , -F and -d. And we will find out what are the errors present and possible
     decisions.
  6. redirecting STDOUT : if user is proving -o then we will redirect STD_OUT to file
     specified
  7. search : now according to value of AMB_CASE we will take each file or directory given by
     user one at a time and search using functions specified above namely find_in_file() and
     find_in_folder().
 

Code

    download this file