⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 str-test.sh

📁 Shall高级编程
💻 SH
字号:
#!/bin/bash#  str-test.sh: Testing null strings and unquoted strings,#+ but not strings and sealing wax, not to mention cabbages and kings . . .# Using   if [ ... ]# If a string has not been initialized, it has no defined value.# This state is called "null" (not the same as zero).if [ -n $string1 ]    # $string1 has not been declared or initialized.then  echo "String \"string1\" is not null."else    echo "String \"string1\" is null."fi  # Wrong result.# Shows $string1 as not null, although it was not initialized.echo# Lets try it again.if [ -n "$string1" ]  # This time, $string1 is quoted.then  echo "String \"string1\" is not null."else    echo "String \"string1\" is null."fi                    # Quote strings within test brackets!echoif [ $string1 ]       # This time, $string1 stands naked.then  echo "String \"string1\" is not null."else    echo "String \"string1\" is null."fi  # This works fine.# The [ ] test operator alone detects whether the string is null.# However it is good practice to quote it ("$string1").## As Stephane Chazelas points out,#    if [ $string1 ]    has one argument, "]"#    if [ "$string1" ]  has two arguments, the empty "$string1" and "]" echostring1=initializedif [ $string1 ]       # Again, $string1 stands naked.then  echo "String \"string1\" is not null."else    echo "String \"string1\" is null."fi  # Again, gives correct result.# Still, it is better to quote it ("$string1"), because . . .string1="a = b"if [ $string1 ]       # Again, $string1 stands naked.then  echo "String \"string1\" is not null."else    echo "String \"string1\" is null."fi  # Not quoting "$string1" now gives wrong result!exit 0# Thank you, also, Florian Wisser, for the "heads-up".

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -