Fish Shell:验证字符串是否包含子字符串
Fish Shell: validate if string contains substring
目前我正在尝试验证包含路径的字符串是否包含特定字符串。
我已经尝试过 bash 比较:
- if [ $path = $needle ]
和
- if [ $path =~ $needle ]
我也试试 'contains' 关键字。但我不确定我是否应该将它与字符串一起使用..
不幸的是,这些尝试都失败了。 :/
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
目前我正在尝试验证包含路径的字符串是否包含特定字符串。
我已经尝试过 bash 比较:
- if [ $path = $needle ]
和
- if [ $path =~ $needle ]
我也试试 'contains' 关键字。但我不确定我是否应该将它与字符串一起使用..
不幸的是,这些尝试都失败了。 :/
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