亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 關(guān)于雙引號(hào)的困惑 [打印本頁(yè)]

作者: long1225    時(shí)間: 2010-03-23 17:44
標(biāo)題: 關(guān)于雙引號(hào)的困惑
腳本A

#!/bin/bash
if [ "${x+X}" = X ]  #if x is set
then
        if [ -n $x ] #if x is not empty
        then
        printf " \$x = %s\n" "$x"
        else
        printf " \$x is set but empty \n"
        fi
else
printf " \$x is not set.\n"
fi


腳本B

#!/bin/bash
if [ "${x+X}" = X ]  #if x is set
then
        if [ -n "$x" ] #if x is not empty
        then
        printf " \$x = %s\n" "$x"
        else
        printf " \$x is set but empty \n"
        fi
else
printf " \$x is not set.\n"
fi


紅色部分的不同,造成了腳本的運(yùn)行錯(cuò)誤,不加引號(hào)的話,即使變量x為空,打印出來(lái)卻是$x=     但是加上引號(hào)后,當(dāng)x為空時(shí),可以識(shí)別出來(lái)為空



但是我想不明白在這里$x 和"$x" 有什么區(qū)別,求教大俠!!
作者: netocool    時(shí)間: 2010-03-23 17:46
試一下這樣:

'"$x"'
作者: long1225    時(shí)間: 2010-03-23 17:49
'"$x"' 的的結(jié)果和$x一樣,無(wú)法識(shí)別空變量
作者: blackold    時(shí)間: 2010-03-23 18:12
因?yàn)閣ord splitting
作者: long1225    時(shí)間: 2010-03-23 18:55
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly "'<space><tab><newline>'", the default, then any sequence of IFS characters serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters "space" and "Tab" are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IF whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

Explicit null arguments ("""" or "''") are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.


Expansion and word splitting
  If no expansion occurs, no splitting is performed.




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2