Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.77 KB | None | 0 0
  1. #! /bin/bash
  2. #Autor: DM
  3.  
  4. if [ "$1" = "--help" ]; then
  5.     echo "
  6. DESCRIPTION
  7. Program searches for a pattern arg1 \
  8. in files of a type arg2 in the catalog tree arg3.
  9. Directory's permissions must be set to r+w+x.
  10. 'w' is needed in order to create an error.log file.
  11. If script can't access directory or file, the stderr stream
  12. is redirected to error.log file.
  13.  
  14. USAGE
  15. program_name arg1 arg2 arg3
  16. arg1 - pattern
  17. arg2 - file type
  18. arg3 - catalog tree
  19.  
  20. EXAMPLE OF USAGE
  21. ./zad2.sh wzorzec .h /usr/include
  22.  
  23. EXIT CODE
  24. 0           No error
  25. 1           Incorrect number of arguments
  26. 2           Directory does not exist
  27. 3           Directory's read bit is set to 0
  28. 4           Directory's execute bit is set to 0
  29. 5           Directory's write bit is set to 0
  30. 6           Inccorect extension
  31. "
  32.     exit 0
  33. fi
  34.  
  35. if [ $# -ne 3 ]; then
  36.     echo "Incorrect number of arguments.
  37. Consider using --help"
  38.     exit 1
  39. fi
  40.  
  41. directory="$3"
  42.  
  43. if [ ! -d "${directory}" ]; then
  44.     echo "Directory does not exist!
  45. Consider using --help"
  46.     exit 2
  47. fi
  48.  
  49. if [ ! -r "${directory}" ]; then
  50.     echo "Directory's read bit is set to 0!
  51. Consider using --help"
  52.     exit 3
  53. fi
  54.  
  55. if [ ! -x "${directory}" ]; then
  56.     echo "Directory's execute bit is set to 0!
  57. Consider using --help"
  58.     exit 4
  59. fi
  60.  
  61. if [ ! -w "${directory}" ]; then
  62.     echo "Directory's write bit is set to 0!
  63. Consider using --help"
  64.     exit 5
  65. fi
  66.  
  67. extension="$2"
  68.  
  69. if [ ! ${extension:0:1} == '.' ]; then
  70.     echo "Incorrect extension!
  71. Consider using --help"
  72.     exit 6
  73. fi
  74.  
  75.  
  76. if [ ${#extension} = 1 ]; then
  77.     echo "Incorrect extension!
  78. Consied using --help"
  79.     exit 6
  80. fi
  81.  
  82. pattern="$1"
  83.  
  84. echo "File that contains \"$pattern\":"
  85.  
  86. grep --include=*"$extension" -Rnlw "$directory" -e "$pattern" 2>./error.log
  87.  
  88. if [ -s ./error.log ]; then
  89.     echo ""
  90.     echo "Check error.log file for errors"
  91. else
  92.     rm -f ./error.log
  93. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement