Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-13-2012, 07:46 AM   #1
Registered: Jun 2007
Location: london
Distribution: ubuntu 12.04 LTS
Posts: 46

Rep: Reputation: 15
Using 2 Burners at the same time Script

Dear all. this will be my 1st script file. so please be gentle with me. I am looking to do a simple script to burn a "ISO" file that i have made (its a video for the charity i work in) to 2 or 3 DVD drivers.

I have had a look over some sites and found this script could someone tell me if this will work, The way i need it to work.


for i in {0..3};do
cdrecord -eject dev=/dev/sr$i $1 &

The line for i in {0..3};do means that use all drives 0,1,2,3 and give it a title of i. I take it that this is called a string as i have seen the $ simbol before. Then the ;do bit is telling the system to 'DO' the next line

The next line cdrecord -eject is the program i need to use to do the burning and a program command (if thats the right word to use). dev=/dev/sr$1 $1 & This bit i'm guessing is saying go to dev/sr directory and use the string $i given a file name to use.???

The last line i'm guessing means something like done or stop processing this script.
After i have done this script i would need to make this executable

chmod +x ~/bin/

Then to run the script i would need to type in the command line. /path/to/image.iso
or /home/keiffee/dvd/final/video1.iso

Could someone tell me if this will work under Ubuntu 12.04 32bit or if there is a better way to do this
Old 05-13-2012, 10:25 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
To start with, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

#!/bin/sh is used for interpreting scripts in restricted, posix-compliant lowest-common-denominator mode, and many shell-specific features may be unrecognized or have their behavior altered. This is mostly recommended for system startup scripts and other cases where portability and standardization are important. When this isn't required, you should use #!/bin/bash (or the path to another shell that has more modern, advanced features available, such as ksh or zsh).

I'm not sure if all the features in your script (specifically brace expansion) are supported by posix, so be sure to specify bash instead.

for i in {0..3};do
This is just a standard for loop, using brace expansion to generate the input word list. Each digit from 0 to 3 will be read into the variable "i" in turn, then the sub-commands run. Assuming there are 4 drives connected, with those numbers in their device names, they should all be processed.

cdrecord -eject dev=/dev/sr$i $1 &
For each loop iteration, run the supplied cdrecord command, with the current value of the variable "$i" inserted at that point. The first command parameter of the script (the path to the iso), "$1" is also inserted. Finally, the "&" at the end forks the command into the background, so that the loop can continue immediately, instead of waiting for it to finish first.

Note that this part of the script is not very safe. There's no test to ensure that the $1 input is a valid file first, and since the variables are unquoted, any spaces in the filename would be word-split before run, breaking the command.

It's vital in scripting to understand how the shell handles arguments and whitespace. Read these links:

The done keyword just completes the loop, of course.

Other than that, so as long as the cdrecord command works when run on its own, the script should work as well. Read the whole bash guide I linked to above for more on the basics of scripting.

BTW, there used to be a small program available in the debian repositories that would write an image to multiple drives at once, but I can't seem to find it now. It was also named something like multiburn.

Last edited by David the H.; 05-13-2012 at 10:26 AM.
Old 05-13-2012, 10:43 AM   #3
Registered: Aug 2002
Posts: 17,842

Rep: Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404Reputation: 2404
Have you tried googling the internet to learn about bash scripting and for loops?

In a nutshell a for loop lets it iterate over a series. In the script you posted, i=0, i=1, i=2 and i=3.
So all statements inbetween the do and done will be executed 4 times and /dev/sr$i will expand to /dev/sr0,dev/sr1,/dev/sr2 and /dev/sr3 i.e. the device ID for the CD/DVD drives.

Make sure you change the loop for the actual number of drives installed.
Yes, it will work under Ubuntu.

A bit late... or a simple summary...
Old 05-13-2012, 12:13 PM   #4
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
Here is a suggestion. This works exactly like cdrecord, except in parallel on all recorders you have:

trap 'kill -INT $(jobs -p) ; exit 1' INT

for DEV in /dev/sr[0-9]* ; do
    [ -b "$DEV" ] && cdrecord -eject dev=$DEV "$@" &

The shebang line specifies we use Bash for this. I guess the script should also work on POSIX shells, too, but I prefer to use Bash for this kind of utility scripts.

The trap ... INT sets a signal handler for the INT signal. If you press Ctrl+C, the INT signal gets sent to the script. Normally, it would interrupt (abort) the script, but that'd leave the cdrecord commands hanging. The $(jobs -p) part expands to all the process ID's of background jobs. Because the entire action is in single quotes, it gets evaluated only when/if the signal occurs. If it was in double quotes, it would be evaluated when the trap was set, and we don't want that. The purpose of this trap is very simple: If the user presses Ctrl+C, the trap will forward the INT signal to all background processes, and exit the script. In other words, you can now press Ctrl+C, and both the script and the cdrecord commands will be interrupted.

In the for loop, DEV will match each file or directory in /dev/ that begins with sr and a digit, in turn.

The [ -b "$DEV" ] && expression on the third line can be read as "if $DEV refers to a block device, then" . If $DEV is not a block device (like an optical writer), then the rest of the line is not executed.

The cdrecord command uses $DEV as the device, but includes all the parameters you supplied to this script at the command line; "$@" evaluates to the current positional parameters, which in this case are the parameters supplied to this script at the command line.

The & after the cdrecord command means it will be run in the background, parallel to this script (and any other background tasks).

After the loop, you can see there is a wait command. This is a shell job control built-in, and simply waits until all background jobs complete. Thus, the script will not exit, until all the background jobs are complete.

Last edited by Nominal Animal; 05-13-2012 at 12:14 PM.
Old 05-30-2012, 07:34 AM   #5
Registered: Jun 2007
Location: london
Distribution: ubuntu 12.04 LTS
Posts: 46

Original Poster
Rep: Reputation: 15
Many thanks for all your time in this matter. I have taken all your advice and i now have my 1st script file working as i need it too.

I might even look into making a GUI for my users to use it. just so they can set what directory and file to burn. a no frills app.

Again many thanks for your time in this matter.


dvd burning, scripts

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
tcsh script: can you time parts of the script? BrianK Programming 7 02-02-2010 08:20 PM
CloneCD Burners o0black0o Linux - Newbie 0 12-02-2003 10:34 PM
DVD Burners Stephanie Linux - General 3 02-27-2003 04:38 PM
PHP last time modified script time way off GabeF Programming 2 11-05-2002 09:05 PM
CD Burners sancho5 Linux - General 1 09-16-2001 05:30 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration