Jayesh Bhoi
Jayesh Bhoi

Reputation: 25915

How i exactly determine if variable not defined in file using bash

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?

Upvotes: 1

Views: 1114

Answers (4)

chepner
chepner

Reputation: 532208

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 $.

Upvotes: 4

tripleee
tripleee

Reputation: 189809

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

Upvotes: 3

suspectus
suspectus

Reputation: 17288

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

Upvotes: 1

Andreas Florath
Andreas Florath

Reputation: 4612

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.

Upvotes: 0

Related Questions