-2

So, I want to pass 2 arguments and want those arguments to be processed in combination and separately too. I have coded the switch case to process them separately but I am not sure how to do process them together.

$ sh match_the_pattern.sh -a 6 -b 5 words.txt

Here,

  • -a: at least number of letters specified.
  • -b: at most number of letters specified.

What I am looking for?

when I get -a and -b together, first the script should list all the words which have at least given number of words, save, then out of those, it should process with the -b, that is the most number of letters in the result we got from the -a, then print the count.

This is a double filter, it is not like you posted both of them individually.

while [[ $# -gt 0 ]]; do
    case $1 in
    -a)
        argument=$
        egrep "^[[:alnum:]]{$argument,}$" $dictionyname | wc -l
        shift ;;

    -b) 
        arg=$2
        egrep "^[[:alnum:]]{0,$argument}$" $dictionyname | wc -l
        shift ;;
    esac
    shift
done

$ sh match_the_pattern.sh -a 6 -b 5 words.txt
7000

$ sh match_the_pattern.sh -a 6 words.txt
115690

$ sh match_the_pattern.sh -b 5 words.txt
12083

Note, I do know how to process -a and -b, I do not know how to combine the results of -a and -b when passed together in the argument...

If right now, I pass the command, I am getting this output :

$ sh match_the_pattern.sh -a 6 -b 5 words.txt
115690
12083

So it is processing a and b but giving the results one after another.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Bhavika
  • 7
  • 3
  • 3
    The trick is to not do any work in the option parsing loop, and just note which options are passed. Don't run `grep` until after the loop has ended, at which point you'll have the values for both flags available to you – that other guy Apr 22 '19 at 20:28
  • `all the words which have at least given number of words,` do you mean `number of letters`? – Walter A Apr 22 '19 at 20:33
  • 2
    To parse multiple command-line argument, especially if they're single character ones, have a look at [`getopts`](https://www.gnu.org/software/bash/manual/bash.html#index-getopts). – Benjamin W. Apr 22 '19 at 20:35
  • `argument=$` is missing the variable name at the end. – Barmar Apr 22 '19 at 21:20
  • After `-b` you set `arg`, but use `$argument`. – Barmar Apr 22 '19 at 21:20
  • Where do you set `$dictionyname`? – Barmar Apr 22 '19 at 21:21
  • Don't keep executing your script with `sh` as you're using constructs that won't work with `sh` (Bourne shell) unless it's aliased to `bash` or some other shell that understands it. Your script should have a shebang at the top and be executable so you can just call it by its name (which btw should just be `match_the_pattern`, not `match_the_pattern.sh` as shell scripts don't require suffixes and you'll notice none of the standard UNIX tools end in a suffix to tell you how they're implemented). – Ed Morton Apr 22 '19 at 22:41

1 Answers1

0

Set variables based on the parameters, then use them after the loop.

min=
max=
while [[ $# -gt 0 ]]; do
    case $1 in
    -a)
        min=$2
        shift ;;
    -b) 
        max=$2
        shift ;;
    -*)
        echo "Unknown option $1"
        exit 1 ;;
    *) 
        break ;;
    esac
    shift
done

if [[ -z $min && -z $max ]]
then 
    echo "At least one of -a and -b must be used"
    exit 1
fi

egrep "^[[:alnum:]]{${min:-1},$max}$" "$@" | wc -l

${min:-1} means to use the value of $min, but if it's empty use 1 instead. So if they don't give -a, it will be {1,$max}.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Well, I am not sure whether I didn't understand the code or you didn't understand the question. I have coded the same thing which you have just posted above. My question is, The script should work on 3 things.. 1.) When just -a or -b provided. ( This part I have working as I have the same code you have posted with the switch case. ) 2.) The same code must work when give the argument like -a 5 -b 5.. So it should be able to process the a and b and should give the 3rd result.... ( here 5 can be any number ). – Bhavika Apr 22 '19 at 22:17
  • You check the expected results, you will understand. – Bhavika Apr 22 '19 at 22:20
  • It was working with just `-a` and both `-a -b`, but didn't work with only `-b`. I thought `{,3}` would mean the same as `{0,3}`, but it doesn't match anything. I added a default value for `$min` to fix that. – Barmar Apr 22 '19 at 23:50