#!/bin/bash ############################################################################## # # # what-cd, (C) 2007-2010 S. Georgaras # # # # This file is part of Clonezilla-SysRescCD. # # # # Clonezilla-SysRescCD is free software: you can redistribute it and/or # # modify it under the terms of the GNU General Public License as published # # by the Free Software Foundation, either version 2 of the License, or # # (at your option) any later version. # # # # Clonezilla-SysRescCD is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License along # # with Clonezilla-SysRescCD. If not, see . # # # ############################################################################## versionNumber=1.0.2 function printDevice(){ local count=0 while [ ! -z "${dev[$count]}" ];do if [ ${property[$count]} -eq 1 ];then echo /dev/"${dev[$count]}" break fi ((count++)) done if [ $ejectDevice -eq 1 ];then if [ $ejectDev -eq -1 ];then ejectDev=$count;fi if [ ! -z "${dev[$count]}" ];then eject /dev/"${dev[$ejectDev]}" fi fi } function printResults(){ # Display results local count=0 while [ ! -z "${dev[$count]}" ];do if [ $findWriter -eq 1 ] && [ ${property[$count]} -eq 1 ];then echo -n "Device " setterm -foreground magenta echo -n "/dev/${dev[$count]}" setterm -foreground white echo -n " (id=$count) " else echo -n "Device /dev/${dev[$count]} (id=$count) " fi if [ ${property[$count]} -eq 1 ];then echo -n "can " if [ $findWriter -eq 1 ] && [ "$ejectDev" = "-1" ]; then ejectDev=$count fi else echo -n "can not " fi if [ $findWriter -eq 0 ];then echo -n "read " else echo -n "write " fi if [ $findDVD -eq 0 ];then echo "CDs" else echo "DVDs" fi ((count++)) done if [ $ejectDevice -eq 1 ];then #if [ $ejectDev -eq -1 ];then ejectDev=0;fi eject /dev/"${dev[$ejectDev]}" fi } function getDevices(){ local count=0 local theDevs=$(cat /proc/sys/dev/cdrom/info | grep 'drive name' |sed 's|drive name:[ \t]*||' | awk '{for(i=1;i<=NF;i++) print $i}') if [ -z "$theDevs" ];then echo "No CD-ROM Device found on this mashine" exit 1 fi local sortedDevs=$(echo "$theDevs" | sort) if [ "$theDevs" != "$sortedDevs" ];then needsSorting=1;fi for n in $(echo "$theDevs") # for n in hde hdd hdc hdb do dev[$count]=$n numOfDev=$count ((count++)) done } function version(){ echo "$(basename "$0") - v $versionNumber - S. Georgaras " echo } function getDeviceType(){ local count=0 found=0 if [ -z "$checkType" ];then property[0]=1 property[1]=1 found=2 else for n in $(cat /proc/sys/dev/cdrom/info | grep "$checkType" |sed "s|$checkType[ \t]*||" | awk '{for(i=1;i<=NF;i++) print $i}') # for n in 1 1 1 1 do property[$count]=$n if [ $n -eq 1 ];then ((found++));fi ((count++)) done fi } function help(){ version echo "$(basename "$0") will try to identify your CD/DVDs You can use it to the device name of your CD-Reader (default), CD-Writer, DVD-Reader, and DVD-Writer. Usage: $(basename "$0") [options] Availabe options are: d Print info about DVDs w Print info about writes b Batch mode. Only print one device name. If more than one device is found, print nothing. For use with scripts e deviceID Eject device deviceID Accecpable values: -1...num of devices Use -1 when in batch mode v Print version info and exit h Print this screen and exit " } ######################################### # # Script starts here # ######################################### ejectDevice=0 ejectDev=-1 batch=0 findWriter=0 findDVD=0 needsSorting=0 while getopts ":vhe:bwd" Option do case $Option in d) findDVD=1;; w) findWriter=1;; b) batch=1;; e) if [ ! -z "$OPTARG" ];then ejectDev="$OPTARG" fi ejectDevice=1;; h) # show help help exit 0;; v) # show version version exit 0 esac done shift $(($OPTIND - 1)) # set -x if [ $findDVD -eq 0 ];then if [ $findWriter -eq 0 ];then checkType='' else checkType='Can write CD-R:' fi else if [ $findWriter -eq 0 ];then checkType='Can read DVD:' else checkType='Can write DVD-R:' fi fi getDevices getDeviceType # if [ $needsSorting -eq 1 ];then echo needs sorting;fi if [ $batch -eq 0 ];then printResults else # found='' if [ $found -eq 1 ];then printDevice; fi fi