Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- arg=
- case $# in
- 1)
- pattern=$1
- dir=./
- ;;
- 2)
- if [ -d $1 ]
- then
- dir=$1
- else
- arg=$1
- dir=./
- fi
- pattern=$2
- ;;
- 3)
- arg=$1
- dir=$2
- pattern=$3
- ;;
- esac
- # echo "$arg"
- # echo "$pattern"
- # echo "$dir"
- # Check incorrect argument input
- if [[ $# -eq 0 || $# -gt 3 || !(-d $dir) || "$arg" != "-r" && -n $arg ]]
- then
- echo Usage: strfinder [KEY] [DIR] [PATTERN]
- echo Key: -r for search in the subdirectories
- exit 1
- fi
- rep=true
- # main loop
- while [[ $rep=true && -n $pattern ]]
- do
- # output pattern and target dir
- echo
- echo "Search $pattern in $dir"
- echo
- # Counter of matches
- count=0
- # work with files, witch consider pattern string
- for file in `grep $arg -l -s $pattern $dir*`
- do
- let "count++"
- # Out file and commands
- echo
- echo "Found \"$pattern\" in file: $file"
- read -p "[R]emove file, [B]an for other users, [Nothing] to do nothing: " op
- echo
- case $op in
- R | r) rm $file && echo "File $file removed" || echo "Can't remove file $file" ;;
- B | b) chmod og-wrx $file && echo "File $file was banned for other" || echo "Can't ban file $file" ;;
- "") continue ;;
- *) echo "Incorrect operation" ;;
- esac
- echo
- done
- # if pattern wasn't found
- if [ $count -eq 0 ]
- then
- echo "No files was found."
- read -p "Input new string for search: " pattern
- else
- # else break loop
- break
- fi
- done
- echo
- echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement