ShellScript - IF处理文件名模式(ShellScript - IF handling filename pattern)

我在UNIX中有一个目录,它有数千个.TGZ压缩文件,它们遵循以下模式:

01.red.something.tgz 02.red.something.tgz 03.red.anything.tgz 04.red.something.tgz 01.blue.something.tgz 02.blue.everything.tgz 03.blue.something.tgz 04.blue.something.tgz 01.yellow.something.tgz 02.yellow.blablathing.tgz 03.yellow.something.tgz 04.yellow.something.tgz

他们正在使用文件系统中的大量数据,我需要列出它们而不提取文件本身。 实际上它需要一些时间,所以我相信这个shellcript符合需要。 我是Shellscript的新手,我正在学习所以我做了这个.sh

$pattern = "red" for file in *.tgz do if [[ ${file} == '...${pattern}.*.tgz' ]]; then echo" ==> ${file} match the pattern and the output dir is : out/" tar -tf $file > ./out/$file else echo "${file} Doesn't match the pattern" fi done

但是我在if部分做错了,即使模式匹配,我也得到了“ 与模式不匹配 ”的信息。

我知道这有点简单,但我无法理解为什么这个家伙不起作用。 如果你们能解释为什么这不起作用,我会感激不尽。

谢谢。

I have a directory in UNIX which has a thousands of .TGZ compressed files, they follow this pattern :

01.red.something.tgz 02.red.something.tgz 03.red.anything.tgz 04.red.something.tgz 01.blue.something.tgz 02.blue.everything.tgz 03.blue.something.tgz 04.blue.something.tgz 01.yellow.something.tgz 02.yellow.blablathing.tgz 03.yellow.something.tgz 04.yellow.something.tgz

They are using a large amount from the filesystem,and i need to list them without extract the file itself. Actually it'll take some time, so i believe this shellscript will fit the need. I'm kinda new to Shellscript, i'm learning so i made this .sh

$pattern = "red" for file in *.tgz do if [[ ${file} == '...${pattern}.*.tgz' ]]; then echo" ==> ${file} match the pattern and the output dir is : out/" tar -tf $file > ./out/$file else echo "${file} Doesn't match the pattern" fi done

But i've made something wrong in the if part,and even when the pattern is matched, i've got the 'Doesn't match the pattern' message.

I Know it's kinda simple if,but i can't understand why this fella doesn't work. I'd be thankfull if you guys can explain why this doesn't work.

Thank you.

最满意答案

你需要在bash中创建varibales时注意空格, if不应该有' - 单引号或" - 双引号,如果你想匹配regex , 使用: if [[ ${file} == ${regEx} ]];

测试:

$ ls *.tgz 01.red.something.tgz 01.yellow.something.tgz $ ./t.sh ==> 01.red.something.tgz match the pattern and the output dir is : out/ 01.yellow.something.tgz Doesn't match the pattern
$ cat t.sh #!/bin/bash pattern="red" regEx="*.${pattern}.*.tgz" for file in *.tgz do if [[ ${file} == ${regEx} ]]; then echo " ==> ${file} match the pattern and the output dir is : out/" #tar -tf $file > ./out/$file else echo "${file} Doesn't match the pattern" fi done

you need to watch out for spaces when you create varibales in bash, in if there should not be ' - single quotes or " - double quotes if you want to match on regex, use: if [[ ${file} == ${regEx} ]];

Test:

$ ls *.tgz 01.red.something.tgz 01.yellow.something.tgz $ ./t.sh ==> 01.red.something.tgz match the pattern and the output dir is : out/ 01.yellow.something.tgz Doesn't match the pattern
$ cat t.sh #!/bin/bash pattern="red" regEx="*.${pattern}.*.tgz" for file in *.tgz do if [[ ${file} == ${regEx} ]]; then echo " ==> ${file} match the pattern and the output dir is : out/" #tar -tf $file > ./out/$file else echo "${file} Doesn't match the pattern" fi done

更多推荐