1

My objective is to create a list of variables that can be called or referenced by any test script that is being executed. There are a lot of test scripts on the system but the variables are convoluted and hard to understand. For each type of setup, there are different variable names for the same thing. I want to rename the variables and modify the test scripts to reference to the same variables so it's easier to understand.

Here is an example of how one variable is defined in a script:

RightNumberOfDevices=$( lspci -n | egrep --count "8086:[[xdigitx]][xxdigit]][[:xdigit:]][[:xdigit:]]" )

In another script, I'll see this:

TotalNumberOfDevices=$( lspci -n | egrep --count "8086:[[xdigitx]][xxdigit]][[:xdigit:]][[:xdigit:]]" )

I want to be able to declare TotalNumberOfDevices in some kind of startup script so that the value in that variable can be accessed throughout the environment.

This is the code that I'm trying to implement:

ethcounter=$(ifconfig -a | egrep --count "eth[0-9]+")
ethindex0=$((ethcounter-1))

declare -a eth                  #This declares that "eth" is an array variable

for ((i=0; i<=ethindex0; i++))
 do
        eth[$i]=$(ifconfig eth$i | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' )
        echo "eth[$i]" = "${eth[$i]}"
        set ${eth[$i]}
 done

All I'm trying to do is store the IP addresses of each Ethernet device and make them available to other scripts.

closed as too broad by Michael Homer, Jakuje, Scott, Anthon, Stephen Kitt Jan 17 '16 at 14:00

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1. What is your test environment? 2. What do you mean when you say "shell scripts export those variables"? If you're running all your tests from a shell, then just export the environment variables before running your tests. – gardenhead Jan 16 '16 at 10:43
  • See To whom do environment variables belong? – Scott Jan 17 '16 at 0:02
  • @gardenhead What I meant to say was that I wanted to declare the variables when the system boots up so that any script that I execute can call those variables. I edited my question so I hope it clears up what I'm trying to do. – WeSC Jan 18 '16 at 22:42
  • @WeSC In that case you're correct: just export them in your .bash_profile. – gardenhead Jan 19 '16 at 0:12
  • Your question is still all over the map. “I'm having a hard time understanding the difference between exporting variables, and global variables.” — sounds like a broad question about terminology and concepts.  “It seems like exporting variables allows me to access variables from other shells, but I don’t understand how this works.” — so, are you asking about how the environment works?  “… the variables are convoluted and hard to understand” — there you go again, talking about “understanding”. … (Cont’d) – G-Man Jan 19 '16 at 0:45
3

Global vs. local variables is a difference within a shell only (with respect to shell functions).

Exported vs. non-exported variables is a difference to subprocesses (not only subshells) of the shell only (only exported variables become environment variables for subprocesses).

Not the answer you're looking for? Browse other questions tagged or ask your own question.