在 bash 脚本中查找文件夹名称的一部分 [英] Find part of folder name in bash script
问题描述
尝试在文件夹结构中查找特定部分,但在 bash 脚本中使用 RegEx 时遇到问题 - 进一步我不会说流利的正则表达式.我需要知道我传递给 bash 脚本的文件夹类型".根据当前所在的文件夹类型",它需要运行不同的操作.
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.
以下示例文件夹结构:
/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
我玩过的 RegExr,但找不到解决方案:http://regexr.com/39mk4一个>.我不知道如何在 bash 中做到这一点.
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.
我正在尝试在 media
中查找文件夹的名称.所以基本上我想创建这样的东西(非 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:
- 我如何
grep
或使用find
或其他更好的方法来获取特定的currentFolder
? - 我可以在一个智能的
case
语句中完成所有这些吗?
- How do I
grep
or usefind
or something better to get the particularcurrentFolder
? - Could I do all that in a smart
case
statement?
推荐答案
您可以将 basename
和 dirname
与 case
语句一起使用:p>
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屋!