-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf_usb.sh
executable file
·135 lines (117 loc) · 3.23 KB
/
fzf_usb.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
# path: /home/klassiker/.local/share/repos/fzf/fzf_usb.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/fzf
# date: 2024-07-03T08:52:56+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# auth can be something like sudo -A, doas -- or nothing,
# depending on configuration requirements
auth="${EXEC_AS_USER:-sudo}"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to manage usb devices
Usage:
$script [--bind/--unbind/--rebind]
Settings:
[--bind] = try to bind logical device
[--unbind] = try to unbind logical device
[--rebind] = try to unbind and bind logical device
Examples:
$script
$script --bind \"Bus 003 Device 001\"
$script --unbind \"Bus 003 Device 001\"
$script --rebind \"Bus 003 Device 001\""
logical_device() {
path_devices="/sys/bus/usb/devices"
path_drivers="/sys/bus/usb/drivers/usb"
ports=$(find "$path_devices" -type l \
| sort \
| cut -d '/' -f6 \
)
device() {
for port in $ports; do
get_info() {
[ -e "$path_devices/$port/$1" ] \
&& cat "$path_devices/$port/$1"
}
printf "Bus %03d Device %03d:%s\n" \
"$(get_info "busnum")" \
"$(get_info "devnum")" \
"$port" \
| awk '{$1=$1;print}'
done \
| grep -m1 "$1" \
| cut -d':' -f2
}
$auth sh -c "printf '%s' \"$(device "$1")\" \
> \"$path_drivers/$2\"" 2>/dev/null \
|| return 0
}
usb() {
[ -n "$2" ] \
&& select=$(lsusb \
| grep -m1 "$2" \
| cut -d ':' -f1 \
)
case "$1" in
rebind)
logical_device "$select" "unbind"
sleep 1
logical_device "$select" "bind"
;;
*bind)
logical_device "$select" "$1"
;;
*)
exit 0
;;
esac
}
select_usb() {
device_info="$(lsusb -v 2>/dev/null)"
select=$(lsusb \
| cut -d ':' -f1 \
| sort -k 2,4 \
| fzf --cycle \
--bind 'focus:transform-preview-label:echo [ {} ]' \
--preview-window "right:75%,wrap" \
--preview "printf '%s\n\n' '$device_info' \
| sed \
-e '/./{H;$!d;}' \
-e 'x;/{1} {2} {3} {4}/!d;' \
| sed '/^$/d'" \
)
[ -n "$select" ] \
&& bind=$(printf "%s\n" \
"bind" \
"unbind" \
"rebind" \
| fzf --cycle \
--bind 'focus:transform-preview-label:echo [ {} ]' \
--preview-window "right:75%,wrap" \
--preview "printf '%s\n\n' '$device_info' \
| sed \
-e '/./{H;$!d;}' \
-e 'x;/$select/!d;' \
| sed '/^$/d'" \
)
[ -n "$bind" ] \
&& usb "$bind"
}
case "$1" in
-h | --help)
printf "%s\n" "$help" \
&& exit 0
;;
--*)
usb "${1##*--}" "$2"
;;
*)
select_usb \
&& sleep 1 \
&& "$0" \
|| exit 0
;;
esac