LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using 2 Burners at the same time Script (https://www.linuxquestions.org/questions/programming-9/using-2-burners-at-the-same-time-script-944714/)

keiffee30 05-13-2012 06:46 AM

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.

#!/bin/sh

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


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/multiburn.sh


Then to run the script i would need to type in the command line.

multiburn.sh /path/to/image.iso
or
multiburn.sh /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

David the H. 05-13-2012 09:25 AM

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.

Code:

#!/bin/sh
#!/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.

Code:

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.

Code:

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:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

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.

michaelk 05-13-2012 09:43 AM

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...

Nominal Animal 05-13-2012 11:13 AM

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

#!/bin/bash

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

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

wait

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.

keiffee30 05-30-2012 06:34 AM

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.


All times are GMT -5. The time now is 05:37 AM.