2017-08-03 42 views
1

我相信可能有語法錯誤,但我看過,這就是人似乎把它寫(here) 誰能告訴我爲什麼下面是happening-方式簡單的bash字符串包含壓縮不工作

h="23538_" 
if [[ "$h" == "23538_" ]]; then echo "true"; else echo "false"; fi; 
>> true 
if [[ *"$h"* == "23538_" ]]; then echo "true"; else echo "false"; fi; 
>> false 

也試過:

h=23538 
if [[ *"$h"* == "23538_" ]]; then echo "true"; else echo "false"; fi; 
>> false 

if [[ *$h* == "23538_" ]]; then echo "true"; else echo "false"; fi; 
>> false 

if [[ *{$h}* == "23538_" ]]; then echo "true"; else echo "false"; fi 
>> false 

h='_23538__' 
if [[ *"$h"* == "23538_" ]]; then echo "true"; else echo "false"; fi 

幾句話 - 我試圖找到,如果$ h是「23538_」字符串的子字符串。

希望有人可以幫我... :)

+0

glob模式只能在右側的變量使用的參數。 – anubhava

+0

你的意思是這樣的'if [[「23538_」== *「$ h」*]];然後回顯「真實」;其他回聲「假」; fi' ...? – Dardar1991

+0

它仍然不能正常工作...:/ – Dardar1991

回答

2

你需要換

if [[ "23538_" == *"$h"* ]]; then echo "true"; else echo "false"; fi; 
    > true 
1

模式只能在右手邊可以使用,如果==!=運營商。見man bash[[ expression ]]

當使用==!=運營商來說,串給操作者的右側被認爲是一個圖案並根據該規則進行匹配下面描述 下模式匹配,彷彿extglob shell選項已啓用。

0

如何:

$ echo $h # true subset 
23538 
$ if [[ "23538_" =~ "$h" ]]; then echo "true"; else echo "false"; fi; 
true 

man bash

另外一個二元運算符,=〜,是可用的,具有相同的優先級 如==和!=。當它被使用時, 右邊的字符串被認爲是一個擴展的正則表達式,並相應匹配(如在regex(3)中)。