[Solved] Command Substitution and Variable Expansion within a Case

Hello All,

I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system).

I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other criteria.

For this example, I want to extract...

  Reception_1

from the following string

  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>

that is buried in the following chunk of text (that is pulled from an event log file in another process)

  14:10:03: CONNECT:      A5BF OcPhone: Reception_1,     OcLine: Reception_1,         <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcHuntGroup: Operator_Group, <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcStaticCall: System, <audio=0.0.0.0:0 729 711 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: IDLE:0,0      25BF OcSIPGatewayNew: T1G1,    OcStreamer: System,
  14:10:03: CONNECT:      25BF OcSIPGatewayNew: T1G1,    OcTrunkGroup: T1,            <audio=10.0.0.2:11728 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcStaticCall: System,        <audio=10.0.0.2:11728 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: ACTIVE:       25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcPhone: Reception_1,     OcLine: Reception_1,         <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcLine: Reception_1,      OcHuntGroup: Operator_Group, <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: CONNECT:      A5BF OcHuntGroup: Operator_Group, OcStaticCall: System, <audio=10.0.0.3:61254 729 711 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcStaticCall: System,     OcHuntGroup: Operator_Group, <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcStaticCall: System,     OcTrunkGroup: T1,            <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcHuntGroup: Operator_Group, OcLine: Reception_1,      <audio=10.0.0.1:12082 729 711 a=sendrecv>
  14:10:03: CONNECT:      25BF OcTrunkGroup: T1,         OcSIPGatewayNew: T1G1,       <audio=10.0.0.1:12080 729 711 101 a=sendrecv>
  14:10:03: ACTIVE:       A5BF OcLine: Reception_1,      OcPhone: Reception_1,        <audio=10.0.0.1:12082 729 711 a=sendrecv>

I am trying to do this with the following snippet of bash script from within a case statement:

#!/bin/bash
#GNU bash, version 2.05b.0(1)-release
#

    IFS=$'\t\n'                                     # 
    CDRF12="OcHuntGroup:~Operator_Group~~~"         # CDR Destination, from another process
    TimeAnsLoc="11/15/2010~14:10:03"                # CDR Date from another process
    EVTFILE="/var/log/eventfile.txt.gz"             # Event file from another process
    EVTSRCH="[2,A]5BF"                              # Event OID set, from another process
    DSTENDT=""                                      # Destination EndPoint Type
    DSTENDN=""                                      # Destination EndPoint Name
    DSTAGNT=""                                      # Destination Agent from EventLog (Huntgroups, Queues)
    case "${CDRF12:0:6}" in
            "OcHunt") DSTENDT="HG"                 # To Huntgroup
                      DSTENDN="${CDRF12:13:30}"    # OcHuntGroup: ~~~~~~~~~~~~~~~~~ ${CDRF11:13:30}
                      EVTSRCH="$(echo "\"${TimeAnsLoc:11:19}: CONNECT:.*""$EVTSRCH"" OcHuntGroup\"")"
                      DSTAGNT="$(zgrep $EVTSRCH $EVTFILE | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3 )"
                      ;;
                  * ) DSTENDT="UK"                  # Unknown
                      DSTENDN="UNKNOWN"             # Also Unknown
                      ;;
    esac
    #Testing
    echo "CDRF12  = $CDRF12"
    echo "TimeAnsLoc = $TimeAnsLoc"
    echo "EVTSRCH = $EVTSRCH"
    echo "EVTFILE = $EVTFILE"
    echo "SEARCH Expansion = zgrep $EVTSRCH $EVTFILE | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3"
    echo "DSTAGNT = $DSTAGNT"

When I run this, I get the following output:

> CDRF12  = OcHuntGroup:~Phil~Crisp~Agency
> TimeAnsLoc = 11/15/2010~14:10:03
> EVTSRCH = "14:10:03: CONNECT:.*[2,A]5BF OcHuntGroup"
> EVTFILE = /var/log/eventfile.txt.gz
> SEARCH Expansion = zgrep "14:10:03: CONNECT:.*[2,A]5BF OcHuntGroup" /var/log/eventfile.txt.gz | grep OcLine | cut -d, -f2 | sed 's/ /~/g' | cut -d~ -f3
> DSTAGNT =

As you can see, DSTAGNT is null, and as best I can tell the zgrep command in the case is not being evaluated. Running the expanded zgrep search from the command line works fine.

Any assistance will be appreciated in determining why this is so.

Jaimie :wall:

---------- Post updated at 05:40 PM ---------- Previous update was at 02:28 PM ----------

Solved.

Found/Remembered use of "eval" in a case structure....

Thanks for keeping us informed

All the best