0

I am expanding the following function to include the option -i | --ignore-case with error-handling

#!/bin/sh
[ $# -ne 1 ] && echo "1 argument is needed" && exit 1
find $HOME -type f -name "*.tex" -exec grep -il "$1" {} + | vim -

Expanded code

#!/bin/sh
################################
# Check if parameters options  #
# are given on the commandline #
################################
while (( "$#" )); do
   case "$1" in
    -h | --help)
        echo "help menu"
        exit 0
        ;;
    -i | --ignore-case)
        [ $# -ne 2 ] && echo "1 argumenst i needed" && exit 1
        find $HOME -type f -name "*.tex" -exec grep -il "$1" {} + | vim -
        exit 0
        ;;
     -*)
        echo "Error: Unknown option: $1" >&2
        exit 1
        ;;
      *) # No more options
        break
        ;;
   esac

   shift # not sure if needed
done

# Do this if no cases chosen
[ $# -ne 1 ] && echo "1 argument is needed" && exit 1
find $HOME -type f -name "*.tex" -exec grep -l "$1" {} + | vim -

Result

  1. haetex "TODO". Expected output is the same as output. Passed!
  2. haetex -i "TODO". Expected result: search with ignore-case. Result: blank file.

Why the option -i is not working here?

  • 1
    When $1 is -i, the string to search for is in $2, but you search for $1. – Barmar May 16 '16 at 15:15
  • Looks like the question has been revised so my answer is no longer appropriate. Was that a copying error that you've fixed? – Barmar May 16 '16 at 15:25
  • 1
    I'm not sure why you have a loop at all, since all the cases either exit the script or break out of the loop. – Barmar May 16 '16 at 15:26
  • If I post it as an answer, it won't make sense, since I'll be telling you to do what you're already doing. You shouldn't put the answer into the question. – Barmar May 16 '16 at 15:27
  • How would you do the thing without looping? I think looping is a natural way but not sure. – Léo Léopold Hertz 준영 May 16 '16 at 15:28
1

Change the grep in the -i case to search for $2, since $1 contains the option that you just tested, not the search string.

find $HOME -type f -name "*.tex" -exec grep -il "$2" {} + | vim -

To handle multiple options, it would be better for the case statement to just set a variable, e.g.

-i | --ignore-case)
    [ $# -ne 2 ] && echo "1 argumenst i needed" && exit 1
    case_option=-i
    ;;

Then the find command after the loop would look like:

find $HOME -type f -name "*.tex" -exec grep -l $case_option "$1" {} + | vim -

In this case it can use $1 because the shift has moved the search string to the beginning of the arguments.

So the whole script looks like:

while (( "$#" )); do
   case "$1" in
    -h | --help)
        echo "help menu"
        exit 0
        ;;
    -i | --ignore-case)
        [ $# -ne 2 ] && echo "1 argumenst i needed" && exit 1
        case_option=-i
        ;;
     -*)
        echo "Error: Unknown option: $1" >&2
        exit 1
        ;;
      *) # No more options
        break
        ;;
   esac

   shift # not sure if needed
done

find $HOME -type f -name "*.tex" -exec grep -l $case_option "$1" {} + | vim -
  • 1
    What I showed just replaces the -i) case from your script, the rest of the loop is the same. I define case_option in the code I showed: case_option=-i. – Barmar May 16 '16 at 15:37
  • 1
    I made a mistake, shift isn't needed in the case. It just sets the variable, and the shift at the bottom of the loop does the necessary rest. – Barmar May 16 '16 at 15:41
  • 1
    I've put the whole script in. – Barmar May 16 '16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.