簡體   English   中英

魚殼:驗證字符串是否包含子字符串

[英]Fish Shell: validate if string contains substring

目前,我正在嘗試驗證包含路徑的字符串是否包含特定字符串。

我已經嘗試過bash比較:

  1. 如果[$ path = $ needle ]

  1. 如果[$ path =〜$ needle]

我也嘗試使用“包含”關鍵字。 但是我不確定是否應該將它與字符串一起使用。

不幸的是,這些嘗試都失敗了。 :/

function next_dir
    set foundcwd 0
    set cwd $PWD
    set error 'There is no next directory to navigate to...'

    if [ -z $cwd ]
        echo $error
    else
        echo $cwd
        for d in ../*/
            set needle (string split "/" -- $d)[2]

            if [ $foundcwd = 1 ]
                cd $d
                break
            end

            if [ $cwd =~ $needle ]
                $foundcwd = 1
            end
        end
    end
end

我函數的目標是導航到下一個(同級)目錄。

/ mnt / c / workingdirectory / repoA->當前目錄/ mnt / c / workingdirectory / repoB->導航到repoB

您想要string match

if string match -q  -- $needle $path

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM