Score:0

Setting local variables from array elements

jp flag

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

}
Score:0
jp flag

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?

jp flag
I would like to use a glob pattern that I can use within the case statement I am using.
jp flag
Have included `(-v([[:blank:]])+([0-9])+) verbiage="${s//[^0-9]}" ;;` in the case statement, but there is some syntax problem with it.
Raffa avatar
jp flag
@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 avatar
jp flag
@Backy I added an example with `case ... esac`
jp flag
Quite right, the special characters go in front of the bracket expressions.
Score:0
hr flag

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).

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.