summarylogtreecommitdiffstats
path: root/pendrive-detect
blob: 143632f127342a526147a6fca980a7fa42ce28be (plain)
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
#!/bin/bash
# Simona Pisano - 2018-11-18
# simona-scripts
# Libertamente utilizzabile sotto GPL v3

  #sget-pendrive() {
    n_found=0
    for short_dev in `lsblk -ndlo NAME` ; do
      drive="/dev/$short_dev"
      if [[ -b $drive ]] ; then
      short_dev="${drive##*/}" # estract substring: take only final part after last '/' char
    #for drive in /dev/sd?* /dev/nvme* ; do
      #if [[ -b $drive ]] ; then

        letter=${drive:7:1}
        #short_dev="${drive##*/}" # estract substring: take only final part after last '/' char

        #base search
        udev=`udevadm info -q path -n $drive`
        udev=`udevadm info -a -p $udev `

        search=`echo KERNEL==\"$short_dev\"`
        udev_info=`echo "$udev" | grep $search | tr -d ' ' `
        [[ $udev_info != "KERNEL==\"$short_dev\"" ]] && continue #not correct device

        udev_info=`echo "$udev" | grep 'SUBSYSTEM=="block"' | tr -d ' ' `
        [[ $udev_info != "SUBSYSTEM==\"block\"" ]] && continue #not correct device

        udev_info=`echo "$udev" | grep 'ATTR{events}=="media_change"' | tr -d ' ' `
        [[ $udev_info != "ATTR{events}==\"media_change\"" ]] && continue #not correct device

        #check > 0 per evitare device pur presenti ma senza memoria inserita
        udev_info=`echo "$udev" | grep -E 'ATTR{size}=="[0-9]{1,}"' | tr -d ' ' `
        [[ $udev_info != *"ATTR{size}=="* ]] && continue #not correct device
        value=` echo "$udev_info" | cut -d "\"" -f2 | cut -d "\"" -f1 `
        [[ $value -lt 1 ]] && continue #not correct device

        echo -e "Pendrive \e[1;39;41m$drive\e[0m (size $(($value *512/1000/1000/1000 ))GBi) detected."
        #break #uncomment to stop search after the first

        n_found=$(( $n_found + 1 ))
      fi
    done
    [[ $n_found -lt 1 ]] && echo "No pendrive found" || echo "$n_found pendrives total found."
  #}