我这样做:
var=`grep GENRE Billy_Cobham_-_Drum_\&_Voice_2.cue | cut -d ' ' -f 3`
mkdir $var
file.cue 感兴趣的行是:
REM GENRE Funky+Jazz
mkdir
创建目录时,显示的目录ls
为:
Funky+Jazz?
我进行了很多测试,以了解许多命令,例如在所有测试中读取终端的结果都是“Funky+Jazz”。但字符串有一个隐藏的“开始新行”。而且,当调用命令awk
grep
cut
sed
时,它会产生奇怪的结果。mkdir
如果我在 Funky+Jazz 目录中打开终端,则 shell 将显示如下内容:
$ @host:~/Desktop/test/5/Funky+Jazz
我不知道如何解决这个问题,并且有一个正确的目录名称而没有问号。这是我的提示
REM GENRE Funky+Jazz
REM DATE 2006
REM DISCID 920BFB0A
REM COMMENT "ExactAudioCopy v0.95b4"
PERFORMER "Billy Cobham"
TITLE "Drum & Voice 2"
FILE "Billy_Cobham_-_Drum_&_Voice_2.ape" WAVE
TRACK 01 AUDIO
TITLE "Waveform"
PERFORMER "Billy Cobham"
INDEX 01 00:00:00
TRACK 02 AUDIO
TITLE "Real Funk"
PERFORMER "Billy Cobham"
INDEX 00 05:04:19
INDEX 01 05:06:21
TRACK 03 AUDIO
TITLE "Running"
PERFORMER "Billy Cobham"
INDEX 00 09:42:52
INDEX 01 09:44:56
TRACK 04 AUDIO
TITLE "Final Destination"
PERFORMER "Billy Cobham"
INDEX 00 17:30:36
INDEX 01 17:32:38
TRACK 05 AUDIO
TITLE "One More Day To Live"
PERFORMER "Billy Cobham"
INDEX 00 24:26:37
INDEX 01 24:28:38
TRACK 06 AUDIO
TITLE "Ozone Part 1"
PERFORMER "Billy Cobham"
INDEX 00 29:41:47
INDEX 01 29:43:39
TRACK 07 AUDIO
TITLE "Ozone Part 2"
PERFORMER "Billy Cobham"
INDEX 00 31:56:06
INDEX 01 31:56:16
TRACK 08 AUDIO
TITLE "Take Seven"
PERFORMER "Billy Cobham"
INDEX 00 38:24:64
INDEX 01 38:26:68
TRACK 09 AUDIO
TITLE "Let Me Breathe"
PERFORMER "Billy Cobham"
INDEX 00 44:28:41
INDEX 01 44:30:41
TRACK 10 AUDIO
TITLE "Amazon"
PERFORMER "Billy Cobham"
INDEX 00 48:54:16
INDEX 01 48:56:20
答案1
解决方案如下:
#!/bin/bash
while read line; do
if [ "${line:0:10}" = "REM GENRE " ]; then
folder=$(echo "${line:10}" | tr -d '\n\r')
mkdir $folder
break
fi
done < file.cue
echo "Folder $folder has been created"
使用您作为此答案的注释附加的示例文件进行测试。
解释: 一开始有一个 while 循环,正如你从 done < file.cue 中看到的那样,它读取 file.cue 并逐行读取 file.cue 的行。现在的 if 语句是行
if [ "${line:0:10}" = "REM GENRE " ]; then
方法:如果变量“line”的前10个字母等于“REM GENRE”那么...
当 if 语句为真时,我们就处于我们感兴趣的行上。然而,我们关心的是这一行中 $line 第 10 个字母之后的字符。${line:10} 表示变量 'line' 前 10 个字母之后的所有内容。
因此,我“回显”前 10 个字符之后的所有内容,并在“tr”的帮助下删除所有换行符(实际上,这才是导致问题的原因)。我将其附加到变量“folder”并创建它。