LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2007, 10:55 PM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
how to burn/write files in a cd-r/rw using bash scripting


hello there again...i have created a script that will open a hdd then close...i want to add a script that would burn my, let's say, mp3 files from the /home/username directory to the media(cd-r/cd-rw)...sad ly, i don't know how to manipulate these mp3 files, transfer them to the burner, then burn the files...

here's my script as for now:

#!/bin/bash
clear

eject /dev/hdd
sleep 5
eject -t /dev/hdd


if [ "$?" -eq "0" ]; then
nautilus /media/cdrecorder1
sleep 5
eject /dev/hdd
sleep 5
eject -t /dev/hdd
else
exit 1
fi

when nautilus closes, it will pause for 5 secs then eject the medium...

help pls

Last edited by jaepi; 05-02-2007 at 10:57 PM.
 
Old 05-02-2007, 11:27 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
The 'eject -T' command I told you about earlier will act as a toggle. Not sure what all the sleeping is about. You could have an interactive prompt, like 'press enter when ready' instead. And, if the script is to burn CDs, you have everything around that, but no *that*. Read the man page for cdrecord - it's pretty simple (if your task is simple) but I try to avoid optical media as much as possible. You might check out bashburn rather than reinvent the wheel, unless this is just a learning exercise.
 
Old 05-02-2007, 11:46 PM   #3
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Original Poster
Rep: Reputation: 30
ohh..uhmmm...sleeping is the time given for the user to place his media on the drive...let's say, for 5 seconds..hehe...im currently viewing the man page for cdrecorder...lol...and again, too complicated for a noob like me, but im trying...this script is a learning exercise at the same time, for lazy person like me...a ready made script for burning audio and data files will be very useful

Last edited by jaepi; 05-02-2007 at 11:48 PM.
 
Old 05-03-2007, 12:13 AM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
right - maybe something like 'read -t30 -n1 -ep "Hit any key when ready..."' - five seconds doesn't seem like much time - this way the script will let you go as fast or slow as you want up to a point, but still not hang the script forever. OTOH, I'm fond of completely non-interactive scripts, so I can see the pros of avoiding a prompt, too. The bashburn script is findable at either freshmeat or sourceforge (or both). Search this site for threads on cdrecord - there are several examples - basically 'cdrecord OPTIONS DEVICE FILE', IIRC.
 
Old 05-06-2007, 01:42 AM   #5
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
Here is a modified version of your script. Feel free to change/delete/add whatever you want.

Its a good way to do what you wanted, and a good thing to play around with.

I've tested everything but the actual cd-burning command.

It may look like a lot, but read through the comments and it may seem very simple.

To test it I commented out the sleep, eject, and just echoed the cdrecord commands

Code:
#!/bin/bash

# Check to see if you supplied an arg, if not
# display usage, then exit with an error
if [ -z "$1" ]
	then
		# Everything between the EOF's will be displayed on the screen
cat <<EOF
Usage: `basename $0` [path_to_mp3's]
EOF
		exit 1
fi

# Sets the directory to a user input
DIRECTORY="$1"

# This should be set incase it changes, you only have to change it here
CDROM="/dev/hdd"

# This gets all the current mp3's in the folder...
# We need this to know which wav's we are deleting.
# Incase there are wav's that were there for some other reason, it
# won't delete them.
ls *.[Mm][Pp]3 | sed 's|.[Mm][Pp]3|.wav|' > CLEANUPFILE

clear

# Open the CDROM, wait 5 seconds and close it
eject $CDROM
sleep 5
eject -t $CDROM

# IF the cdrom sucessfully closes, convert all mp3's to wav's
if [ "$?" -eq "0" ]
	then
		echo "Converting mp3's to wav's..."

		# Convert them into wav's
		cd $DIRECTORY
		TOTAL=`ls *.[Mm][Pp]3 | wc -l`

		# A very bad and crude way of checking if the songs will fit
		# onto the cdrom.  You should check each mp3's length, and
		# compare it to 80 minutes
		# This just takes the amount of mp3's and checks it against 20
		if [ "$TOTAL" -gt 20 ]
			then
				echo "there are 20 or more mp3's, they will not fit"
				exit 1
		fi

		# The main loop for converting

		# Set which track we are converting
		CURRENT=1
		# Run the main loop on all mp3's
		for x in *.[Mm][Pp]3
		do
			# Change the file name from .mp3 to .wav so "sox" can save it as a .wav
			FILECHANGE=`echo "$x" | sed 's|.[Mm][Pp]3|.wav|'`
			# Check if the file already exists, This may be bad if the script errors
			# and you get a half complete wav file.
			if [ -e "$FILECHANGE" ]
				then	
			echo "Skipping file $CURRENT of $TOTAL, .wav file exists"
				else
			echo "Converting file $CURRENT of $TOTAL"
			# The actual converting program.  Sox should be on all major distros
			sox "$x" "$FILECHANGE"
			fi
			# The counter
			CURRENT=$(($CURRENT+1))
		done

		echo "Conversion Complete, now trying to burn..."

		# Burn the wav's onto the cd
		# You may want to add other options like speed, tao or dao, etc.
		cdrecord dev $CDROM "$DIRECTORY/*.wav"
		# If cdrecord completes sucessfully, then eject the drive
		if [ $? -eq 0 ]
			then
				echo "Burning successful.  Ejecting drive..."
		fi

		# Sleep, and cycle the cdrom
		sleep 5
		eject /dev/hdd
		sleep 5
		eject -t /dev/hdd
	else
		echo "Something went wrong with closing the CDROM"
		exit 1
fi

# remove only the .wav's that we created.
cat CLEANUPFILE | sed "s|'|'\\\''|" | sed "s|^|rm '|" | sed "s|$|'|" | bash

# Removes the CLEANUPFILE, I use a dump to perserve formatting.
rm CLEANUPFILE

exit 0

Last edited by jaykup; 05-06-2007 at 01:44 AM.
 
Old 05-06-2007, 08:26 PM   #6
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by jaykup
Here is a modified version of your script. Feel free to change/delete/add whatever you want.

Its a good way to do what you wanted, and a good thing to play around with.

I've tested everything but the actual cd-burning command.

It may look like a lot, but read through the comments and it may seem very simple.

To test it I commented out the sleep, eject, and just echoed the cdrecord commands

Code:
#!/bin/bash

# Check to see if you supplied an arg, if not
# display usage, then exit with an error
if [ -z "$1" ]
	then
		# Everything between the EOF's will be displayed on the screen
cat <<EOF
Usage: `basename $0` [path_to_mp3's]
EOF
		exit 1
fi

# Sets the directory to a user input
DIRECTORY="$1"

# This should be set incase it changes, you only have to change it here
CDROM="/dev/hdd"

# This gets all the current mp3's in the folder...
# We need this to know which wav's we are deleting.
# Incase there are wav's that were there for some other reason, it
# won't delete them.
ls *.[Mm][Pp]3 | sed 's|.[Mm][Pp]3|.wav|' > CLEANUPFILE

clear

# Open the CDROM, wait 5 seconds and close it
eject $CDROM
sleep 5
eject -t $CDROM

# IF the cdrom sucessfully closes, convert all mp3's to wav's
if [ "$?" -eq "0" ]
	then
		echo "Converting mp3's to wav's..."

		# Convert them into wav's
		cd $DIRECTORY
		TOTAL=`ls *.[Mm][Pp]3 | wc -l`

		# A very bad and crude way of checking if the songs will fit
		# onto the cdrom.  You should check each mp3's length, and
		# compare it to 80 minutes
		# This just takes the amount of mp3's and checks it against 20
		if [ "$TOTAL" -gt 20 ]
			then
				echo "there are 20 or more mp3's, they will not fit"
				exit 1
		fi

		# The main loop for converting

		# Set which track we are converting
		CURRENT=1
		# Run the main loop on all mp3's
		for x in *.[Mm][Pp]3
		do
			# Change the file name from .mp3 to .wav so "sox" can save it as a .wav
			FILECHANGE=`echo "$x" | sed 's|.[Mm][Pp]3|.wav|'`
			# Check if the file already exists, This may be bad if the script errors
			# and you get a half complete wav file.
			if [ -e "$FILECHANGE" ]
				then	
			echo "Skipping file $CURRENT of $TOTAL, .wav file exists"
				else
			echo "Converting file $CURRENT of $TOTAL"
			# The actual converting program.  Sox should be on all major distros
			sox "$x" "$FILECHANGE"
			fi
			# The counter
			CURRENT=$(($CURRENT+1))
		done

		echo "Conversion Complete, now trying to burn..."

		# Burn the wav's onto the cd
		# You may want to add other options like speed, tao or dao, etc.
		cdrecord dev $CDROM "$DIRECTORY/*.wav"
		# If cdrecord completes sucessfully, then eject the drive
		if [ $? -eq 0 ]
			then
				echo "Burning successful.  Ejecting drive..."
		fi

		# Sleep, and cycle the cdrom
		sleep 5
		eject /dev/hdd
		sleep 5
		eject -t /dev/hdd
	else
		echo "Something went wrong with closing the CDROM"
		exit 1
fi

# remove only the .wav's that we created.
cat CLEANUPFILE | sed "s|'|'\\\''|" | sed "s|^|rm '|" | sed "s|$|'|" | bash

# Removes the CLEANUPFILE, I use a dump to perserve formatting.
rm CLEANUPFILE

exit 0
hey, thanks dude...haha...im just a noob, some of the commands here are new to me...oh well..i'll just figure it out...thanks a lot...
 
Old 05-06-2007, 08:49 PM   #7
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
Go ahead and read through the comments. And feel free to ask about any command you don't understand.
 
Old 05-06-2007, 08:50 PM   #8
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 1,960

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
How about this: BashBurn
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 scripting problem: Can't get a list of all files, including hidden ones oxi Programming 24 03-12-2007 07:19 AM
Bash Scripting: Editing external files trek413 Linux - Software 1 11-02-2006 05:11 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 08:30 AM
bash scripting - editing files brian0918 Linux - Newbie 2 07-01-2003 03:27 PM
bash scripting - editing files brian0918 Programming 1 06-30-2003 07:16 PM


All times are GMT -5. The time now is 02:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration