Home > OS >  grep for a list of variables that are referenced by a script?
grep for a list of variables that are referenced by a script?

Time:12-21

I have some scripts that use various variables, and I want to grep (from within bash on FreeBSD) each of them for the list of variables that are used by the script. These are not shell scripts, but the syntax of referencing a variable is similar to that used in bash. Specifically, a reference to a variable can be like:

  • $X, or
  • ${X}

and the name of the variable ("X" in this case) can include alphanumerics and underscores. At this point I want to explicitly note that I imagine that bash itself probably has a more complicated set of possible ways to reference variables, but if so, I do not care about that for the purposes of this question.

I would like to find all variable names that are so referenced in a given file - just the name, not the entire line. So something of the form awesomegrepcmd filename | sort | uniq, or something like that.

Note that there may be multiple different variables referenced on a single line.

I have been fighting regex and escaping rules via largely ignorant stab-in-the-dark attempts (e.g. "OK, maybe I need to put TWO backslashes here, but only ONE there") for a while now, and googling to try to find others who have done this sort of thing, but have thus far been unable to accomplish what I want. How can this be done? Thanks.

EDIT: Example: Let's say the content of a file is this (just pseudocode, not meant as bash or anything):

SPECIFICS={ "be born", "die", "plant", "reap", "kill", "heal", "laugh", "weep" }

CHORUS_PREFIX="To every thing"
CHORUS_INFIX="there is a season"
CHORUS_SUFFIX="and a time to every purpose under heaven."
CHORUS_SEPARATOR=", turn, turn, turn, "
CHORUS = $CHORUS_PREFIX   ${CHORUS_SEPARATOR}   $CHORUS_INFIX   $CHORUS_SEPARATOR   ${CHORUS_SUFFIX}

SPECIFIC_PREFIX="A time to "

UNUSED=${SOMETHING}

echo $CHORUS

foreach SPECIFIC in $SPECIFICS {
   echo $SPECIFIC_PREFIX   " "   ${SPECIFIC}
}

echo ${CHORUS}

Then the output I would want would be:

CHORUS
CHORUS_INFIX
CHORUS_PREFIX
CHORUS_SEPARATOR
CHORUS_SUFFIX
SOMETHING
SPECIFIC
SPECIFIC_PREFIX
SPECIFICS

A couple things to note:

  1. ${CHORUS} and $CHORUS (for example) both refer to the same variable, named CHORUS
  2. The UNUSED variable is not in the output, because it is not actually used in the script (despite having been defined in it)
  3. The SOMETHING variable, used only to define the unused UNUSED variable, is in the output, since it was used
  4. The various CHORUS_xxx variables are only used in the one single line defining the CHORUS variable, but they are all present in the output.

CodePudding user response:

Using an Extended regular expression:

me@lappy386:/tmp$ grep -Eoi '\$(\{[a-z0-9_] \}|[a-z0-9_] )' \
  /tmp/example |tr -d '{}$'|sort|uniq
CHORUS
CHORUS_INFIX
CHORUS_PREFIX
CHORUS_SEPARATOR
CHORUS_SUFFIX
SOMETHING
SPECIFIC
SPECIFIC_PREFIX
SPECIFICS

CodePudding user response:

Using grep and sed

grep -oE '\$[A-Za-z_] |\${[A-Za-z_] }' inputFile|  sed -r 's/[${}]//g' | sort | uniq

Example :

$ grep -oE '\$[A-Za-z_] |\${[A-Za-z_] }' inputFile |  sed -r 's/[${}]//g' | sort | uniq
CHORUS
CHORUS_INFIX
CHORUS_PREFIX
CHORUS_SEPARATOR
CHORUS_SUFFIX
SOMETHING
SPECIFIC
SPECIFICS
SPECIFIC_PREFIX
  • Related