2016-11-16 102 views
1

一個shell腳本的功能裏面我看到這樣的事情

func() 
{ 
local x 
x=${1:3:1} 
... 
} 

是什麼x=${1:3:1}意思?我知道$1,$2$3是函數的參數。那麼上面的陳述意味着x = $1:$2:$3

此外,如果有人可以建議我如何谷歌搜索這樣的特殊字符真的很有幫助?任何標準關鍵字?我嘗試搜索「什麼是」:「在shell腳本中」等。但是,當試圖搜索特殊字符時,結果是隨機的。

回答

5

這在shell中稱爲參數擴展。

$ {PARAMETER:OFFSET:LENGTH}

這一次只能擴展參數值的部分,因爲開始的位置,也許長度。如果忽略LENGTH,則參數將被擴展到字符串的末尾。如果LENGTH爲負,則將其作爲字符串的第二個偏移量,從字符串的末尾開始計數。

OFFSET和LENGTH可以是任何算術表達式。該OFFSET從0開始,而不是在1

比如讓說的參數是一個字符串,

MyString的=「在你接受什麼樣的自由主義和保守你發送」

回聲$ {MyString的:34:13}

上述會給你以下

CON servative

因爲它會計算第33個(索引從0開始)字符,它將以字符「c」開始,然後計數(13字符)長度。

因此,在您的情況下,$ 1是您傳遞給腳本的參數,然後將其偏移3個字符並採用長度爲1的字符串並將其初始化爲x。

更多在這裏閱讀:http://wiki.bash-hackers.org/syntax/pe#substring_expansion

1

使用手冊頁,所有信息都在那裏。 man bash

${parameter:offset:length} 
      Substring Expansion. Expands to up to length characters of the 
      value of parameter starting at the character specified by off‐ 
      set. If parameter is @, an indexed array subscripted by @ or *, 
      or an associative array name, the results differ as described 
      below. If length is omitted, expands to the substring of the 
      value of parameter starting at the character specified by offset 
      and extending to the end of the value. length and offset are 
      arithmetic expressions (see ARITHMETIC EVALUATION below). 
+0

謝謝。順便說一下,我使用的是adb shell。是adb shell bash? –

+1

'if [adb = bash];然後回聲「是的,它是」;否則回聲「不,它是不同的」; fi' –

+0

它說不,它是不同的。但我試圖打印$ adb。它是空的。這是正確的方法來檢查? –

2

這是一個參數擴展,許多與${開始的一部分。

Like $ {parameter:-word},$ {parameter:= word},$ {parameter:?word},$ {parameter:+ word}等等。

這一個(專用於ksh,bash和zsh):${parameter:offset:length}從偏移處開始提取長度字符(可選,如果缺失,參數中字符串的其餘部分)。隨着bash手冊中描述的幾個細節。

${name:offset:length} 
    Substring Expansion. Expands to up to length characters of the value 
    of parameter starting at the character specified by offset. If parameter 
    is @, an indexed array subscripted by @ or *, or an associative 
    array name, the results differ as described below. If length is omitted, 
    expands to the substring of the value of parameter starting at the character 
    specified by offset and extending to the end of the value. length and 
    offset are arithmetic expressions (see ARITHMETIC EVALUATION below). 

    If offset evaluates to a number less than zero, the value is used as an 
    offset in characters from the end of the value of parameter. If length 
    evaluates to a number less than zero, it is interpreted as an offset in 
    characters from the end of the value of parameter rather than a number 
    of characters, and the expansion is the characters between offset and 
    that result. Note that a negative offset must be separated from the 
    colon by at least one space to avoid being confused with the :- expansion. 

    If parameter is @, the result is length positional parameters 
    beginning at offset. A negative offset is taken relative to one greater 
    than the greatest positional parameter, so an offset of -1 evaluates 
    to the last positional parameter. It is an expansion error if length 
    evaluates to a number less than zero. 

    If parameter is an indexed array name subscripted by @ or *, the result 
    is the length members of the array beginning with ${parameter[offset]}. 
    A negative offset is taken relative to one greater than the maximum 
    index of the specified array. It is an expansion error if length evaluates 
    to a number less than zero. 

    Substring expansion applied to an associative array produces undefined 
    results. 

    Substring indexing is zero-based unless the positional parameters 
    are used, in which case the indexing starts at 1 by default. If 
    offset is 0, and the positional parameters are used, $0 is prefixed 
    to the list. 
0

試試這個:

集ABCDEFG

回聲$ {1:3:1}

這是得到一個子。通常$ {}是指數組變量(在這種情況下是字符數組)

1

x = $ {1:3:1}是什麼意思?

它的子切斷,英文:使用字符串中$1,拉1字符開始於索引3(其中索引是從0開始)。所以如果$1 === "foobar",那麼${1:3:1} === "b"

我知道$ 1,$ 2和$ 3是函數的參數。那麼上面的陳述意味着x = $ 1:$ 2:$ 3?

沒有,鄰接代表字符串連接:x="$1$2$3"$1$2,並且$3串接的字符串的結果。

此外,如果有人可以建議我如何谷歌搜索這樣的特殊字符是真的有幫助嗎?任何標準關鍵字?我嘗試搜索「什麼是」:「在shell腳本中」等。但是,當試圖搜索特殊字符時,結果是隨機的。

bash parameter substitution通常讓你進入球場。我知道我不記得bash可以用數據處理的所有不同的語法方式,因此將「參數替換」提交給內存是有好處的。字符串操作恰好是參數替換之前的章節。

相關問題