Bash: Checking existence of environment variable using a string
I have a pretty good idea how to check for the existence of an environment variable, however I am having issues referencing the variable's value.
My script is something like: Code:
#!/usr/bin/env bash Code:
LD_LIBRARY_PATH 1. How can I be assured that "env | grep $var" returns an exact match, and not another similarly named environment variable (e.g. CLASSPATH vs. MYCLASSPATH vs. CLASSPATH_FOO)? 2. How can I display the current setting of the environment variable if it is found to exist? Lastly, if it is not too much trouble to ask, how would I develop similar code but for Windows (ie. batch script)? |
untested:
Code:
#!/bin/bash |
Unless you wish to use the env_vars array later, I see no point in creating it first, just read the file. Generally I would recommend against reading with for, however, if you are
certain the file contains complete single strings on each line, I would probably go with: Code:
#!/bin/bash |
NevemTeve & Grail -- Thanks for your replies. I was unaware of the ${!var} feature of bash.
My next step is to see if I can translate your suggestions into batch script. :eek: |
Note that -n will check whether variable is set to a non-empty value, which is not the same as if it is set. If you are interested in whether variable is set (with possibly empty value) or not, you should use:
Code:
if [ x${!var+set} = xset ]; then Code:
eval "[ x\${$var+set} = xset ]" |
mina86 -
I ended up using the -z option (since it seems to suit my needs), along with the critical hints from NevemTeve and Grail. My script, which is larger than the example below, merely is being used to indicate to the end-user whether an environment variable is set or not. If it is set, then I also display its value. Here's an example of what my script looks like: Code:
#!/usr/bin/env bash |
Oh, that's actually invalid. You need to quote the variables:
Code:
for var in $(< file.env); do |
Quote:
|
Script-writers (every programmer, actually) should make a habit of being paranoid with data coming from external sources (keyboard, network, files, environment variables etc)...
|
Quote:
Some ways to fool or otherwise abuse your script are: Code:
$ cat a.sh |
Quote:
|
As shown, if the variable is set in the environment, then you can just test for its existence directly. Or there's also the built-in declare option you can use instead of env. This might be easiest, if the exact format of the output isn't particularly important.
And if you're using bash v4+, I suggest reading the file directly into an array fist with mapfile. Code:
#!/usr/bin/env bash Incidentally, bash from v4.2 also has a new -v test. You can check whether the variable name exists directly, without having to use an indirect reference. Code:
if [[ -v $var ]]; then |
Thanks for the tips. One of the systems that my script runs on, has Bash 3.00.16. The system is a Solaris 10 Sparc.
|
Reading a whole file into memory is a very good way to eat up every bit of RAM.
|
But that depends on how big the file is, of course, and the amount of RAM you have available. I don't think that you're going to get lists of environment variables in the multi-gigabyte range. ;)
Do remember to unset the array when you're done if you're worried about the data sitting there in memory, though. Anyway, the traditional way to load an array from a file, or to process it directly, for that matter, is like this: Code:
while IFS='' read -r line || [[ -n $line ]]; do |
All times are GMT -5. The time now is 04:50 AM. |