查找bash脚本文件夹名称的一部分 [英] Find part of folder name in bash script
问题描述
试图找到一个文件夹结构的特定部分,但我使用bash脚本中的正则表达式遇到了麻烦 - 此外我不会讲流利的正则表达式的。我需要知道我传递给bash脚本文件夹是什么类型。根据不同的文件夹类型它目前是它需要运行不同的操作。
下面的示例文件夹结构:
/home/usr/media/series/Great.Series.S01E02.Something
/home/usr/media/movies/Some.Movie.Here
/回家的/ usr /媒体/音乐/专辑这里
/回家的/ usr /媒体/音乐/另一张专辑
该RegExr我发挥各地,但无法找到一个解决方案: http://regexr.com/39mk4一>。我不知道如何在bash做到这一点。
我试图找到下面的文件夹中的的名称的传媒
。所以基本上我要创建这样的事情(非bash的伪code):
$ currentFolder ='/home/usr/media/series/Great.Series.S01E02.Something
//神奇,下面将需要系列|电影|音乐根据输入的路径
$ currentFolder ='系列'
开关($ currentFolder){
案系列:
//东西
案例电影:
//别的东西
案音乐:
//更多
}
一个问题的两个部分:
- 如何
的grep
或使用找到
或更好的东西来获取特定的currentFolder
? - 我所能做的一切,在一个聪明的
情况
语句?
您可以使用基本名
和目录名
与情况
语句:
currentFolder ='/家庭的/ usr /媒体/系列/ Great.Series.S01E02.Something
currentFolder = $($基名(目录名称$ currentFolder))
万一$ currentFolder
系列)
# 做一点事
;;
电影)
#做别的事情
;;
音乐)
#另一个
;;
*)
;;
ESAC
Trying to find a specific part in a folder structure, but I'm having trouble using RegEx within a bash script - furthermore I don't speak RegEx fluently. I need to know what folder 'type' I passed to the bash script. Depending on the folder 'type' it currently is in it would need to run different actions.
The following example folder structure:
/home/usr/media/series/Great.Series.S01E02.Something
/home/usr/media/movies/Some.Movie.Here
/home/usr/media/music/An Album Here
/home/usr/media/music/Another Album
The RegExr I played around with, but couldn't find a solution: http://regexr.com/39mk4. I wouldn't know how to do this in bash.
I'm trying to find the name of the folder below media
. So basically I want to create something like this (non-bash pseudocode):
$currentFolder = '/home/usr/media/series/Great.Series.S01E02.Something'
//magic, the following would need to be series|movies|music depending on the input path
$currentFolder = 'series'
switch ($currentFolder) {
case 'series':
//something
case 'movies':
//something else
case 'music':
//more
}
A two part question:
- How do I
grep
or usefind
or something better to get the particularcurrentFolder
? - Could I do all that in a smart
case
statement?
You can use basename
and dirname
with case
statements:
currentFolder='/home/usr/media/series/Great.Series.S01E02.Something'
currentFolder=$(basename $(dirname $currentFolder))
case $currentFolder in
series)
# Do something
;;
movies)
# Do something else
;;
music)
# another
;;
*)
;;
esac
这篇关于查找bash脚本文件夹名称的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!