猿问
下载APP

在Bash中测试非零长度字符串:[ - n“$ var”]或[“$ var”]

在Bash中测试非零长度字符串:[ - n“$ var”]或[“$ var”]

我已经看到bash脚本以两种不同的方式测试非零长度字符串。大多数脚本使用-n选项:

#!/bin/bash# With the -n optionif [ -n "$var" ]; then
  # Do something when var is non-zero lengthfi

但是-n选项并不是真正需要的:

# Without the -n optionif [ "$var" ]; then
  # Do something when var is non-zero lengthfi

哪种方式更好?

同样,这是测试零长度的更好方法:

if [ -z "$var" ]; then
  # Do something when var is zero-lengthfi

要么

if [ ! "$var" ]; then
  # Do something when var is zero-lengthfi


陪伴而非守候
浏览 45回答 3
3回答

天涯尽头无女友

这是一个更完整的版本,显示[(aka test)和[[。之间的更多差异。下表显示是否引用变量,是使用单括号还是双括号以及变量是否仅包含空格是影响使用或不使用测试-n/-z是否适合检查变量的因素。&nbsp; &nbsp; &nbsp;| 1a&nbsp; &nbsp; 2a&nbsp; &nbsp; 3a&nbsp; &nbsp; 4a&nbsp; &nbsp; 5a&nbsp; &nbsp; 6a&nbsp; &nbsp;| 1b&nbsp; &nbsp; 2b&nbsp; &nbsp; 3b&nbsp; &nbsp; 4b&nbsp; &nbsp; 5b&nbsp; &nbsp; 6b&nbsp; &nbsp; &nbsp;| [&nbsp; &nbsp; &nbsp;["&nbsp; &nbsp; [-n&nbsp; &nbsp;[-n"&nbsp; [-z&nbsp; &nbsp;[-z" | [[&nbsp; &nbsp; [["&nbsp; &nbsp;[[-n&nbsp; [[-n" [[-z&nbsp; [[-z"-----+------------------------------------+------------------------------------unset| false false true&nbsp; false true&nbsp; true | false false false false true&nbsp; truenull | false false true&nbsp; false true&nbsp; true | false false false false true&nbsp; truespace| false true&nbsp; true&nbsp; true&nbsp; true&nbsp; false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsezero | true&nbsp; true&nbsp; true&nbsp; true&nbsp; false false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsedigit| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsechar | true&nbsp; true&nbsp; true&nbsp; true&nbsp; false false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsehyphn| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsetwo&nbsp; | -err- true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falsepart | -err- true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseTstr | true&nbsp; true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseFsym | false true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseT=&nbsp; &nbsp;| true&nbsp; true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseF=&nbsp; &nbsp;| false true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseT!=&nbsp; | true&nbsp; true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseF!=&nbsp; | false true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseTeq&nbsp; | true&nbsp; true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseFeq&nbsp; | false true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseTne&nbsp; | true&nbsp; true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false falseFne&nbsp; | false true&nbsp; -err- true&nbsp; -err- false| true&nbsp; true&nbsp; true&nbsp; true&nbsp; false false如果您想知道变量的长度是否为非零,请执行以下任一操作:在单括号中引用变量(第2a列)-n在单括号中使用和引用变量(第4a列)使用带或不带引号的双括号和有或没有-n(第1b - 4b列)请注意,在第1a列中,从标有“two”的行开始,结果表明[正在评估变量的内容,就好像它们是条件表达式的一部分一样(结果与“T”或“F”中隐含的断言相匹配)描述栏)。何时[[使用(第1b列),变量内容被视为字符串而不进行评估。列3a和5a中的错误是由变量值包括空格而变量未加引号的事实引起的。同样,如列3b和5b所示,[[将变量的内容作为字符串进行计算。相应地,对于零长度字符串的测试,列6a,5b和6b显示了正确的方法。还要注意,如果否定显示比使用相反操作更清晰的意图,则可以否定这些测试中的任何一个。例如:if ! [[ -n $var ]]。如果您正在使用[,那么确保您没有获得意外结果的关键是引用该变量。使用[[,没关系。被抑制的错误消息是“一元运算符预期”或“二元运算符预期”。这是生成上表的脚本。#!/bin/bash# by Dennis Williamson# 2010-10-06, revised 2010-11-10# for http://stackoverflow.com/q/3869072# designed to fit an 80 character terminaldw=5&nbsp; &nbsp; # description column widthw=6&nbsp; &nbsp; &nbsp;# table column widtht () { printf '%-*s' "$w" " true"; }f () { [[ $? == 1 ]] && printf '%-*s' "$w" " false" || printf '%-*s' "$w" " -err-"; }o=/dev/nullecho '&nbsp; &nbsp; &nbsp;| 1a&nbsp; &nbsp; 2a&nbsp; &nbsp; 3a&nbsp; &nbsp; 4a&nbsp; &nbsp; 5a&nbsp; &nbsp; 6a&nbsp; &nbsp;| 1b&nbsp; &nbsp; 2b&nbsp; &nbsp; 3b&nbsp; &nbsp; 4b&nbsp; &nbsp; 5b&nbsp; &nbsp; 6b'echo '&nbsp; &nbsp; &nbsp;| [&nbsp; &nbsp; &nbsp;["&nbsp; &nbsp; [-n&nbsp; &nbsp;[-n"&nbsp; [-z&nbsp; &nbsp;[-z" | [[&nbsp; &nbsp; [["&nbsp; &nbsp;[[-n&nbsp; [[-n" [[-z&nbsp; [[-z"'echo '-----+------------------------------------+------------------------------------'while read -r d tdo&nbsp; &nbsp; printf '%-*s|' "$dw" "$d"&nbsp; &nbsp; case $d in&nbsp; &nbsp; &nbsp; &nbsp; unset) unset t&nbsp; ;;&nbsp; &nbsp; &nbsp; &nbsp; space) t=' '&nbsp; &nbsp; ;;&nbsp; &nbsp; esac&nbsp; &nbsp; [ $t ]&nbsp; &nbsp; &nbsp; &nbsp; 2>$o&nbsp; && t || f&nbsp; &nbsp; [ "$t" ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && t || f&nbsp; &nbsp; [ -n $t ]&nbsp; &nbsp; &nbsp;2>$o&nbsp; && t || f&nbsp; &nbsp; [ -n "$t" ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; [ -z $t ]&nbsp; &nbsp; &nbsp;2>$o&nbsp; && t || f&nbsp; &nbsp; [ -z "$t" ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; echo -n "|"&nbsp; &nbsp; [[ $t ]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && t || f&nbsp; &nbsp; [[ "$t" ]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && t || f&nbsp; &nbsp; [[ -n $t ]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; [[ -n "$t" ]]&nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; [[ -z $t ]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; [[ -z "$t" ]]&nbsp; &nbsp; &nbsp; &nbsp;&& t || f&nbsp; &nbsp; echodone <<'EOF'unsetnullspacezero&nbsp; &nbsp; 0digit&nbsp; &nbsp;1char&nbsp; &nbsp; chyphn&nbsp; &nbsp;-ztwo&nbsp; &nbsp; &nbsp;a bpart&nbsp; &nbsp; a -aTstr&nbsp; &nbsp; -n aFsym&nbsp; &nbsp; -h .T=&nbsp; &nbsp; &nbsp; 1 = 1F=&nbsp; &nbsp; &nbsp; 1 = 2T!=&nbsp; &nbsp; &nbsp;1 != 2F!=&nbsp; &nbsp; &nbsp;1 != 1Teq&nbsp; &nbsp; &nbsp;1 -eq 1Feq&nbsp; &nbsp; &nbsp;1 -eq 2Tne&nbsp; &nbsp; &nbsp;1 -ne 2Fne&nbsp; &nbsp; &nbsp;1 -ne 1EOF

拉丁的传说

就Bash而言,最好使用更强大的功能 [[。通常情况if [[ $var ]]; then&nbsp; &nbsp;# var is set and it is not emptyif [[ ! $var ]]; then # var is not set or it is set to an empty string以上两种结构看起来干净可读。在大多数情况下,它们应该足够了。请注意,我们不需要引用内部的变量扩展,[[因为没有单词拆分和globbing的危险。为了防止shellcheck的软抱怨[[ $var ]]和[[ ! $var ]],我们可以使用的-n选项。罕见的情况在极少数情况下,我们必须区分“被设置为空字符串”与“未被设置”,我们可以使用这些:if [[ ${var+x} ]]; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# var is set but it could be emptyif [[ ! ${var+x} ]]; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# var is not setif [[ ${var+x} && ! $var ]]; then # var is set and is empty我们也可以使用-v测试:if [[ -v var ]]; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# var is set but it could be emptyif [[ ! -v var ]]; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# var is not setif [[ -v var && ! $var ]]; then&nbsp; &nbsp;# var is set and is emptyif [[ -v var && -z $var ]]; then&nbsp; # var is set and is empty

手掌心

正确答案如下:if&nbsp;[[&nbsp;-n&nbsp;$var&nbsp;]]&nbsp;;&nbsp;then &nbsp;&nbsp;blahfi注意使用[[...]],正确处理为您引用变量。
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答