1

Having following text file which contain ip_address variable. File as follows

$ cat file
ip_address=10.78.1.0
filename=test.bin

Now having bash script which check if ip_address defined( or available or not)

#!/bin/bash

for list in $(cat file)
do
    eval $list
done

${ip_Address:?Error \$IP_Address is not defined}

[ -z ${ip_Address:-""} ] && printf "No IPaddress\n" || echo "$ip_Address"

Now if my file not contain line for ip_address variable then script is break here but if there then it again check if ip_adress contain any value of not.

But i not want to break my script instead if variable not available the want to do something

like

#!/bin/bash

for list in $(cat file)
do
    eval $list
done

if [ variable not available ]
then
    #do something
else
    #check variable set or not
    [ -z ${ip_Address:-""} ] && printf "No IP address\n" || echo "$ip_Address"
fi

Having tried using -z flag (actually this flag check variable empty or not but not for availability of variable) like this

if [ -z  $ip_Address ]
then
    #do something
else 
    #rest of code
fi

But it fails in following conditions

case 1: If my file as follows

$ cat file
  filename=test.bin

then it must go in if.. block and it does.So it's not problem

case 2 :

If my file as follows

$ cat file
  ip_address=
  filename=test.bin

then it must go in else.. block but it does't. So it's problem

So how can i differentiate if variable defined or variable available in bash?

9
  • does this file also a shell script? Commented Mar 25, 2014 at 11:35
  • No it's simple file like text file. Commented Mar 25, 2014 at 11:37
  • The eval loop near the beginning looks awkward and dangerous. If the file is executable code, why do you not simply source file? Commented Mar 25, 2014 at 11:43
  • @tripleee you are right but source seems not proper for simple text file right? Commented Mar 25, 2014 at 11:44
  • Why not? If you can eval it you can certainly source it as well. Commented Mar 25, 2014 at 11:48

4 Answers 4

4

If you are using bash 4.2 (the latest, although 4.3 should be released soon...), you can use the -v conditional operator to test if a variable is set.

if [[ -v ip_Address ]]; then
    printf "ip_Address is set\n";
fi

Note that the argument for -v is the name of the variable you are testing, so you don't prefix it with a $.

Sign up to request clarification or add additional context in comments.

4 Comments

That's brilliant -- And of course I've been asking myself Why Bash doesn't have a "defined() function". Where (though) is this documented? I have Bash v4.3 and I checked: man bash, man test, mythical google ... still looking. I did some testing of my own too. ____ -v ip_Address, works with just one bracket-pair( [... ] ). Also, it works on a defined variable which was undefined with unset ip_Address . That is very handy for me as I wish to use this feature to turn UNIT_TEST on / off like a light-switch.
Seniors moment; in the old days such arguments and variables were mostly called "switch-es", just like light-switches. So if a <something> was undefined; it would just be in the state of: OFF -- Which would be like: false, 00, NULL, invalid, etc. the suitable nothing-too-see-here state.
It's in the man page for 4.3 at least (I don't have a 4.2 handy to check), but it's not in alphabetical order with the rest of the operators for conditional expressions. It's near the bottom of the list, just before the string comparison operators like == et al.
Found: Bash Conditional Expressions and on man pages via grep. OK thanks.
3

You can differentiate between unset, set but empty, and non-empty using the ${var-value} substitutions.

case ${ip_address-UNSET} in UNSET) echo "It's unset." ;; esac
case ${ip_address:-EMPTY} in EMPTY) echo "It's set, but empty." ;; esac
case ${ip_address:+SET} in SET) echo "It's set and nonempty." ;; esac

This is just for demonstration; your logic would probably look quite different.

See also http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Comments

1

Use test's -n flag instead of -z.

This will test if the variable has content and will also identify if the variable is unset.

if [ -n "$ip_Address" ]
then
    # ip_address is set 
else 
    # ip_address is no content OR is not set
fi

Comments

0

For me the following lines do the job:

#!/bin/bash

if test $# -ne 1;
then
    echo "Usage: check_for_ip.sh infile"
    exit
fi

. $1

test -z "${ip_address}" && echo "No IP address" || echo "IP address is: ${ip_address}"

Testfiles:

$ cat file1 
ip_address=
filename=test.bin
$ cat file2 
ip_address=10.78.1.0
filename=test.bin
$ cat file3
filename=test.bin

Testresults:

$ bash check_for_ip.sh file1
No IP address
$ bash check_for_ip.sh file2
IP address is: 10.78.1.0
$ bash check_for_ip.sh file3
No IP address

I'm not sure if I understood the problem because this looks mostly like your solution; maybe you just missing the "" in the test.

1 Comment

As per your result, case 1 and case 3 looks identical which i not want means in case 1 variable is there but no value so it's ok but in case 3 variable not available. So require to differentiate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.