You are not logged in.

#1 2009-04-18 02:55:47

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Bash script doesn't work (Also, help me condense it)...

I am trying to make myself a bash script which combines files together based off of a config file. It is automating combining the audio book tracks I ripped of my CDs into chapters for easier reference. My first problem is that even though I used ', cat still thinks that everything is a separate file. My second problem is that it is way too long, any way I can have it automatically go up 1 chapter until a specified number (ie, until the value of the CHAPTERS variable)? Thanks to those who help. smile

chapcomb.sh

#!/bin/bash
# **************************************
# *           ChapterCombine           *
# *         By smartboyathome          *
# **************************************
# * A script which was made to combine *
# *  chapters from ripped audio books  *
# *   together, but can be edited to   *
# *    combine just about anything.    *
# * The config file is located in your *
# *   home directory, under the name   *
# * '.chapcomb.config'. If you do not  *
# *     have this file, create it.     *
# *   Otherwise this script will not   *
# *  run, as it won't have the proper  *
# *             variables.             *
# *  This can be changed by changing   *
# *     the CONFIG variable below.     *
# **************************************
# *           Licensed under:          *
# *      SmartLicense version 1.0      *
# **************************************

VERSION=0.1
usage() {
  echo "Chapter Combine v$VERSION"
  echo "A configuration file must be made in order to use this script. Configuration files are located at $CONFIG. This can be changed by adding CONFIG='blah' before this command, or by changing the script directly. See the sample file for how it should look."
}

while [ "$#" -ne "0" ]; do
  case $# in
    --help)
      usage
      exit 0
    ;;
    -h)
      usage
      exit 0
    ;;
  esac
done

# The config file stuff.
if [ -z "$CONFIG" ]; then
CONFIG="$HOME/.chapcomb.config"
fi
. $CONFIG
cd "$BOOKDIR"

combine() {
  # Checks if file exists, and if so, deletes it.
  if [ -a $NAME ]; then
    rm $NAME
  fi

  # Combines files
  cat $FILES >> $NAME

  # Checks to make sure that the chapter combined ok.
  if [ -s "$NAME" ]; then
    echo "File $NAME is ok."
  else
    echo "File $NAME had an error and didn't combine. Please fix config file and rerun this script."
    exit 0
  fi
}

chapter01
combine
chapter02
combine
chapter03
combine
chapter04
combine
chapter05
combine
chapter06
combine
chapter07
combine
chapter08
combine
chapter09
combine
chapter10
combine
chapter11
combine
chapter12
combine
chapter13
combine
chapter14
combine
chapter15
combine
chapter16
combine
chapter17
combine
chapter18
combine
chapter19
combine
chapter20
combine
chapter21
combine
chapter22
combine
chapter23
combine
chapter24
combine
chapter25
combine
chapter26
combine
chapter27
combine
chapter28
combine
chapter29
combine

.chapcomb.config

# Config file for ChapterCombine
# DO NOT DELETE, OR CHAPTERCOMBINE WILL NOT FUNCTION!

# Book's Directory
BOOKDIR='/media/Home/aabbott/Star Wars Fate of the Jedi Outcast'

# Each chapter's settings.
chapter01() {
#    FILES='Outcast\ Disc\ 1/01\ Track\ 1.mp3 Outcast\ Disc\ 1/02\ Track\ 2.mp3 Outcast\ Disc\ 1/03\ Track\ 3.mp3 Outcast\ Disc\ 1/04\ Track\ 4.mp3'
    FILES="'Outcast Disc 1'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4'}.mp3"
    NAME='Chapter01.mp3'
}

chapter02() {
#    FILES='Outcast\ Disc\ 1/05\ Track\ 5.mp3 Outcast\ Disc\ 1/06\ Track\ 6.mp3 Outcast\ Disc\ 1/07\ Track\ 7.mp3 Outcast\ Disc\ 1/08\ Track\ 8.mp3 Outcast\ Disc\ 1/09\ Track\ 9.mp3 Outcast\ Disc\ 1/10\ Track\ 10.mp3 Outcast\ Disc\ 1/11\ Track\ 11.mp3'
    FILES="'Outcast Disc 1'/{'05 Track 5','06 Track 6','07 Track 7','08 Track 8','09 Track 9','10 Track 10','11 Track 11'}.mp3"
    NAME='Chapter02.mp3'
}

chapter03() {
#    FILES='Outcast\ Disc\ 1/12\ Track\ 12.mp3 Outcast\ Disc\ 1/13\ Track\ 13.mp3 Outcast\ Disc\ 1/14\ Track\ 14.mp3 Outcast\ Disc\ 1/15\ Track\ 15.mp3 Outcast\ Disc\ 1/16\ Track\ 16.mp3'
    FILES="'Outcast Disc 1'/{'12 Track 12','13 Track 13','14 Track 14','15 Track 15','16 Track 16'}.mp3"
    NAME='Chapter03.mp3'
}

chapter04() {
#    FILES='Outcast\ Disc\ 2/01\ Track\ 1.mp3 Outcast\ Disc\ 2/02\ Track\ 2.mp3 Outcast\ Disc\ 2/03\ Track\ 3.mp3 Outcast\ Disc\ 2/04\ Track\ 4.mp3'
    FILES="'Outcast Disc 2'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4'}.mp3"
    NAME='Chapter04.mp3'
}

chapter05() {
    FILES="'Outcast Disc 2'/{'05 Track 5','06 Track 6','07 Track 7','08 Track 8'}.mp3"
    NAME='Chapter05.mp3'
}

chapter06() {
    FILES="'Outcast Disc 2'/{'09 Track 9','10 Track 10','11 Track 11','12 Track 12','13 Track 13'}.mp3"
    NAME='Chapter06.mp3'
}

# This one needs some special parameters as the chapter is split between two Discs.
chapter07() {
    FILES="{'Outcast Disc 2'/{'14 Track 4','15 Track 15','16 Track 16','17 Track 17'}.mp3,'Outcast Disc 3'/{'01 Track 1','02 Track 2','03 Track 3'}}.mp3"
    NAME='Chapter07.mp3'
}

chapter08() {
    FILES="'Outcast Disc 3'/{'04 Track 4','05 Track 5','06 Track 6','07 Track 7','08 Track 8'}.mp3"
    NAME='Chapter08.mp3'
}

chapter09() {
    FILES="'Outcast Disc 3'/{'09 Track 9','10 Track 10','11 Track 11'}.mp3"
    NAME='Chapter09.mp3'
}

chapter10() {
    FILES="'Outcast Disc 3'/{'12 Track 12','13 Track 13','14 Track 14','15 Track 15'}.mp3"
    NAME='Chapter10.mp3'
}

chapter11() {
    FILES="'Outcast Disc 4'/{'01 Track 1','02 Track 2','03 Track 3'}.mp3"
    NAME='Chapter11.mp3'
}

chapter12() {
    FILES="'Outcast Disc 4'/{'04 Track 4','05 Track 5','06 Track 6','07 Track 7','08 Track 8'}.mp3"
    NAME='Chapter12.mp3'
}

chapter13() {
    FILES="'Outcast Disc 4'/{'09 Track 9','10 Track 10','11 Track 11'}.mp3"
    NAME='Chapter13.mp3'
}

chapter14() {
    FILES="'Outcast Disc 4'/{'12 Track 12','13 Track 13','14 Track 14'}.mp3"
    NAME='Chapter14.mp3'
}

chapter15() {
    FILES="{'Outcast Disc 4'/'15 Track 15','Outcast Disc 5'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4','05 Track 5'}}.mp3"
    NAME='Chapter15.mp3'
}

chapter16() {
    FILES="'Outcast Disc 5'/{'06 Track 6','07 Track 7','08 Track 8','09 Track 9'}.mp3"
    NAME='Chapter16.mp3'
}

chapter17() {
    FILES="'Outcast Disc 5'/{'10 Track 10','11 Track 11','12 Track 12','13 Track 13'}.mp3"
    NAME='Chapter17.mp3'
}

chapter18() {
    FILES="{'Outcast Disc 5'/'15 Track 15','Outcast Disc 6'/{'01 Track 1','02 Track 2'}}.mp3"
    NAME='Chapter18.mp3'
}

chapter19() {
    FILES="'Outcast Disc 6'/{'03 Track 3','04 Track 4','05 Track 5','06 Track 6','07 Track 7','08 Track 8'}.mp3"
    NAME='Chapter19.mp3'
}

chapter20() {
    FILES="'Outcast Disc 6'/{'09 Track 9','10 Track 10','11 Track 11','12 Track 12','13 Track 13','14 Track 14'}.mp3"
    NAME='Chapter20.mp3'
}

chapter21() {
    FILES="'Outcast Disc 6'/{'15 Track 15','16 Track 16','17 Track 17','18 Track 18','19 Track 19'}.mp3"
    NAME='Chapter21.mp3'
}

chapter22() {
    FILES="'Outcast Disc 7'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4'}.mp3"
    NAME='Chapter22.mp3'
}

chapter23() {
    FILES="'Outcast Disc 7'/{'05 Track 5','06 Track 6','07 Track 7','08 Track 8'}.mp3"
    NAME='Chapter23.mp3'
}

chapter24() {
    FILES="'Outcast Disc 7'/{'09 Track 9','10 Track 10','11 Track 11','12 Track 12','13 Track 13'}.mp3"
    NAME='Chapter24.mp3'
}

chapter25() {
    FILES="{'Outcast Disc 7'/{'14 Track 14','15 Track 15','16 Track 16','17 Track 17'},'Outcast Disk 8'/'01 Track 1'}.mp3"
    NAME='Chapter25.mp3'
}

chapter26() {
    FILES="'Outcast Disc 8'/{'02 Track 2','03 Track 3','04 Track 4','05 Track 5'}.mp3"
    NAME='Chapter26.mp3'
}

chapter27() {
    FILES="'Outcast Disc 8'/{'06 Track 6','07 Track 7','08 Track 8','09 Track 9'}.mp3"
    NAME='Chapter27.mp3'
}

chapter28() {
    FILES="'Outcast Disc 8'/{'10 Track 10','11 Track 11','12 Track 12','13 Track 13'}.mp3"
    NAME='Chapter28.mp3'
}

chapter29() {
    FILES="'Outcast Disc 8'/{'14 Track 14','15 Track 15','16 Track 16'}.mp3"
    NAME='Chapter29.mp3'
}

Offline

#2 2009-04-18 11:41:35

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash script doesn't work (Also, help me condense it)...

I can't get your first problem to work at all.

$ FILES=test/{a,b,c}
$ cat $FILES
cat: test/{a,b,c}: No such file or directory
$ cat test/{a,b,c}
1
2
3

And no amount of quotes fixes it. So you have to do that directly. cat 'Outcast Disc 4'/{'01 Track 1',02*}.mp3 will work. And that also allow some more shortcuts, like cat *4/{01*,02*}

First though, are you sure you can cat multiple mp3s in a new one? Can your player play what comes out?

Offline

#3 2009-04-18 14:52:16

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: Bash script doesn't work (Also, help me condense it)...

Procyon wrote:

I can't get your first problem to work at all.

$ FILES=test/{a,b,c}
$ cat $FILES
cat: test/{a,b,c}: No such file or directory
$ cat test/{a,b,c}
1
2
3

And no amount of quotes fixes it. So you have to do that directly. cat 'Outcast Disc 4'/{'01 Track 1',02*}.mp3 will work. And that also allow some more shortcuts, like cat *4/{01*,02*}

First though, are you sure you can cat multiple mp3s in a new one? Can your player play what comes out?

I am sure I can do it. I have already tested it with one chapter manually. The MP3s don't have any tags, so that is why it works.

Second, I just did it all manually. Kind of a pain, but I guess I am forced to. :-\

Offline

#4 2009-04-18 15:08:50

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: Bash script doesn't work (Also, help me condense it)...

for FILES=test/{a,b,c} you may want to do something like

FILES=$(echo test/{a,b,c})

specifically $() aka `` so the expression is evaluated
and you can do arithmetic with

i=1
i=$(($i + 1))

for or while commands should prove useful as well

Last edited by kumyco (2009-04-18 15:09:16)

Offline

#5 2009-04-18 15:30:02

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash script doesn't work (Also, help me condense it)...

kumyco wrote:

for FILES=test/{a,b,c} you may want to do something like

FILES=$(echo test/{a,b,c})

That's a good one. And eval would work with simple examples, but once you have spaces and such in filenames it won't work I think.

If you are going to do this often, then there are too many things that can go wrong with a script that tries to automatically generate the filenames. Even 01* can go wrong.

So if you do have the filenames all in the .conf, then you can replace FILES= with cat and NAME= with >

The actual script doesn't add that much, except checking if it exists (just use > instead of >>) and if it isn't empty (not really necessary) (why did you put a license on that? is that even legal?)

So just use the .conf file as the main script.

Turn this:

BOOKDIR='/media/Home/aabbott/Star Wars Fate of the Jedi Outcast'
chapter01() {
    FILES="'Outcast Disc 1'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4'}.mp3"
    NAME='Chapter01.mp3'
}
chapter02() {
    FILES="'Outcast Disc 1'/{'05 Track 5','06 Track 6','07 Track 7','08 Track 8','09 Track 9','10 Track 10','11 Track 11'}.mp3"
    NAME='Chapter02.mp3'
}

into this:

cd '/media/Home/aabbott/Star Wars Fate of the Jedi Outcast'
cat 'Outcast Disc 1'/{'01 Track 1','02 Track 2','03 Track 3','04 Track 4'}.mp3 > 'Chapter01.mp3'
cat 'Outcast Disc 1'/{'05 Track 5','06 Track 6','07 Track 7','08 Track 8','09 Track 9','10 Track 10','11 Track 11'}.mp3 > 'Chapter02.mp3'

Right?

Why would you build anything around that?

If you are sure the filenames are all the same, you can use *

cat *\ 1/*\ {1..4}.mp3 > Chapter01.mp3
cat *\ 100/*\ {70..99}.mp3 > Chapter99.mp3

Offline

#6 2009-04-18 17:03:08

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: Bash script doesn't work (Also, help me condense it)...

Thanks, and my license is legal. I didn't include it here, but I included it with the actual file. All it is is a modified BSD license which says that the source must be given freely if this code specifically is used with any binary (that shouldn't be illegal).

And thanks to everyone, the script now works. I just wanted to create a conf so that I didn't have to go into the script itself and edit it, but looks like I can't. Oh well, I used this and it all works. smile

Offline

Board footer

Powered by FluxBB