简体   繁体   English

鱼壳:验证字符串是否包含子字符串

[英]Fish Shell: validate if string contains substring

Currently I'm trying to validate if my string which contains a path contains a specific string. 目前,我正在尝试验证包含路径的字符串是否包含特定字符串。

I've already tried the bash comparison: 我已经尝试过bash比较:

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

and

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

I also try the 'contains' keyword. 我也尝试使用“包含”关键字。 But i'm not sure if I'm suppose to use this with a string.. 但是我不确定是否应该将它与字符串一起使用。

Unfortunately these attempts all failed. 不幸的是,这些尝试都失败了。 :/ :/

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

The goal of my function is to navigate to the next (sibling) directory. 我函数的目标是导航到下一个(同级)目录。

/mnt/c/workingdirectory/repoA --> current directory /mnt/c/workingdirectory/repoB --> navigate to repoB / 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