Welcome
Welcome to refracta

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

burn_iso.sh

Stuff that has not yet gone into the official build.

burn_iso.sh

Postby fsmithred » Sun Oct 27, 2013 8:45 pm

Here's some code that's been hanging around on my hard drive for a long time. It was originally going to be included at the end of refractasnapshot, but I made it a standalone program instead. Played with it some more today and deleted all the cli-only sections for simplicity. If a nogui script is needed, I think I'd go with something that takes command line options for filename and burn speed, rather than have it ask a lot of questions.

This is a test script. See the comments at the head if you want to burn a real disc. (comment out one line)
Extra echo crap will eventually be removed.
Code: Select all
#!/usr/bin/env bash
# burn_iso.sh     


## This is a test version. Comment out dry_run="--dummy" on the
## next line to burn a real CD or DVD.
dry_run="--dummy"


# if yad is installed, use in preference
if [[ -f /usr/bin/yad ]]; then

   DIALOG="yad"
   INFO="image=gtk-dialog-info"
   QUESTION="image=gtk-dialog-question"
   WARNING="image=gtk-dialog-warning"
   ERROR="image=gtk-dialog-error"
   
   #buttons
   BUTTON0="button"
   BUTTON1="button"
   BUTTON0NUM=":0"
   BUTTON1NUM=":1"

#cancel button always returns 1 as $?
#ok button always returns 0 as $?
#ok is default (highlighted)
#buttons in yad dialog window may show reversed from zenity window, e.g.
#yad: ok -- cancel (0 -- 1)
#zenity: cancel -- ok (1 -- 0)

elif [[ -f /usr/bin/zenity ]]; then

   # use zenity
   
   DIALOG="zenity"
   INFO="info"
   QUESTION="question"
   WARNING="warning"
   ERROR="error"
   
   #buttons
   BUTTON0="ok-label"
   BUTTON1="cancel-label"
   BUTTON0NUM=""
   BUTTON1NUM=""

else
   echo " Neither Yad nor Zenity is installed."
fi


if ! [[ $DISPLAY ]] ; then
   echo "Run this from an xsession."
   exit 1
fi


TITLE="burn_iso.sh"


exit_dialog () {
   $DIALOG --$INFO --width 360 --title="Exit Message" --text="Exiting...
$exit_message"
   exit 1
}


select_iso () {

isofile=$($DIALOG --file-selection --file-filter="*.[iI][sS][oO]" --file-filter="*"  --width=640 --height=640 --title=$"Select .iso file" --text="\n  Select the CD/DVD image file to burn.\n")

      
if ! [[ "$isofile" =~ .[Ii][Ss][Oo]$ ]]; then
   exit_message="\nYou did not select a CD/DVD image file."
   exit_dialog
fi

echo "*** isofile is $isofile"
}


select_device () {

   selection=$(wodim --devices | grep "/dev/" | $DIALOG --list --separator="" \
         --column 'Optical Drives' --height 230 --width 500 --title="Select optical drive" \
         --text="Put a blank disc in the drive and
select the CD/DVD burner from the list.

To skip this and exit, hit Cancel.")
   
   if [[ $? -eq 0 ]]; then
      if [[ -n "$selection" ]]; then
         burn_device=$(echo $selection | awk -F"'" '{ print $2 }')
echo "********* selection is $selection"
echo "********* burn_device is $burn_device"

      else
         exit_message="No device was selected."
         exit_dialog
      fi
   else
      exit 0
   fi
}


set_speed () {
supported_speeds=$(wodim -prcap dev="$burn_device" | tail |awk '/Write speed/ { printf $9 " " }' | sed 's/x,//g')

speed_list=($supported_speeds)
echo "Speed list is ${speed_list[@]}"


selected_speed=$($DIALOG --entry --entry-text="${speed_list[0]}" --text="Enter a valid burn speed. Default speed is maximum for your drive.

Supported Speeds:
$supported_speeds
")

if [[ $? -ne 0 ]]; then
   exit_message="user canceled"
   exit_dialog
fi

#   if $(echo  ${speed_list[@]} | grep -q $selected_speed); then  # either test works.
   if $(echo "$supported_speeds" | grep -q $selected_speed);then
      echo "Selected speed is valid"
   else
      exit_message="Unsupported speed chosen: $selected_speed"
      exit_dialog
   fi
}


burn_disc () {
iso_size=$(du -h "$isofile" | awk '{ print $1 }')
$DIALOG --$QUESTION --title="Burn iso" --width=400 \
--text="Burn iso file to disk?

Image file:   ${isofile##*/}
Size:      $iso_size
Speed:      ${selected_speed}x"

if [[ $? = 0 ]]; then
   wodim ${dry_run} dev="$burn_device" -v -eject driveropts=burnfree "${burn_speed}" -data "$isofile" | tee >($DIALOG --title=\"Burning CD...\" --progress --pulsate --width=300 --auto-close)
      if [[ $? -ne 0 ]]; then
         exit_message="Error! Burn failed."
         exit_dialog
      fi
else
   exit_message="user canceled"
   exit_dialog
fi
}


# Make sure wodim is installed.
if ! [[ $(type -p wodim) ]]; then
   exit_message="Error: Wodim is not installed."
   exit_dialog
fi


select_iso
select_device
set_speed
burn_disc

exit 0
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: burn_iso.sh

Postby fsmithred » Mon Oct 28, 2013 4:02 am

From the script above:
Code: Select all
#   if $(echo  ${speed_list[@]} | grep -q $selected_speed); then  # either test works.
   if $(echo "$supported_speeds" | grep -q $selected_speed);then
      echo "Selected speed is valid"
   else
      exit_message="Unsupported speed chosen: $selected_speed"
      exit_dialog
   fi


No, neither of those tests will work correctly if 32 is a valid speed and you choose to use 3. It'll tell you it's a valid speed when it isn't. Instead, do something like this, and don't ask me to explain it.
Code: Select all
containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

containsElement "$selected_speed" "${speed_list[@]}"

if [[ $? != 0 ]]; then
   echo "Unsupported speed chosen: $burn_speed

Supported Speeds:
$supported_speeds"
   exit 1
else
        blah


OK, I can say this much -"${@:2}" refers to the second argument to containsElement, which is ${speed_list[@]}", and $1 is the first argument, "$selected_speed".
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: burn_iso.sh

Postby fsmithred » Mon Oct 28, 2013 1:28 pm

Here's the cli version. You need to be in the directory that contains the iso file(s). You can specify the burn speed on the command line, or else call it with no argument, and it'll use the highest supported speed.

Example: To burn at 4x
Code: Select all
burn_iso.sh 4


Code: Select all
#!/usr/bin/env bash
# burn_iso.sh


exit_dialog () {
   echo -e "\n$exit_message\n"
   exit 1
}


# Select isofile from cli...
echo "
Run this script from the directory that contains the iso files.
First argument can be a burn speed. No argument uses maximum speed.
   Example:
         burn_iso.sh  4

Select file:
"
for i in *.iso ; do
   options=( "${options[@]}" "${i[@]}" )
done

select isofile in "${options[@]}"; do
   break
done
   
iso_size=$(du -h "$isofile" | awk '{ print $1 }')

# Select the optical drive
echo
wodim --devices
sleep 1
echo "
Select the burner or ctrl-c to exit.
"
for i in $(wodim --devices | awk -F"'" '/\/dev\// { print $2 }') ; do
   devices=( "${devices[@]}" "${i[@]}" )
done
select burn_device in "${devices[@]}" ; do
   echo " burn device is $burn_device"
   break
done

# Check that the chosen burning speed is supported by the device.
# If speed not specified on command line, use maximum.
supported_speeds=$(wodim -prcap dev="$burn_device" | tail |awk '/Write speed/ { printf $9 " " }' | sed 's/x,//g')
speed_list=($supported_speeds)
#echo "Speed list is ${speed_list[@]}"

if [[ $1 ]]; then
   burn_speed="speed=$1"
else
   burn_speed="speed=${speed_list[0]}"
fi

selected_speed=$(echo $burn_speed | awk -F"=" '{ print $2 }')

containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

containsElement "$selected_speed" "${speed_list[@]}"

if [[ $? != 0 ]]; then
   echo " Unsupported speed chosen: $burn_speed

Supported Speeds:
$supported_speeds"
   exit 1
else


      echo "
Image file:      $isofile
Size:         $iso_size
Burn Speed:      ${selected_speed}x
"
    read -p "  Press the ENTER key to proceed or crtl-c to exit.
"

         wodim --dummy dev="$burn_device" -v -eject driveropts=burnfree "${burn_speed}" -data "$isofile"
            if [[ $? -ne 0 ]]; then
               exit_message="Error! Burn failed."
               exit_dialog
            fi
fi
exit 0


Edit: I just realized that I ignored DVD speeds. Anyone know how to tell if a CD or DVD is in the drive?
(back to man wodim, I guess)

Edit2: I hacked the script so it used the dvd speeds and tried to write to a blank dvd. Got the error, "wodim: No such file or directory. Cannot open. '' I'm giving up on this. Not a total loss, though. I learned how to test to see if a variable is an element in an array. (See containsElement () in the script above.)
Last edited by fsmithred on Mon Oct 28, 2013 3:39 pm, edited 1 time in total.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm

Re: burn_iso.sh

Postby golinux » Mon Oct 28, 2013 3:08 pm

FWIW, I always burn at 4x to have the best chance of a 'good' burn. Maybe that's a myth but I still do it.
May the FORK be with you!
User avatar
golinux
 
Posts: 643
Joined: Thu Nov 08, 2012 1:23 am

Re: burn_iso.sh

Postby fsmithred » Mon Oct 28, 2013 3:42 pm

golinux wrote:FWIW, I always burn at 4x to have the best chance of a 'good' burn. Maybe that's a myth but I still do it.


I don't believe it's a myth. I've made bad discs at high speed and then gotten success with a lower speed. Bad disc was determined by trying to read the finished product - not a matter of the burn software telling me it failed. It might have to do with the quality of the blank media - not sure.
User avatar
fsmithred
 
Posts: 1987
Joined: Wed Mar 09, 2011 9:13 pm


Return to Experimental

Who is online

Users browsing this forum: No registered users and 0 guests

suspicion-preferred