`

Bash字符串处理(与Java对照) - 7.字符串与默认值

阅读更多

Bash字符串处理(与Java对照) - 7.字符串与默认值

In Java

StringUtils.defaultString

在字符串为null的时候,返回默认值为""或者指定的字符串。主要是为了避免检查null对象。

org.apache.commons.lang.StringUtils defaultString方法 写道
public static String defaultString(String str)

Returns either the passed in String, or if the String is null, an empty String ("").

StringUtils.defaultString(null) = ""
StringUtils.defaultString("") = ""
StringUtils.defaultString("bat") = "bat"


Parameters:
str - the String to check, may be null
Returns:
the passed in String, or the empty String if it was null

public static String defaultString(String str, String defaultStr)

Returns either the passed in String, or if the String is null, the value of defaultStr.

StringUtils.defaultString(null, "NULL") = "NULL"
StringUtils.defaultString("", "NULL") = ""
StringUtils.defaultString("bat", "NULL") = "bat"


Parameters:
str - the String to check, may be null
defaultStr - the default String to return if the input is null, may be null
Returns:
the passed in String, or the default if it was null

 

StringUtils.defaultIfEmpty

在字符串为null或者空串时,返回指定的默认值。

org.apache.commons.lang.StringUtils defaultIfEmpty方法 写道
public static String defaultIfEmpty(String str, String defaultStr)

Returns either the passed in String, or if the String is empty or null, the value of defaultStr.

StringUtils.defaultIfEmpty(null, "NULL") = "NULL"
StringUtils.defaultIfEmpty("", "NULL") = "NULL"
StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
StringUtils.defaultIfEmpty("", null) = null


Parameters:
str - the String to check, may be null
defaultStr - the default String to return if the input is empty ("") or null, may be null
Returns:
the passed in String, or the default
 

StringUtils.defaultIfBlank

在字符串为null,空串或者空白串的时候,返回指定的默认值。

org.apache.commons.lang.StringUtils defaultIfBlank方法 写道
public static String defaultIfBlank(String str, String defaultStr)

Returns either the passed in String, or if the String is whitespace, empty ("") or null, the value of defaultStr.

StringUtils.defaultIfBlank(null, "NULL") = "NULL"
StringUtils.defaultIfBlank("", "NULL") = "NULL"
StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
StringUtils.defaultIfBlank("bat", "NULL") = "bat"
StringUtils.defaultIfBlank("", null) = null


Parameters:
str - the String to check, may be null
defaultStr - the default String to return if the input is whitespace, empty ("") or null, may be null
Returns:
the passed in String, or the default

 

In Bash

在学习Bash多年以后,我终于记住了以下几个参数扩展方法,关键点在于有没有冒号(:)。虽然这些默认值替换方法也可以用if语句罗嗦的解决掉,但远没有这些方法来的简洁。

 

Use Default Values: ${parameter-default}, ${parameter:-default}

${parameter-default} -- 如果变量parameter没被声明, 那么就使用默认值.
${parameter:-default} -- 如果变量parameter没被设置, 那么就使用默认值.
注:“(没)被声明”与“(没)被设置”在是否有 “:” 号的句式差别中仅仅是触发点的不同而已。

“被声明”的触发点显然要比“被设置”的要低,

“被设置”是在“被声明”的基础上而且不能赋值(设置)为空(没有赋值/设置为空)。

man bash 写道
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise,
the value of parameter is substituted.

omitting the colon results in a test only for a parameter that is unset.
 

用途:常用来指定命令行参数的默认值。

比如:DIR=${1:-/root}  意思是说,当第一个命令行参数没有或者为空时,默认值为/root。

比如:DIR=${1-/root}  当没有参数时(第一个命令行参数没有声明,当然就是没有参数),默认值为/root。

 

Assign Default Values: ${parameter=default}, ${parameter:=default}

${parameter=default} -- 如果变量parameter没被声明, 那么就把它的值设为default.
${parameter:=default} -- 如果变量parameter没被设置, 那么就把它的值设为default.

 

man bash 写道
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter.
The value of parameter is then substituted. Positional parameters and special parameters may not be
assigned to in this way.
 

 

: ${parameter:=default}

注意前面加上了冒号(:),即空命令,等同于下面的写法:

if [ -z "$parameter" ]; then parameter=default; fi

[ -z "$parameter" ] && parameter=default

 

Use Alternate Value: ${parameter+alt_value}, ${parameter:+alt_value}

${parameter+alt_value} -- 如果变量parameter被声明了, 那么就使用alt_value, 否则就使用null字符串.
${parameter:+alt_value} -- 如果变量parameter被设置了, 那么就使用alt_value, 否则就使用null字符串.

man bash 写道
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of
word is substituted.
 

 

Display Error if Null or Unset: ${parameter?err_msg}, ${parameter:?err_msg}

${parameter?err_msg} -- 如果parameter被声明了, 那么就使用设置的值, 否则打印err_msg错误消息.
${parameter:?err_msg} -- 如果parameter被设置了, 那么就使用设置的值, 否则打印err_msg错误消息.

 

man bash 写道
${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to
that effect if word is not present) is written to the standard error and the shell, if it is not inter-
active, exits. Otherwise, the value of parameter is substituted.
 

用途1:判断环境变量是否设置了

: ${HOME?}

用途2:判断某些变量是否设置了

: ${VAR?}

用途3:判断位置参数是否设置了

: ${1?}

 

示例:某脚本接受一个参数,如果没有提供就打印使用帮助并退出(注意冒号的使用)。

#!/bin/sh

: ${1?usage: $0 <file>}
 

[root@jfht ~]# ./test1.sh
./test1.sh: line 3: 1: usage: ./test1.sh <file>

[root@jfht ~]# ./test1.sh 1.txt
[root@jfht ~]#

 

如果你对空命令(冒号)还不是很了解,请参见“我使用过的Linux命令之:(冒号) - 啥也不做(除了……) ”。

 

 

本文链接:http://codingstandards.iteye.com/blog/1172191   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)

下节内容:Bash字符串处理(与Java对照) - 8.计算字符串长度

 

 

5
4
分享到:
评论
1 楼 superlittlefish 2011-10-08  
一直都记不住,不知道楼主有什么好办法.

相关推荐

Global site tag (gtag.js) - Google Analytics