在 bash 脚本中查找文件夹名称的一部分 [英] Find part of folder name in bash script

查看:18
本文介绍了在 bash 脚本中查找文件夹名称的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在文件夹结构中查找特定部分,但在 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:

  1. 我如何 grep 或使用 find 或其他更好的方法来获取特定的 currentFolder?
  2. 我可以在一个智能的 case 语句中完成所有这些吗?
  1. How do I grep or use find or something better to get the particular currentFolder?
  2. Could I do all that in a smart case statement?

推荐答案

您可以将 basenamedirnamecase 语句一起使用: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆