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 |
local count=0 |
45 |
while [ ! -z "${dev[$count]}" ];do |
46 |
if [ $findWriter -eq 1 ] && [ ${property[$count]} -eq 1 ];then |
47 |
echo -n "Device " |
48 |
setterm -foreground magenta |
49 |
echo -n "/dev/${dev[$count]}" |
50 |
setterm -foreground white |
51 |
echo -n " (id=$count) " |
52 |
else |
53 |
echo -n "Device /dev/${dev[$count]} (id=$count) " |
54 |
fi |
55 |
if [ ${property[$count]} -eq 1 ];then |
56 |
echo -n "can " |
57 |
if [ $findWriter -eq 1 ] && [ "$ejectDev" = "-1" ]; then |
58 |
ejectDev=$count |
59 |
fi |
60 |
else |
61 |
echo -n "can not " |
62 |
fi |
63 |
|
64 |
if [ $findWriter -eq 0 ];then |
65 |
echo -n "read " |
66 |
else |
67 |
echo -n "write " |
68 |
fi |
69 |
if [ $findDVD -eq 0 ];then |
70 |
echo "CDs" |
71 |
else |
72 |
echo "DVDs" |
73 |
fi |
74 |
((count++)) |
75 |
done |
76 |
if [ $ejectDevice -eq 1 ];then |
77 |
#if [ $ejectDev -eq -1 ];then ejectDev=0;fi |
78 |
eject /dev/"${dev[$ejectDev]}" |
79 |
fi |
80 |
} |
81 |
|
82 |
function getDevices(){ |
83 |
local count=0 |
84 |
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}') |
85 |
if [ -z "$theDevs" ];then |
86 |
echo "No CD-ROM Device found on this mashine" |
87 |
exit 1 |
88 |
fi |
89 |
local sortedDevs=$(echo "$theDevs" | sort) |
90 |
if [ "$theDevs" != "$sortedDevs" ];then needsSorting=1;fi |
91 |
for n in $(echo "$theDevs") |
92 |
# for n in hde hdd hdc hdb |
93 |
do |
94 |
dev[$count]=$n |
95 |
numOfDev=$count |
96 |
((count++)) |
97 |
done |
98 |
} |
99 |
|
100 |
function version(){ |
101 |
echo "$(basename "$0") - v $versionNumber - S. Georgaras <sng@hellug.gr>" |
102 |
echo |
103 |
} |
104 |
|
105 |
function getDeviceType(){ |
106 |
local count=0 |
107 |
found=0 |
108 |
|
109 |
if [ -z "$checkType" ];then |
110 |
property[0]=1 |
111 |
property[1]=1 |
112 |
found=2 |
113 |
else |
114 |
for n in $(cat /proc/sys/dev/cdrom/info | grep "$checkType" |sed "s|$checkType[ \t]*||" | awk '{for(i=1;i<=NF;i++) print $i}') |
115 |
# for n in 1 1 1 1 |
116 |
do |
117 |
property[$count]=$n |
118 |
if [ $n -eq 1 ];then ((found++));fi |
119 |
((count++)) |
120 |
done |
121 |
fi |
122 |
} |
123 |
|
124 |
function help(){ |
125 |
version |
126 |
echo "$(basename "$0") will try to identify your CD/DVDs |
127 |
You can use it to the device name of your CD-Reader (default), |
128 |
CD-Writer, DVD-Reader, and DVD-Writer. |
129 |
|
130 |
Usage: $(basename "$0") [options] |
131 |
Availabe options are: |
132 |
d Print info about DVDs |
133 |
w Print info about writes |
134 |
b Batch mode. Only print one device name. |
135 |
If more than one device is found, print |
136 |
nothing. For use with scripts |
137 |
e deviceID Eject device deviceID |
138 |
Accecpable values: -1...num of devices |
139 |
Use -1 when in batch mode |
140 |
v Print version info and exit |
141 |
h Print this screen and exit |
142 |
" |
143 |
} |
144 |
|
145 |
######################################### |
146 |
# |
147 |
# Script starts here |
148 |
# |
149 |
######################################### |
150 |
ejectDevice=0 |
151 |
ejectDev=-1 |
152 |
batch=0 |
153 |
findWriter=0 |
154 |
findDVD=0 |
155 |
needsSorting=0 |
156 |
while getopts ":vhe:bwd" Option |
157 |
do |
158 |
case $Option in |
159 |
d) |
160 |
findDVD=1;; |
161 |
w) |
162 |
findWriter=1;; |
163 |
b) |
164 |
batch=1;; |
165 |
e) |
166 |
if [ ! -z "$OPTARG" ];then |
167 |
ejectDev="$OPTARG" |
168 |
fi |
169 |
ejectDevice=1;; |
170 |
h) # show help |
171 |
help |
172 |
exit 0;; |
173 |
v) # show version |
174 |
version |
175 |
exit 0 |
176 |
esac |
177 |
done |
178 |
shift $(($OPTIND - 1)) |
179 |
# set -x |
180 |
if [ $findDVD -eq 0 ];then |
181 |
if [ $findWriter -eq 0 ];then |
182 |
checkType='' |
183 |
else |
184 |
checkType='Can write CD-R:' |
185 |
fi |
186 |
else |
187 |
if [ $findWriter -eq 0 ];then |
188 |
checkType='Can read DVD:' |
189 |
else |
190 |
checkType='Can write DVD-R:' |
191 |
fi |
192 |
fi |
193 |
getDevices |
194 |
getDeviceType |
195 |
# if [ $needsSorting -eq 1 ];then echo needs sorting;fi |
196 |
if [ $batch -eq 0 ];then |
197 |
printResults |
198 |
else |
199 |
# found='' |
200 |
if [ $found -eq 1 ];then printDevice; fi |
201 |
fi |
202 |
|
203 |
|