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?
evalloop near the beginning looks awkward and dangerous. If the file is executable code, why do you not simplysource file?sourceseems not proper for simple text file right?evalit you can certainlysourceit as well.