how to burn/write files in a cd-r/rw using bash scripting
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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
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.
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
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.