LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-16-2006, 10:35 PM   #1
khairil
LQ Newbie
 
Registered: May 2005
Distribution: gentoo
Posts: 23

Rep: Reputation: 15
detect multi session cd


i am writing a bash shell script to automate cd burning. what i'm trying to do is,
1. if a user insert a blank cd it will burn with multi session (new multi session).
2. if not the script will try to detect whether the disc inserted is multi session disc or not.
3. if the disc is multi session disc then the script will continue writing with next session.

however i have difficulty to detect if the disc is multi session disc.

using the code from http://mlug.missouri.edu/~rjudd/proj...d/README.multi

for new disc;
Code:
icsb:/home/khairil # cdrecord -msinfo dev=1,0,0
cdrecord: Input/output error. read toc: scsi sendcmd: no error
CDB:  43 00 01 00 00 00 00 00 04 00
status: 0x2 (CHECK CONDITION)
Sense Bytes: 70 00 05 00 00 00 00 0A 00 00 00 00 24 00 00 00
Sense Key: 0x5 Illegal Request, Segment 0
Sense Code: 0x24 Qual 0x00 (invalid field in cdb) Fru 0x0
Sense flags: Blk 0 (not valid)
cmd finished after 0.000s timeout 40s
cdrecord: Cannot read session offset
for multi session disc
Code:
icsb:/home/khairil # cdrecord -msinfo dev=1,0,0
0,11702
how do i make my script detect a multi session disc?

thx!
 
Old 11-17-2006, 07:51 AM   #2
ZeromusM
LQ Newbie
 
Registered: May 2006
Distribution: OpenSUSE 10.0
Posts: 27

Rep: Reputation: 15
One possible way to do it might be to test against the exit status of the command instead of its output.

Code:
cdrecord -msinfo dev=1,0,0 &> /dev/null
if [ $? -eq 0 ]
then
  .....
The $? variable contains the exit status of the last command. This is just an idea for you to try, it may not work if, for example, the script gives you the same exit status either way.
 
Old 11-17-2006, 10:16 AM   #3
khairil
LQ Newbie
 
Registered: May 2005
Distribution: gentoo
Posts: 23

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ZeromusM
The $? variable contains the exit status of the last command. This is just an idea for you to try, it may not work if, for example, the script gives you the same exit status either way.
yes exactly! i tried this method before..

is there anyway i can count line of the output
Code:
cdrecord -msinfo dev=1,0,0
what i thought is;
- if that command runs on MULTI SESSION cd the output of the command will be just ONE LINE
- the same command if runs on NON MULTI SESSION cd will return MORE THAN ONE LINE..

or if there is better ways please let me know.

thx!
 
Old 11-17-2006, 10:33 AM   #4
ZeromusM
LQ Newbie
 
Registered: May 2006
Distribution: OpenSUSE 10.0
Posts: 27

Rep: Reputation: 15
A simple way to count lines would be:

Code:
cdrecord -msinfo dev=1,0,0 2>&1 | wc -l
Alternately, you could leave out the redirect to omit STDERR from the word count. This is pretty simple logic though, so I'd test it with all the different situations you might run into just to make sure the program reacts as expected.

You could also try counting only STDERR.
 
Old 11-18-2006, 02:36 AM   #5
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
Hi, I'm using the bash function getCdInfo() underneath to determine if a CD is multi-session.

It's part of a complete bash script to create a nightly dar backup and write it to cdrw. You can see the full script here:
http://www.fvue.nl/wiki/Bash:_Backup_on_cdrw

Hope this helps,

Freddy Vulto

Code:
# Get info from current loaded cd
# @param string $1  Name of device containing the cd.
# @return
# @global string $cdIsMultiSession  1 if cd is multi-session, 0 if not
# @global string $cdMultiSessionInfo  Format: last_sess_start,next_sess_start
#    The first number is the sector number of the first sector in the last 
#    session of the disk that should be appended to.  The second number is 
#    the starting sector number of the new session.
#    Example values are:
#    0,-1       : readonly CD
#    0,24448    : multi-session CD with one previous session
#    null       : blank CD:
# @global integer $cdLastSessionStart  Starting sector number of last successful written session.
# @global integer $cdNextSessionStart  Starting sector number of new session.
# @global integer $cdNextSessionNr     Session number of new session, origin = 0.
#
# This software is released under the terms of the GNU General Public License.
# Copyright (C) 2006  Freddy Vulto
#
# The latest version of this software can be obtained here:
# http://www.fvue.nl/wiki/Bash:_Backup_on_cdrw
#
function getCdInfo() {
        # Retrieve maximum number of blocks on cd
    cdMaxNrOfBlocks=`\
        sudo cdrecord dev=$1 -atip 2> /dev/null |\
        sed -rn 's/^  ATIP start of lead out: ([0-9]+).*/\1/ p'`
    cdIsMultiSession=1
    cdLastSessionStart=
    cdMkisofsMultiSessionOptions=
    cdMultiSessionInfo=`sudo cdrecord -msinfo dev=$1`
    cdNextSessionNr=0
    cdNextSessionStart=
    if [ $cdMultiSessionInfo ]; then
        cdLastSessionStart="`echo $cdMultiSessionInfo | awk -F, '{ print $1 }'`"
        cdNextSessionStart="`echo $cdMultiSessionInfo | awk -F, '{ print $2 }'`"
        cdMkisofsMultiSessionOptions="-C $cdMultiSessionInfo -M $1"
        cdNextSessionNr=2  # Or greater...
        if (( $cdNextSessionStart == -1 )); then
            cdIsMultiSession=0
        elif (( $cdLastSessionStart == 0 )); then
            cdNextSessionNr=1
        fi
    fi
} # getCdInfo()

Last edited by fvu; 11-24-2006 at 01:39 AM.
 
Old 11-28-2006, 08:47 PM   #6
khairil
LQ Newbie
 
Registered: May 2005
Distribution: gentoo
Posts: 23

Original Poster
Rep: Reputation: 15
wow!

that's really cool!

thanks to all.. i can now finalize my script!
 
  


Reply



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
get information on multi session cd. bruse Linux - Software 2 09-10-2005 07:12 AM
multi session cd. bruse Linux - Newbie 7 09-07-2005 09:06 AM
Help with multi session download une Mandriva 1 11-24-2004 01:31 AM
dd multi-session CDs goofyheadedpunk Slackware 6 10-15-2004 12:48 PM
KDE multi session MadCow Mandriva 6 10-20-2003 02:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

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
Open Source Consulting | Domain Registration