Fish Shell:验证字符串是否包含子字符串

Nie*_*ksa 1 string shell fish

目前我正在尝试验证包含路径的字符串是否包含特定字符串。

我已经尝试过 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

Run Code Online (Sandbox Code Playgroud)

我的函数的目标是导航到下一个(同级)目录。

/mnt/c/workingdirectory/repoA --> 当前目录 /mnt/c/workingdirectory/repoB --> 导航到 repoB

fah*_*aho 5

你要string match

if string match -q  -- $needle $path
Run Code Online (Sandbox Code Playgroud)