-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver_for_devices.sh
executable file
·48 lines (42 loc) · 1.25 KB
/
driver_for_devices.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## Helper script to find the driver associated to the given linux device
## usage: get_driver_for_device.sh [options]
## options:
## -d <device> The path to the device. (TODO: let prepend /dev unnecessary)
[ $# -eq 0 ] && sed -ne 's/^## \(.*\)/\1/p' $0 && exit 1
while getopts 'hd:' OPT; do
case $OPT in
h)
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
d)
_device=$OPTARG
;;
\?)
echo "---"
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
esac
done
[ -z ${_device} ] && echo "device name is needed" && exit 1
dev_spec=$(ls -l $_device)
echo $dev_spec
# Get device type
[[ ${dev_spec} == b* ]] && dev_type="block"
[[ ${dev_spec} == c* ]] && dev_type="char"
[ -z ${dev_type} ] && echo "${_device} is not block nor character device." && exit 1
# Get major and minor number
# transform ls -s string in array
read -ra DEV_SPEC <<< "$dev_spec"
# Parameter expansion to get rid of the comma attached to the major number
major=${DEV_SPEC[4]%?}
minor=${DEV_SPEC[5]}
rv=`readlink -f /sys/dev/"$dev_type"/"$major"\\:"$minor"/device/driver/`
if [ -z $rv ]; then
echo "No driver found"
else
echo $rv
fi