Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a variable exists and has been initialized

I have to execute a function which has to test if a variable has been correctly defined in Bash and must use its associated value.

For instance, these variables are initialized at the top of the script.

#!/bin/bash

var1_ID=0x04
var2_ID=0x05
var3_ID=0x06
var4_ID=0x09

I would like to call the script named test as follows:

./test var1

The current implemented function is:

function Get()
{
     if [ $1"_ID" != "" ]; then
         echo "here"
         echo $(($1_ID))
     else
         exit 0
     fi
}

I don't understand why I obtain here even if I enter ./test toto or something else.

Do I need to use a specific command, such as grep?

like image 500
ogs Avatar asked Nov 24 '14 15:11

ogs


People also ask

How can an object be tested to find out if it is initialized?

Just check simply with a if-clause if object ain't null. If that's the case it has been initialized.

How do you check if a variable is initialized and how do you check if a variable has a value?

There's no reasonable way to check whether a value has been initialized. If you care about whether something has been initialized, instead of trying to check for it, put code into the constructor(s) to ensure that they are always initialized and be done with it.

How do you check if a variable has been initialized Python?

Python doesn't have a specific function to test whether a variable is defined, since all variables are expected to have been defined before use, even if initially assigned the None object.

How do you check if a variable is set or not in JS?

JavaScript has a built-in function to check whether a variable is defined/initialized or undefined. Note: The typeof operator will check whether a variable is defined or not. The typeof operator doesn't throw a ReferenceError exception when it is used with an undeclared variable.


2 Answers

Use parameter expansion:

: ${var:?}

Remove the colon if the empty string is a valid value (i.e., you only want to test for definedness).

: ${var?}

If you don't want the script to stop on the problem, you can use

if [[ ${var:+1} ]] ; then
    # OK...
else
    echo Variable empty or not defined. >&2
fi

Documented under Parameter Expansion in man bash:

When not performing substring expansion, using the forms documented below (e.g., :-), bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset.

${parameter:?word}

Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

${parameter:+word}

Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

like image 194
choroba Avatar answered Nov 01 '22 11:11

choroba


You probably want to use indirect expansion: ${!variable} and then -n to check if it has been defined:

The indirect expansion consists in calling a variable with another variable. That is, as the variable name may be changing, instead of saying $a we say ${!var} and var=a.

$ cat a
var1_ID=0x04
var2_ID=0x05
var3_ID=0x06
var4_ID=0x09

for i in {1..5}; do
   v="var${i}_ID"
   if [ -n "${!v}" ]; then             # <-- this expands to varX_ID
      echo "$v set to value: ${!v}"
   else
      echo "$v not set"
   fi
done

If we execute, we get:

$ ./a
var1_ID set to value: 0x04
var2_ID set to value: 0x05
var3_ID set to value: 0x06
var4_ID set to value: 0x09
var5_ID not set

From man test:

-n STRING

the length of STRING is nonzero

like image 25
fedorqui 'SO stop harming' Avatar answered Nov 01 '22 11:11

fedorqui 'SO stop harming'