Bash script to add tracks from a directory

This is the place where you can find everything related to the dedicated server, control scripts and community tools.

Moderators: Pit Crew, TM-Patrol

Post Reply
oscarian
cyclist
cyclist
Posts: 43
Joined: 12 Jan 2008 23:51
Owned TM-games: TMU

Bash script to add tracks from a directory

Post by oscarian » 24 Dec 2009 22:40

Hi guys,

I have a fairly simple question for anyone who knows a bit about Bash scripting.

I have a list of 350 tracks I have uploaded to my Linux VPS. I'm trying to work out a nice simple way to add the track names to my tracklist.txt (Matchsettings) file.

I've tried reading through a few Bash scripting tutorials, but I haven't managed to get anything working as yet.

Basically all I need is for a script to print out the lines:

Code: Select all

<challenge>
     <file>Challenges/MyDir/TheChallenge.gbx</file>
</challenge>
Where "TheChallenge.gbx" is taken from the directory listing and inserted into the appropriate place.

Once I have the formatted tracklist, I can simply add the rest of the matchsettings at the top and finish the file off with whatever else is needed.

Anyone able to offer some insights?

-Oscarian

nouseforname
swift roadking
swift roadking
Posts: 840
Joined: 20 Apr 2008 08:58
Owned TM-games: TMUF,TMNF, TMN
Manialink(s): therookies
Location: Germany
Contact:

Re: Bash script to add tracks from a directory

Post by nouseforname » 26 Dec 2009 14:51

how about the ID you need?

Code: Select all

	<challenge>
		<file>Campaigns\United\Stunts\StuntA1.Challenge.Gbx</file>
		<ident>7OwvyziKkRFvMawvFRxFKTShuZc</ident>
	</challenge>
you would have to read the gbx file and add that too.

why don't you use rcp to do that job? it's also very easy. you could even upload the tracks with it?
Owner of the Rookie servers and some more. :)
Rookies HP
My Project
Rookies Manialinkcolor>

Xymph
Pit Crew
Pit Crew
Posts: 5685
Joined: 19 Aug 2007 12:58
Owned TM-games: TMN, TMU, TMF, TM²
Contact:

Re: Bash script to add tracks from a directory

Post by Xymph » 26 Dec 2009 15:45

nouseforname wrote:how about the ID you need?
The <ident> fields are optional.
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub

nouseforname
swift roadking
swift roadking
Posts: 840
Joined: 20 Apr 2008 08:58
Owned TM-games: TMUF,TMNF, TMN
Manialink(s): therookies
Location: Germany
Contact:

Re: Bash script to add tracks from a directory

Post by nouseforname » 26 Dec 2009 16:07

Xymph wrote:
nouseforname wrote:how about the ID you need?
The <ident> fields are optional.
aso, didn't know that. maybe i thoughtit's a must cause i had to use it for some of my scripts :)
Owner of the Rookie servers and some more. :)
Rookies HP
My Project
Rookies Manialinkcolor>

nouseforname
swift roadking
swift roadking
Posts: 840
Joined: 20 Apr 2008 08:58
Owned TM-games: TMUF,TMNF, TMN
Manialink(s): therookies
Location: Germany
Contact:

Re: Bash script to add tracks from a directory

Post by nouseforname » 26 Dec 2009 17:08

oscarian wrote:Hi guys,

Anyone able to offer some insights?

-Oscarian

well i just created a little shell to output all trackfiles into a tracklist.txt. it will also remove any space in the filename. you'll have to take care any special characters anyway.

just add the tracklist header afterwards.

Code: Select all

#/bin/bash

TRACKDIR="tracks"
TRACKLIST="tracklist.txt"

DIR=`pwd`
cd $TRACKDIR
rename 's/\s+/_/g' *
cd $DIR

for i in $(ls $TRACKDIR); do
	echo -e "\t<challenge>" 
	echo -e "\t\t<file>$i</file>"
	echo -e "\t</challenge>" 
done >$TRACKLIST
output like that:

Code: Select all

	<challenge>
		<file>MC_bol_5.gbx</file>
	</challenge>
	<challenge>
		<file>MiNi+LoL+RR1[1].Challenge.Gbx</file>
	</challenge>
	<challenge>
		<file>MiNi+RR2[1].Challenge.Gbx</file>
	</challenge>
Owner of the Rookie servers and some more. :)
Rookies HP
My Project
Rookies Manialinkcolor>

nocturne
solid chaser
solid chaser
Posts: 1390
Joined: 08 Jun 2007 18:48
Owned TM-games: all
Contact:

Re: Bash script to add tracks from a directory

Post by nocturne » 26 Dec 2009 18:51

Beat me to the punch, hehe...

Code: Select all

#/bin/bash

## set to full path of which folder you wish to read
target_dir="/home/user/dir/GameData/Tracks/Challenges/"

cd $target_dir
find ./ -iname '*.Challenge.Gbx' |
while read F
do
	NF=${F#*./}
	DIR=${target_dir#*Tracks/}
		echo "	<challenge>"
		echo "		<file>${DIR}${NF}</file>"
		echo "	</challenge>"
done
Slightly different... Only reads trackfiles (not all as via ls), and works with any specific directory inside the Tracks directory, automatically including it's path in the outputted file.

I didn't have it output to a file by default... Figured might as well check it first, and use 'sh script.sh >file.txt'.

Ex.
<challenge>
<file>Challenges/$i$743Caned $678Unable.Challenge.Gbx</file>
</challenge>
<challenge>
<file>Challenges/TMX/TMNF-922607.challenge.gbx</file>
</challenge>
Last edited by nocturne on 26 Dec 2009 19:07, edited 1 time in total.

nouseforname
swift roadking
swift roadking
Posts: 840
Joined: 20 Apr 2008 08:58
Owned TM-games: TMUF,TMNF, TMN
Manialink(s): therookies
Location: Germany
Contact:

Re: Bash script to add tracks from a directory

Post by nouseforname » 26 Dec 2009 19:02

lol, i not a linux pro^^ i was happy to get it anyway. :)
Owner of the Rookie servers and some more. :)
Rookies HP
My Project
Rookies Manialinkcolor>

nocturne
solid chaser
solid chaser
Posts: 1390
Joined: 08 Jun 2007 18:48
Owned TM-games: all
Contact:

Re: Bash script to add tracks from a directory

Post by nocturne » 26 Dec 2009 19:16

Neither am I, hehe :1

Was part of a bash script I made a while back which would check for new tracks jukeboxed from tmx on one server, and add them to the tracklist of another server... Proved to be a highly over-complicated and under-elegant solution to a simple problem.

nouseforname
swift roadking
swift roadking
Posts: 840
Joined: 20 Apr 2008 08:58
Owned TM-games: TMUF,TMNF, TMN
Manialink(s): therookies
Location: Germany
Contact:

Re: Bash script to add tracks from a directory

Post by nouseforname » 26 Dec 2009 19:22

both scripts doing the job. i just wanted to delete the empty space in file names too. :)
Owner of the Rookie servers and some more. :)
Rookies HP
My Project
Rookies Manialinkcolor>

oscarian
cyclist
cyclist
Posts: 43
Joined: 12 Jan 2008 23:51
Owned TM-games: TMU

Re: Bash script to add tracks from a directory

Post by oscarian » 26 Dec 2009 20:46

aah, awesome. thanks so much for this guys! :thumbsup:

Post Reply