简体   繁体   English

我如何确切地确定变量是否未使用bash在文件中定义

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

Having following text file which contain ip_address variable. 以下文本文件包含ip_address变量。 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) 现在具有bash脚本,用于检查ip_address定义(或是否可用)

#!/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. 现在,如果我的文件中不包含ip_address变量行,则脚本在此处中断,但如果存在,则再次检查ip_adress包含不包含任何值。

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 尝试使用-z标志(实际上此标志检查变量是否为空,但不检查变量的可用性),如下所示

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 情况1:如果我的文件如下

$ cat file
  filename=test.bin

then it must go in if.. block and it does.So it's not problem 那么它必须进入if..块,并且可以。所以这不是问题

case 2 : 情况2:

If my file as follows 如果我的文件如下

$ cat file
  ip_address=
  filename=test.bin

then it must go in else.. block but it does't. 那么它必须进入else..块,但不是。 So it's problem 所以有问题

So how can i differentiate if variable defined or variable available in bash? 那么如何区分bash中定义的变量或可用的变量呢?

You can differentiate between unset, set but empty, and non-empty using the ${var-value} substitutions. 您可以使用${var-value}替换来区分未设置,设置但为空和非空。

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 另请参见http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

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. 如果您正在使用bash 4.2(最新版本,尽管4.3应该很快发布……),则可以使用-v条件运算符来测试是否设置了变量。

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 $ . 请注意, -v的参数是您要测试的变量的名称 ,因此您不必在其前面加上$

Use test's -n flag instead of -z . 使用test的-n标志而不是-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

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. 也许您只是在测试中遗漏了“”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM