LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2006, 03:07 AM   #1
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Rep: Reputation: 16
Unhappy bash, sorting mp3's, problem in "for" ... may be


Hello, please excuse me for my awful english .
I try to wrote a script on bash for sorting my *.mp3 files (they are thousand's in one directory), sorting is based ot their mp3 tags. Тhere it is:
Code:
#!/bin/bash
mp3info -p '%a\n' *.mp3 | sort | uniq | sed 's/\ /_/g' | tr -d \' | tr -d \/ > dirlst.eni
mp3info -p '%f:%a\n'  *.mp3 | sed 's/\ /_/g' |  tr -d \' | tr -d \/ > trdirlst.eni
cat dirlst.eni | xargs mkdir
for dir  in $(awk '{print $1}' < dirlst.eni )
do
cat trdirlst.eni | grep $dir > drls.eni
cat drls.eni | cut -d : -f 1 > trls.eni
  for tr in $(awk '{print $1}' < trls.eni )
        do
                mv $tr -v --target-directory=$dir
        done
done
And for better explanation(in case if you don't have mp3info to see output generated from it) I show you an exit of :

mp3info -p '%f:%a\n' *.mp3 :

in_mood_for_love.mp3:Ernesto Cortazar
Into_Violet_Excerpt.mp3:Bindlestiff
and so on...

And this is an exit of :

mp3info some_track.mp3 :

File: Zensual.mp3
Title: Zensual Track: 6
Artist: Karunesh
Album: Call of the Mystic Year: 2004
Comment: Genre: New Age [10]


This script must move every mp3 to directory that matches on "Artist:" field from mp3info, but script simply moves all files in first directory created from mkdir. Тhats is the problem, maybe I have a logical mistake in two "for" loops but I can't find it.

Thanks in advance for all of your help.

Last edited by zomane; 10-06-2006 at 03:17 AM.
 
Old 10-06-2006, 05:10 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Hello and welcome to LQ, hope you like it here.

Hello, please excuse me for my awful english
It's rather good, actually.


Thats is the problem, maybe I have a logical mistake in two "for" loops but I can't find it.
Better use a "while" loop. Something like this:
Code:
#!/bin/sh --
# Set debug and error flag while testing. 
set -xe 
# Whoami? 
progn=${$0//*\//} 
# Unalias any move aliases.
alias mv 2>&1>/dev/null && unalias mv 2>/dev/null
# Root of directory where your MP3's reside. 
MP3_BASEDIR=/some/dir  
# Root of directory where they'll be moved to. 
MP3_MOVEDIR=/another/dir  
# Preflight checks. 
# Use while loop instead to work on each (IFS) line of output. 
[ ! -d "${MP3_BASEDIR}" -o ! -r "${MP3_BASEDIR}" ] && \ 
echo "${progn} [FATAL]: No \"${MP3_BASEDIR}\" or not readable." >/dev/stderr; exit 127
[ ! -d "${MP3_MOVEDIR}" -o ! -w "${MP3_MOVEDIR}" ] && \ 
echo "${progn} [FATAL]: No \"${MP3_MOVEDIR}\" or not writable." >/dev/stderr; exit 127
# Find mp3's by extension, disregarding case.
find ${MP3_BASEDIR} -type f -iname \*.mp3 | while read f; do
 # Find artist.
 ARTIST=$(mp3info -p '%a' "${f}")
 # Check if variable is not empty.
 if [ "X${ARTIST}" = "X" ]; then
  echo "${progn} [FATAL]: Artist tag empty in \"${f}\"." >/dev/stderr; break
 else
  # Quick check for disallowed chars in directory names.
  if [ "Y${ARTIST//[a-z0-9A-Z]/}" != "Y" ]; then
   echo "${progn} [FATAL]: Disallowed chars (\"${ARTIST//[a-z0-9A-Z]/}\") for \"${f}\"."\ 
    >/dev/stderr; break
  else  
   # If destination doesn't exist, make it.
   if [ ! -d "${MP3_MOVEDIR}/${ARTIST}" ]; then
    mkdir "${MP3_MOVEDIR}/${ARTIST}" \
    || echo "${progn} [FATAL]: couldn't make dir \"${MP3_MOVEDIR}/${ARTIST}\"."\
    >/dev/stderr; break
   fi   
   # Move file and alert.
   mv "${f}" "${MP3_MOVEDIR}/${ARTIST}"; case "$?" in
    0) [ "$1" = "-v" ] && echo "${progn} [INFO]: \"${f}\" -> \"${MP3_MOVEDIR}/${ARTIST}\";;
    *) echo "${progn} [FATAL]: couldn't move \"${f}\"." >/dev/stderr;;
   esac 
 fi
fi
done 
# Always exit properly
exit 0
Untested, but YMMV(VM) anyway ;-p

Last edited by unSpawn; 10-06-2006 at 05:12 AM.
 
Old 10-06-2006, 05:26 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Btw...

Using
Code:
[ "Y${ARTIST//[a-z0-9A-Z]/}" != "Y" ]
appears to be a good way to test for \t and \n using default IFS. Anyone care to break it? TIA
 
Old 10-06-2006, 07:08 AM   #4
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by unSpawn
Hello and welcome to LQ, hope you like it here.
Yes I feel good in LQ, here I find very usefull information many times.For this reason I made my first post more than a year after I registered here, as people said "in almost 100% of cases someone, somewhere already have the same problem and solved it, just search" .

Quote:
Originally Posted by unSpawn
It's rather good, actually.
Thanks.

Now i make some changes in your code, because in some "Artist" fields I have a "/ " and "'" and cannot pass this check:
Code:
  if [ "Y${ARTIST//[a-z0-9A-Z]/}" != "Y" ]; then
 echo "${progn} [FATAL]: Disallowed chars (\"${ARTIST//[a-z0-9A-Z]/}\") for \"${f}\"."\ 
 >/dev/stderr; break
. For this reason in my buggy script I do
Code:
tr -d \' | tr -d \/
. My first idea was to remove all whitespaces, "/" and "'" and then give values to variables and commands to avoid breaks provoked from disallowed chars, because I don't make any checks for them.But you help me very much, I'm still a totaly bash newbie and in yor code I find things that I don't knew it before .

Thanks again.

Last edited by zomane; 10-06-2006 at 08:11 AM.
 
  


Reply

Tags
bash, loop, sort



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
bash equivalence of tcsh "alias em "emacs \!:1 &""? rgiggs Slackware 3 07-29-2004 02:07 AM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration