Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- #Autor: DM
- if [ "$1" = "--help" ]; then
- echo "
- DESCRIPTION
- Program searches for a pattern arg1 \
- in files of a type arg2 in the catalog tree arg3.
- Directory's permissions must be set to r+w+x.
- 'w' is needed in order to create an error.log file.
- If script can't access directory or file, the stderr stream
- is redirected to error.log file.
- USAGE
- program_name arg1 arg2 arg3
- arg1 - pattern
- arg2 - file type
- arg3 - catalog tree
- EXAMPLE OF USAGE
- ./zad2.sh wzorzec .h /usr/include
- EXIT CODE
- 0 No error
- 1 Incorrect number of arguments
- 2 Directory does not exist
- 3 Directory's read bit is set to 0
- 4 Directory's execute bit is set to 0
- 5 Directory's write bit is set to 0
- 6 Inccorect extension
- "
- exit 0
- fi
- if [ $# -ne 3 ]; then
- echo "Incorrect number of arguments.
- Consider using --help"
- exit 1
- fi
- directory="$3"
- if [ ! -d "${directory}" ]; then
- echo "Directory does not exist!
- Consider using --help"
- exit 2
- fi
- if [ ! -r "${directory}" ]; then
- echo "Directory's read bit is set to 0!
- Consider using --help"
- exit 3
- fi
- if [ ! -x "${directory}" ]; then
- echo "Directory's execute bit is set to 0!
- Consider using --help"
- exit 4
- fi
- if [ ! -w "${directory}" ]; then
- echo "Directory's write bit is set to 0!
- Consider using --help"
- exit 5
- fi
- extension="$2"
- if [ ! ${extension:0:1} == '.' ]; then
- echo "Incorrect extension!
- Consider using --help"
- exit 6
- fi
- if [ ${#extension} = 1 ]; then
- echo "Incorrect extension!
- Consied using --help"
- exit 6
- fi
- pattern="$1"
- echo "File that contains \"$pattern\":"
- grep --include=*"$extension" -Rnlw "$directory" -e "$pattern" 2>./error.log
- if [ -s ./error.log ]; then
- echo ""
- echo "Check error.log file for errors"
- else
- rm -f ./error.log
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement