I am trying to create a directory from a variable that comes from grep'ing and it keeps failing, telling 开发者_开发百科me there is a carriage return where there isn't. I have tried dos2unix, it tells me it is an invalid file. I created the script on ubuntu 10.10 with gedit and unix line endings.
I have also tried piping the output of the cat'd file to
tr -d '\r'
that doesn't do it.
here is the part of the script which its failing on:
ARTIST=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 1`
ALBUM=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 2 | awk '{gsub(/^[ \t]+|[\t]+$/,"")};1'`
mkdir $DEST/"$ARTIST"
cd $DEST/"$ARTIST"
mkdir "$ALBUM"
mv $DEST/temp/*.flac $DEST/"$ARTIST"/"$ALBUM"/
and the error i receive:
mv: target `Hard\r' is not a directory
here is the file "read" that it is pulling from:
macmini:~/Dropbox/bin$ cat ~/Desktop/temp/read
210 folk 0d021f02 CD database entry follows (until terminating `.')
# xmcd CD database file
#
# Track frame offsets:
# 150
# 13002
#
# Disc length: 545 seconds
#
# Revision: 0
# Processed by: cddbd v1.5.2PL0 Copyright (c) Steve Scherf et al.
# Submitted via: CDex 1.50Beta7
#
DISCID=0d021f02
DTITLE=Cursive / Art is Hard
DYEAR=2003
DGENRE=Indie
TTITLE0=Art is Hard
TTITLE1=Sinner's Serenade
EXTD= YEAR: 2003
EXTT0=
EXTT1=
PLAYORDER=
.
i am a little lost at this point. where is the carriage return coming from?
I think it's coming from the read file. Run dos2unix
on that file.
However, this error message highlights another problem: mv
is misinterpreting what you're giving it, because what it thinks is the target is too short. I'm guessing you aren't quoting file paths containing spaces correctly. Try surrounding the whole argument in double quotes, not merely components of it.
You can do this with one awk script
#!/bin/bash
awk -F"[/=]" 'BEGIN{
qq="\047"
DEST="/tmp"
}/DTITLE/{
ARTIST=$2
ALBUM=$NF
gsub(/^[ \t]+|[ \t]+$/,"",ALBUM)
gsub(/^[ \t]+|[ \t]+$/,"",ARTIST)
cmd="mkdir "qq DEST"/"ARTIST qq
print cmd
#system(cmd)
cmd="mkdir -p " qq DEST"/"ARTIST "/" ALBUM qq
print cmd
#system(cmd)
cmd="mv "DEST"/temp/*.flac " qq DEST"/"ARTIST "/" ALBUM qq
print cmd
#system(cmd)
} ' file
To run it, save as a shell script (eg myscript.sh) and type ./myscript.sh
on the command line
Or you can use a programming language that takes care of cumbersome shell quoting issues eg Ruby(1.9+) script
#!/usr/bin/env ruby
require 'fileutils'
DEST="/tmp"
File.open("file") do |f|
while not f.eof?
line = f.gets.chomp
if line[/DTITLE/]
line = line.split(/[\/=]/)
ARTIST=line[1].strip
ALBUM=line[2].strip
Dir.mkdir( File.join(DEST, ARTIST) )
Dir.mkdir( File.join(DEST, ARTIST, ALBUM) )
Dir.glob( File.join(DEST, "temp", "*.flac") ).each do |file|
FileUtils.mv ( file, File.join(DEST, ARTIST, ALBUM) )
end
end
end
end
To run it, save as (Eg myscript.rb ) and type ruby myscript.rb
on the command line or inside a shell script.
精彩评论