From 4c92ad77fd2893505ecf42182a53f96fe3f9141b Mon Sep 17 00:00:00 2001 From: dmarakom6 Date: Mon, 14 Jun 2021 19:56:37 +0300 Subject: [PATCH] Continue with grep if parameter is invalid --- util/notegrep.sh | 65 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/util/notegrep.sh b/util/notegrep.sh index cee49e8..cbae2e9 100644 --- a/util/notegrep.sh +++ b/util/notegrep.sh @@ -92,6 +92,46 @@ comment function notegrep() { + + DIR="$(pwd)" + + do_grep() { + + if [ -e ${DEFAULT_PATH}/${notename} ]; then + cd $DEFAULT_PATH + out="$(grep $regex $notename | wc -l)" + + if [ "$(grep -c $regex $notename)" -eq 0 ]; then + echo "No matches found." + else + + if [ $out -gt 20 ]; then + grep -i $regex $notename | less + elif [ $out -le 20 ]; then + grep -i $regex $notename + fi + + fi + + else + echo "This note doesn't exist in $DEFAULT_PATH." + fi + + } + + check_params() { + case $option in + parameter) + #parameter script import... + ;; + *) + echo "Invalid parameter. Proceeding in normal grep mode." + do_grep + ;; + + esac + } + if [ $# -eq 0 ]; then echo -n "Enter regex: " && read regex echo -n "Enter note name: " && read notename @@ -100,7 +140,7 @@ function notegrep() { option=$1 regex=$2 notename=$3 - #check + check_params return 0 else regex=$1 @@ -119,27 +159,10 @@ function notegrep() { return 0 fi - if [ -e ${DEFAULT_PATH}/${notename} ]; then - cd $DEFAULT_PATH - out="$(grep $regex $notename | wc -l)" - - if [ "$(grep -c $regex $notename)" -eq 0 ]; then - echo "No matches found." - else - - if [ $out -gt 20 ]; then - grep -i $regex $notename | less - elif [ $out -le 20 ]; then - grep -i $regex $notename - fi - - fi - - else - echo "This note doesn't exist in $DEFAULT_PATH." - fi + + do_grep unset GREP_OPTIONS cd $DIR return 0 -} \ No newline at end of file +}