1 |
#!/bin/bash |
2 |
############################################################################## |
3 |
# # |
4 |
# what-cd, (C) 2007-2010 S. Georgaras <sng@hellug.gr> # |
5 |
# # |
6 |
# This file is part of Clonezilla-SysRescCD. # |
7 |
# # |
8 |
# Clonezilla-SysRescCD is free software: you can redistribute it and/or # |
9 |
# modify it under the terms of the GNU General Public License as published # |
10 |
# by the Free Software Foundation, either version 2 of the License, or # |
11 |
# (at your option) any later version. # |
12 |
# # |
13 |
# Clonezilla-SysRescCD is distributed in the hope that it will be useful, # |
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
16 |
# GNU General Public License for more details. # |
17 |
# # |
18 |
# You should have received a copy of the GNU General Public License along # |
19 |
# with Clonezilla-SysRescCD. If not, see <http://www.gnu.org/licenses/>. # |
20 |
# # |
21 |
############################################################################## |
22 |
|
23 |
versionNumber=1.0.2 |
24 |
|
25 |
function printDevice(){ |
26 |
local count=0 |
27 |
while [ ! -z "${dev[$count]}" ];do |
28 |
if [ ${property[$count]} -eq 1 ];then |
29 |
echo /dev/"${dev[$count]}" |
30 |
break |
31 |
fi |
32 |
((count++)) |
33 |
done |
34 |
if [ $ejectDevice -eq 1 ];then |
35 |
if [ $ejectDev -eq -1 ];then ejectDev=$count;fi |
36 |
if [ ! -z "${dev[$count]}" ];then |
37 |
eject /dev/"${dev[$ejectDev]}" |
38 |
fi |
39 |
fi |
40 |
} |
41 |
|
42 |
function printResults(){ |
43 |
# Display results |
44 |
set -x |
45 |
local count=0 |
46 |
while [ ! -z "${dev[$count]}" ];do |
47 |
if [ $findWriter -eq 1 ] && [ ${property[$count]} -eq 1 ];then |
48 |
echo -n "Device " |
49 |
setterm -foreground magenta |
50 |
echo -n "/dev/${dev[$count]}" |
51 |
setterm -foreground white |
52 |
echo -n " (id=$count) " |
53 |
else |
54 |
echo -n "Device /dev/${dev[$count]} (id=$count) " |
55 |
fi |
56 |
if [ ${property[$count]} -eq 1 ];then |
57 |
echo -n "can " |
58 |
if [ $findWriter -eq 1 ] && [ "$ejectDev" = "-1" ]; then |
59 |
ejectDev=$count |
60 |
fi |
61 |
else |
62 |
echo -n "can not " |
63 |
fi |
64 |
|
65 |
if [ $findWriter -eq 0 ];then |
66 |
echo -n "read " |
67 |
else |
68 |
echo -n "write " |
69 |
fi |
70 |
if [ $findDVD -eq 0 ];then |
71 |
echo "CDs" |
72 |
else |
73 |
echo "DVDs" |
74 |
fi |
75 |
((count++)) |
76 |
done |
77 |
if [ $ejectDevice -eq 1 ];then |
78 |
#if [ $ejectDev -eq -1 ];then ejectDev=0;fi |
79 |
eject /dev/"${dev[$ejectDev]}" |
80 |
fi |
81 |
} |
82 |
|
83 |
function getDevices(){ |
84 |
local count=0 |
85 |
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}') |
86 |
if [ -z "$theDevs" ];then |
87 |
echo "No CD-ROM Device found on this mashine" |
88 |
exit 1 |
89 |
fi |
90 |
local sortedDevs=$(echo "$theDevs" | sort) |
91 |
if [ "$theDevs" != "$sortedDevs" ];then needsSorting=1;fi |
92 |
for n in $(echo "$theDevs") |
93 |
# for n in hde hdd hdc hdb |
94 |
do |
95 |
dev[$count]=$n |
96 |
numOfDev=$count |
97 |
((count++)) |
98 |
done |
99 |
} |
100 |
|
101 |
function version(){ |
102 |
echo "$(basename "$0") - v $versionNumber - S. Georgaras <sng@hellug.gr>" |
103 |
echo |
104 |
} |
105 |
|
106 |
function getDeviceType(){ |
107 |
local count=0 |
108 |
found=0 |
109 |
|
110 |
if [ -z "$checkType" ];then |
111 |
property[0]=1 |
112 |
property[1]=1 |
113 |
found=2 |
114 |
else |
115 |
for n in $(cat /proc/sys/dev/cdrom/info | grep "$checkType" |sed "s|$checkType[ \t]*||" | awk '{for(i=1;i<=NF;i++) print $i}') |
116 |
# for n in 1 1 1 1 |
117 |
do |
118 |
property[$count]=$n |
119 |
if [ $n -eq 1 ];then ((found++));fi |
120 |
((count++)) |
121 |
done |
122 |
fi |
123 |
} |
124 |
|
125 |
function help(){ |
126 |
version |
127 |
echo "$(basename "$0") will try to identify your CD/DVDs |
128 |
You can use it to the device name of your CD-Reader (default), |
129 |
CD-Writer, DVD-Reader, and DVD-Writer. |
130 |
|
131 |
Usage: $(basename "$0") [options] |
132 |
Availabe options are: |
133 |
d Print info about DVDs |
134 |
w Print info about writes |
135 |
b Batch mode. Only print one device name. |
136 |
If more than one device is found, print |
137 |
nothing. For use with scripts |
138 |
e deviceID Eject device deviceID |
139 |
Accecpable values: -1...num of devices |
140 |
Use -1 when in batch mode |
141 |
v Print version info and exit |
142 |
h Print this screen and exit |
143 |
" |
144 |
} |
145 |
|
146 |
######################################### |
147 |
# |
148 |
# Script starts here |
149 |
# |
150 |
######################################### |
151 |
ejectDevice=0 |
152 |
ejectDev=-1 |
153 |
batch=0 |
154 |
findWriter=0 |
155 |
findDVD=0 |
156 |
needsSorting=0 |
157 |
while getopts ":vhe:bwd" Option |
158 |
do |
159 |
case $Option in |
160 |
d) |
161 |
findDVD=1;; |
162 |
w) |
163 |
findWriter=1;; |
164 |
b) |
165 |
batch=1;; |
166 |
e) |
167 |
if [ ! -z "$OPTARG" ];then |
168 |
ejectDev="$OPTARG" |
169 |
fi |
170 |
ejectDevice=1;; |
171 |
h) # show help |
172 |
help |
173 |
exit 0;; |
174 |
v) # show version |
175 |
version |
176 |
exit 0 |
177 |
esac |
178 |
done |
179 |
shift $(($OPTIND - 1)) |
180 |
# set -x |
181 |
if [ $findDVD -eq 0 ];then |
182 |
if [ $findWriter -eq 0 ];then |
183 |
checkType='' |
184 |
else |
185 |
checkType='Can write CD-R:' |
186 |
fi |
187 |
else |
188 |
if [ $findWriter -eq 0 ];then |
189 |
checkType='Can read DVD:' |
190 |
else |
191 |
checkType='Can write DVD-R:' |
192 |
fi |
193 |
fi |
194 |
getDevices |
195 |
getDeviceType |
196 |
# if [ $needsSorting -eq 1 ];then echo needs sorting;fi |
197 |
if [ $batch -eq 0 ];then |
198 |
printResults |
199 |
else |
200 |
# found='' |
201 |
if [ $found -eq 1 ];then printDevice; fi |
202 |
fi |
203 |
|
204 |
|