什麼是像這樣使用一個bash變量的含義:
${Server?}
什麼是像這樣使用一個bash變量的含義:
${Server?}
它的工作原理幾乎相同(從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 interactive, exits. Otherwise, the value of parameter is substituted.
那特定變體檢查以確保變量存在s(既定義了也不爲空)。如果是這樣,它使用它。如果不是,則輸出由word
指定的錯誤消息(如果沒有word
,則輸出錯誤消息)並終止腳本。
該和之間的實際差值的非結腸版本可以在bash
手冊頁的部分上面引述找到:
當不執行子串擴展,使用形式如下記載,
bash
測試爲一個參數這是未設置或爲空。 省略冒號只會導致對未設置的參數進行測試。
換句話說,上面的部分可以被修改爲讀(基本上取出 「空」 位):
${parameter?word}
Display Error if Unset. If parameter is 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 interactive, exits. Otherwise, the value of parameter is substituted.
所不同的是如此說明:
pax> unset xyzzy ; export plugh=
pax> echo ${xyzzy:?no}
bash: xyzzy: no
pax> echo ${plugh:?no}
bash: plugh: no
pax> echo ${xyzzy?no}
bash: xyzzy: no
pax> echo ${plugh?no}
pax> _
在那裏,您可以看到,雖然未設置和空變量導致與:?
錯誤,但只有未設置的一個錯誤與?
。
這意味着,如果沒有定義的變量腳本應該中止
實施例:
#!/bin/bash
echo We will see this
${Server?Oh no! server is undefined!}
echo Should not get here
該腳本將打印出第一個回顯,並顯示「Oh no!...」錯誤信息。
查看所有對於bash這裏的變量替換: http://tldp.org/LDP/abs/html/parameter-substitution.html
而且沒有字,有一個默認錯誤消息「parameter null or not set」(相同的消息有或沒有冒號)。 – 2016-07-07 13:39:58