Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

bash - 如何检查Bash中是否设置了变量?(How to check if a variable is set in Bash?)

How do I know if a variable is set in Bash? (我如何知道是否在Bash中设置了变量?)

For example, how do I check if the user gave the first parameter to a function? (例如,如何检查用户是否将第一个参数赋予函数?)

function a {
    # if $1 is set ?
}
  ask by prosseek translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Usually) The right way ((通常)正确的方法)

if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi

where ${var+x} is a parameter expansion which evaluates to nothing if var is unset, and substitutes the string x otherwise. (其中${var+x}参数扩展 ,如果未设置var ,则不求值,否则替换字符串x 。)

Quotes Digression (行情离题)

Quotes can be omitted (so we can say ${var+x} instead of "${var+x}" ) because this syntax & usage guarantees this will only expand to something that does not require quotes (since it either expands to x (which contains no word breaks so it needs no quotes), or to nothing (which results in [ -z ] , which conveniently evaluates to the same value (true) that [ -z "" ] does as well)). (可以省略引号(因此我们可以说${var+x}而不是"${var+x}" ),因为这种语法和用法保证了它只会扩展为不需要引号的内容(因为它要么扩展为x (不包含分词符,因此不需要引号)或不包含任何内容(导致[ -z ] ,其便利地求值为[ -z "" ]相同的值(true))。)

However, while quotes can be safely omitted, and it was not immediately obvious to all (it wasn't even apparent to the first author of this quotes explanation who is also a major Bash coder), it would sometimes be better to write the solution with quotes as [ -z "${var+x}" ] , at the very small possible cost of an O(1) speed penalty. (但是,尽管可以安全地省略引号,并且所有人都不会立即看到引号(对于引号解释的第一作者,他也是主要的Bash编码器甚至还不明显),但有时编写解决方案会更好引号为[ -z "${var+x}" ] ,但代价是O(1)速度损失很小。) The first author also added this as a comment next to the code using this solution giving the URL to this answer, which now also includes the explanation for why the quotes can be safely omitted. (第一作者还使用此解决方案在代码旁边添加了该注释作为注释,并给出了该答案的URL,该注释现在还包括为什么可以安全地省略引号的说明。)

(Often) The wrong way ((通常)错误的方式)

if [ -z "$var" ]; then echo "var is blank"; else echo "var is set to '$var'"; fi

This is often wrong because it doesn't distinguish between a variable that is unset and a variable that is set to the empty string. (这通常是错误的,因为它无法区分未设置的变量和设置为空字符串的变量。) That is to say, if var='' , then the above solution will output "var is blank". (也就是说,如果var='' ,则上述解决方案将输出“ var is blank”。)

The distinction between unset and "set to the empty string" is essential in situations where the user has to specify an extension, or additional list of properties, and that not specifying them defaults to a non-empty value, whereas specifying the empty string should make the script use an empty extension or list of additional properties. (在用户必须指定扩展名或其他属性列表且未指定属性默认情况下默认为非空值的情况下,未设置和“设置为空字符串”之间的区别至关重要。使脚本使用空扩展名或其他属性列表。)

The distinction may not be essential in every scenario though. (但是,这种区分可能并非在每种情况下都必不可少。) In those cases [ -z "$var" ] will be just fine. (在这种情况下, [ -z "$var" ]就可以了。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...