Function ignoring global variables

Hi there.
I'm writing a function to which I want to pass a global variable. For some reason, it's ignoring the variable.

#!/bin/bash
#####################################
#Variable Declaration
#####################################

CURPATH=`dirname $0`
DEEP=$CURPATH/depth.txt

export $DEEP

function getdepth()
{

  echo $DEEP

  cat $1  | egrep ^${2} | awk -vgroup=$1 -vdepth=$DEEP -F, '
  {
      #Bunch of stuff that works when I hardcode it, but fails with the variable.

  }'
}


######
# Main block
######

getdepth some_file some_element

Any idea why the output of "echo $DEEP" is just a blank? And how do I make it pay attention to the global variables?

Thanks in advance,
Mike

---------- Post updated at 11:18 PM ---------- Previous update was at 11:14 PM ----------

Nevermind, I figured it out.

it's

export DEEP=$CURPATH/depth.txt.

Now I feel like a tool.

There's no need to use dirname in bash (or any POSIX shell); use parameter expansion:

CURPATH=${0##*/}

(But there's rarely a good reason to require the location of the script.)

export DEEP

Why are you exporting DEEP? Are you calling another script that will need it?

That is a bash hybrid, a combination of ksh syntax and standard syntax. Use standard syntax:

getdepth()

UUOC

What "stuff"? If that's where it fails, that's what you should be posting.

Thank you kindly for your help. I'll make the changes to my syntax that you describe. However, to answer some of your concerns:

I have that in there for now, because I originally had several scripts being called, instead of knowing how to get them all as functions in one script, and I wanted to make sure I had an easy way to change all of their paths if I needed to.

[quote="cfajohnson,post:2,topic:261180"]

[indent]

[indent]

export DEEP

Why are you exporting DEEP? Are you calling another script that will need it?

Because until I exported it, this function flatly refused to believe its existence - hence the subject of the post. It would always return a blank.

The only line of importance, really, was the "echo $DEEP", since, if $DEEP had no value (which was the problem I was experiencing), then the rest of the stuff would fail, but if $DEEP had a value, it worked.

Once I made the change that I posted (export DEEP=blahblahblah), it worked magic.

If you really want to view a bunch of cringe-worthy code, I'll be happy to PM you the whole sad script, but, the reality is, I'm just trying to slam a hodge-podge of what little I know about bash scripting into a workable tool that will take the useless output from the configuration reports of a SYMC firewall and convert it into Cisco commands. I'm a network guy - once it gets past layer four, I stop caring. :slight_smile:

Exporting DEEP makes no difference in the code you posted. The problem is elsewhere.

Perhaps it should not make a difference, but the fact is that when I export it, it works, and when I don't, it doesn't. However, I'll post my whole horribly awful file here, and perhaps you'll see why it doesn't work when it's not exported. The root cause of all of my scripting woes is, of course, "I suck at scripting, and am only doing this to keep from trying to create 2000 objects by hand", instead of "gosh, I really love this stuff, and want to do it right."

It truly is an awful hodge-podge. Don't say I didn't warn you.

Try not to puke into your keyboard when/if you read it.

#!/bin/bash



#####################################
#Variable Declaration
#####################################

CURPATH=`dirname $0`
CSV=$CURPATH/output.csv
CISCO=$CURPATH/cisco_protocol_object_groups.txt
PROTOCOLS=$CURPATH/protocol_table.csv
GROUP=$CURPATH/groups.csv
ERRORS=$CURPATH/ambiguous_group_members.txt
FTEMP=$CURPATH/swap.txt
export DEEP=$CURPATH/depth.txt





##############################################################
# Declare Functions
##############################################################
function getdepth()
{
  ## ARGUMENTS:
  # $1 is the groups file from which we obtain the member list
  # $2 is the group name whose members we're querying


  cat $1  | egrep ^${2} | awk -vgroup=$1 -vdepth=$DEEP -F, '
  {
      for (i=2;i<=NF;i++)
          {
           if ($1 != "") 
           {
            if (system("isgroup " group " "  $i) != 0)
            {
                {system("echo " group " is being sent to " depth) 2>/dev/null}
                {system("setdepth " $i " " depth ) 2>/dev/null}
                {system("getdepth " group " " $i) 2>/dev/null}

            }
          }

    }
  }'
}



function setdepth()
{
    #ARGUMENTS:
    # $1 is the element for which we are searching.
    # $2 is the depthfile from which we're running the search
    echo $1 $2
    if [ ! -f $2 ]; 
       then touch $2  
    fi
    
    let DEPTH=`cat $2 | egrep ^${1} | cut -d: -f2`+1
    cat $2 | egrep -v $1 > tmp
    echo ${1}:${DEPTH} >> tmp
    sort -t: -rnk 2,2 tmp >$2
    rm -f tmp
}

function isgroup()
{
  #ARGUMENTS:
  #$1 is the name of the groups file
  #$2 is the group name we seek.
  
  excode=`cat $1 | egrep -wc ^${2}`
  exit $excode
}

function protocolshred()
{
    echo -e "Processing" $1


    echo -e "Stripping HTML tags"
    tr -d '\t' < $1 | sed 's/<.*>//g' | sed '/^$/d' | sed 's/\&amp\;/,/g' | egrep -v "Symantec Gateway Security|Report Generated|Network Protocol Report|UUID|Caption|Description|Read only|true|false" | sed 's/Base Protocol/\nBase Protocol/g' > $CSV
    #Next, fix the stuff with weirdly placed line breaks
    echo -e "Removing erroneously placed line breaks"
    perl ./fixit.pl $CSV
    #Turn it into a nice CSV file where most of the columns line up.
    echo -e "Converting to CSV"
    cat $CSV | egrep -v "[A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12}|Use Native Service|Use GSP"  |  tr '\n' ',' | sed 's/,,/,/g'|  perl -pe 's/,Protocol name:/\nProtocol name:/g' | sed 's/Protocol name: //g' | sed 's/-,/-/g'  > $FTEMP
    #Remove my temp file
    rm -f $CSV
    mv $FTEMP $CSV
    echo -e "Removing comments and other unhelpful items."
    #Remove comments to make the rest of the columns line up properly.
    cat $CSV | awk -F, '
        {for (i=1;i<=NF;i++)
           {
             if (((i < 4) || ($i ~ /(Native Service|(Source|Destination) (High|Low)) Port/) || ($i ~ /^[0-9]+$/)) && (i != NF))
                {printf $i ",";}
             else if (i == NF)
             {printf $i "\n"};
           }

        }' > $PROTOCOLS

    #Ahhh. Many perfect little lines. Everything in the same column.
    #Don't need this $CSV file anymore
    rm -f $CSV
}

function groupshred()
{
   echo -e "Processing" $1
   #First, rip out the OBVIOUS trash
    echo -e "Stripping HTML tags"
    tr -d '\t' < $1 | sed 's/<.*>//g' | sed 's/\&[gl]t\;//g' | sed '/^$/d' | sed 's/\&amp\;/,/g' | egrep -v "Symantec Gateway Security|Report Generated|Service Group Report|UUID|Caption|Description|Read only|true|false" | sed 's/Service Group Name:/\nService Group Name:/g' > $CSV

    #Next, fix the stuff with weirdly placed line breaks
    echo -e "Removing erroneously placed line breaks"
    perl ./fixit.pl $CSV

    #Turn it into a nice CSV file where most of the columns line up.
    echo -e "Converting to CSV"
    cat $CSV | sed 's/Additional/\nAdditional/g'|  egrep -v "[A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12}"  |  tr '\n' ',' |sed 's/[ \t]*$//' | sed 's/,,/,/g'|  perl -pe 's/,Service Group Name:/\nService Group Name:/g' | sed 's/Service Group Name: //g' | sed 's/-,/-/g' | sed '/^$/d' > $FTEMP
    #Remove my temp file
    rm -f $CSV
    mv $FTEMP $CSV


    #Yank out the descriptions
    cat $CSV | awk -F, '
        {
         {pf = 0}
         for (i=1;i<=NF;i++)
             {if ($i ~ /^Protocols$/) {pf = (i+1)}}
         printf $1 ","
         for (i=pf;i<=NF;i++)
             {
              if (i == NF) {print $i;}
              else {printf $i ","};
             }
          }
     ' > $GROUP

    #Aaaaaand, wipe out the temp table
    rm -f $CSV
}

function NestedGroups()
{
  echo "Finding nested groups."

   cat $GROUP | awk -F, -v groupfile=$GROUP '
       {if ($1 != "")
       {system("getdepth " groupfile " " $1) 2>/dev/null} }'
}


###############################################
# Cleanup
###############################################
function cleanup()
{
    files="$CSV $HNS $PIVOT $GROUP $ERRORS $FTEMP $DOMAINS $NAMES $LOCATIONS $DEEP"
    # $i will hold single file at a time in following loop
    for i in $files
    do
       if [ -f $i ]; then
         echo -e "Deleting file from last run: " $i
         rm -f $i
       fi
    done
}


############################################################
# Export Functions
############################################################
export -f getdepth
export -f setdepth
export -f isgroup


###############################################
# Check for proper number of command line args.
###############################################

EXPECTED_ARGS=2
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` /path/to/Protocols.htm  /path/to/Service_Groups.htm"
  exit $E_BADARGS
fi



###########################################################
# MAIN BODY
##########################################################

###
# Ran it before, and need to clean up the old files?
###

   cleanup


##
# Send the protocols file for processing
##

  protocolshred $1

##
# Now we start processing the groups list.#
##

  groupshred $2

###
# The first step in processing the groups is to figure out which ones to do first.
# The ones that are nested deepest must be created in the ASA before the ones that
# contain them, or there's going to be an error. I don't like errors.
###


   NestedGroups

###
# Holy shit, it worked. That little bit you saw was two days of work. God, I suck at programming.
# Anyway, now that we know which groups to define first, we can start the relatively simple process
# of populating the groups.
###

#Haven't written this part yet, but it should be fairly straightforward.