0

I am passing the variable akms to the function named sufx. I am able te correctly capture the second array element and set the variable fskl.

I want the capability of setting the variable verbiage according to the numeric value of -v. How can I do this for the function sufx ? Currently verbiage is just taking a string value.

akms=("-v 3" "INCLUDE")
sufx akms

Here is the function

sufx ()
{
  typeset -n _akms="$1"

Capture INCLUDE or EXCLUDE option for grep

local fskl="" verbiage=0 for s in "${_akms[@]}"; do case $s in ("XCL"|"INCL"|"INCLUDE") fskl="INCLUDE" ;; ("NCL"|"EXCL"|"EXCLUDE") fskl="EXCLUDE" ;; () verbiage=${s#-v} ;; esac done

}

Fatipati
  • 379

2 Answers2

0

I guess, you mean:

  1. How to check if an array element is of the pattern -v then "anything" then "numeric character/s" ... You can do this using a sub-string regular expression match with bash's extended test brackets and the =~ regex operator i.e. [[ "$element" =~ regex ]].

  2. How to extract only the numeric characters of a string stored in a parameter ... You can do this using bash's parameter expansion to exclude all non-numeric characters with e.g. "${parameter//[^0-9]}".

So, combining the above two methods:

$ array=("-v 3" "INCLUDE")

$ for i in "${array[@]}" do if [[ "$i" =~ "-v".*[0-9]{1,} ]] then var="${i//[^0-9]}" echo "$var" echo "$((var+2))" fi done

Output:

3 5

Or, using a globbing pattern with case ... esac for example:

$ array=("-v 3" "INCLUDE")

$ for i in "${array[@]}" do case "$i" in [-][v]*[0-9] ) var="${i//[^0-9]}" && echo -e "$var\n$((var+2))" ;; esac done

Output

3 5

Please also see Can globbing be used to search file contents?

Raffa
  • 32,237
  • I would like to use a glob pattern that I can use within the case statement I am using. – Fatipati Apr 02 '23 at 18:34
  • Have included (-v([[:blank:]])+([0-9])+) verbiage="${s//[^0-9]}" ;; in the case statement, but there is some syntax problem with it. – Fatipati Apr 02 '23 at 18:42
  • @Backy That is a regular expression pattern and not a globbing pattern … please see the linked post for how to use globbing patterns with case and how to use regular expression patterns with extended test brackets [[ … =~ … ]] – Raffa Apr 02 '23 at 18:49
  • @Backy I added an example with case ... esac – Raffa Apr 02 '23 at 19:46
  • Quite right, the special characters go in front of the bracket expressions. – Fatipati Apr 02 '23 at 20:34
0

I'd suggest passing the "-v 3" string as a separate option letter + option argument "-v" "3". Then you can use the bash shell's built-in option parser getopts to process them:

sufx ()
{
    typeset -n _akms="$1"
local verbiage=0 fskl="" 

## process option arguments
local OPTIND=1
while getopts ':v:' opt "${_akms[@]}"
do
    case $opt in
        (v) verbiage=$OPTARG
            ;;
        (*) echo "unkown option: $opt" 1>&2
            ;;
    esac
done

## process remaining non-option arguments
for s in "${_akms[@]:$((OPTIND-1))}"
do
    case $s in
       ("XCL"|"INCL"|"INCLUDE") fskl="INCLUDE"
       ;;
       ("NCL"|"EXCL"|"EXCLUDE") fskl="EXCLUDE"
       ;;
    esac
done

}

The usage becomes

akms=("-v" "3" "INCLUDE")

sufx akms

You could even use akms=("-v" "3" -- "INCLUDE") to explicitly mark the end of options (which would allow you to use non-option arguments starting with a dash).

steeldriver
  • 136,215
  • 21
  • 243
  • 336