Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    4

    Red face Unanswered: Parsing tag & value help

    Hi,

    I'm a newbie to (bash) shell programming.

    I need to parse a user entered string and from there, call the required function to perform the task.

    The string command is based on: Tag(value)

    How do I parse the tag and its value, for example when a user enter a string like:

    A("abc") BB("123")

    How can I get the Tag "A" and its value "abc", and Tag "BB" and its value "123" so that I can call the required function based on it? The user can enter 1 or many Tag&Value in one command line string, but no repeated tag.

    Thank you..

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The following function parse the tag list and build two arrays.
    The first array contains the tag names and the second contains the tag values.
    Code:
    #!/usr/bin/bash
    
    # Usage: GetTags array_names array_vals tags..
    # Args:  array_names = Name of result array for tag names
    #        array_vals  = Name of result array for tag values
    #        tags..  = List of tags
    
    function GetTags {
       [ $# -lt 2 ] && return 1
       local array_names=$1
       local array_vals=$2
       shift 2
       local tags=$*
       local tnames tvals
    
       while [ ${#tags} -gt 0 ]
       do
          # Get Tag Name
          tnames="$tnames ${tags%%(*}"
          tags=${tags#*(}
    
          # Get Tag Value
          tvals="$tvals ${tags%%)*}"
          tags=${tags#*)}
    
       done
    
       eval ${array_names}=\($tnames\)
       eval ${array_vals}=\($tvals\)
    }
    
    #
    # GetTags call example
    #
    
    Tokens='A("abc")  BB("cde gh")'
    GetTags TagNames TagValues $Tokens
    for (( t=0; t<${#TagNames[*]}; t++ ))
    do
       echo "Tag <${TagNames[$t]}> Value <${TagValues[$t]}>"
    done
    Jean-Pierre.

  3. #3
    Join Date
    Apr 2004
    Posts
    4

    Thumbs up

    Thank you..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •