Google Answers Logo
View Question
 
Q: linux shell scripting ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: linux shell scripting
Category: Computers > Programming
Asked by: bokke-ga
List Price: $5.00
Posted: 13 Oct 2003 09:08 PDT
Expires: 12 Nov 2003 08:08 PST
Question ID: 265728
I have directory trees that contains music files that are structured
as follows :

       directory
           Artist1
               Album1
                  01_title of track 1.mp3
                  02_title of track 2.mp3
                  .......
               Album2
               .....
           Artist2
           Artist3
           Artist4
           ......
how can I write a shell script that takes a single directory as its
parameter that produces a formatted index for a music directory as its
output.
The index should be sorted by artist and then by album, the tracks
remaining in the order in which they appear on the album.
 
Regards.

Request for Question Clarification by maniac-ga on 13 Oct 2003 10:04 PDT
Hello Bokke,

What kind of formatting are you interested in? For example:

  ls -1R directory
would produce output like
  directory/Artist1/Album1:
  (list of files here)
  directory/Artist1/Album2:
  (list of files here)
and so on

or
  find directory -name "*" -print
would produce output like
  directory/Artist1/Album1/01_title of track1.mp3
  directory/Artist1/Album1/O2_title of track2.mp3
and so on for

In either case, the result should be sorted. If neither of these are
OK, please indicate the format you are looking for.

Also - since you asked for a shell script, which shell should it be
for? The default for many users is bash, but if you want tcsh instead
- please indicate that.
  --Maniac

Clarification of Question by bokke-ga on 14 Oct 2003 06:10 PDT
Thank you for your quuick response maniac... I would like the format
below.
Obviously I can make the directories myself but was hoping for help on
the actuall script. The shell which is being used is "bourne shell".

find directory -name "*" -print 
would produce output like 
  directory/Artist1/Album1/01_title of track1.mp3 
  directory/Artist1/Album1/O2_title of track2.mp3 

What I would like is to enter a parameter which is also a
directory(i.e music) and list the directory in the format above with
all it's music.
Answer  
Subject: Re: linux shell scripting
Answered By: maniac-ga on 15 Oct 2003 04:39 PDT
Rated:4 out of 5 stars
 
Hello Bokke,

I assume you have some familiarity with the command line interface and
can create text files with an editor. If you need help with that or
any other part of the answer - please use a clarification request.

The basic script you ask for is:

#!/bin/sh
find $1 -name "*" -print

Using a text editor (vi, emacs, ed), create a text file with those two
lines. If you named the file list.sh, then use:
  chmod +x list.sh
to make the script executable.

Assume the script is named list.sh, your music files are in the
"music" directory under your home directory, and started with a
command like
  list.sh ~/music
for the explanation that follows:
 - the first line indicates you want to use the shell program at
/bin/sh (bourne shell)
 - the second line indicates you want to run the find program. The
music directory (~/music) will replace $1 in the command line
generating
  find ~/music -name "*" -print
as the command to be executed. As I noted in the request for question
clarification, the format of output will be one line per music file,
sorted by artist and album. If the output is not sorted, change the
second line to read
find $1 -name "*" -print | sort
to sort the result.

If you want just one album printed, change the command line to
  list.sh ~/music/Artist1/Album1
or if you want just one artist printed, change the command line to
  list.sh ~/music/Artist2
as appropriate.

As shown, the output of the script will appear on the screen. If you
want to save this into a file, change the command line to
  list.sh ~/music > music.dir
to capture the output in music.dir.

For more information about scripts and other utlities I suggest
reviewing
  man bash
or
  man sh
[varies by type of Unix / Linux system]
  man find
on your system. Alternatively, you may get on line sites using phrases
such as:
  bourne shell script description
  bourne shell script tutorial
or check more general sites such as
  http://www.devdaily.com/unix/edu/index.shtml
which have more general tutorials and training materials.
  
  --Maniac
bokke-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy