Advertisement
simo-reby

pendrive-detect

Aug 14th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.86 KB | None | 0 0
  1. #!/bin/bash
  2. # Simona Pisano - 2018-11-18
  3. # simona-scripts
  4. # Libertamente utilizzabile sotto GPL v3
  5.  
  6.   #sget-pendrive() {
  7.     n_found=0
  8.     for short_dev in `lsblk -ndlo NAME` ; do
  9.       drive="/dev/$short_dev"
  10.       if [[ -b $drive ]] ; then
  11.       short_dev="${drive##*/}" # estract substring: take only final part after last '/' char
  12.     #for drive in /dev/sd?* /dev/nvme* ; do
  13.       #if [[ -b $drive ]] ; then
  14.  
  15.         letter=${drive:7:1}
  16.         #short_dev="${drive##*/}" # estract substring: take only final part after last '/' char
  17.  
  18.         #base search
  19.         udev=`udevadm info -q path -n $drive`
  20.         udev=`udevadm info -a -p $udev `
  21.  
  22.         search=`echo KERNEL==\"$short_dev\"`
  23.         udev_info=`echo "$udev" | grep $search | tr -d ' ' `
  24.         [[ $udev_info != "KERNEL==\"$short_dev\"" ]] && continue #not correct device
  25.  
  26.         udev_info=`echo "$udev" | grep 'SUBSYSTEM=="block"' | tr -d ' ' `
  27.         [[ $udev_info != "SUBSYSTEM==\"block\"" ]] && continue #not correct device
  28.  
  29.         udev_info=`echo "$udev" | grep 'ATTR{events}=="media_change"' | tr -d ' ' `
  30.         [[ $udev_info != "ATTR{events}==\"media_change\"" ]] && continue #not correct device
  31.  
  32.         #check > 0 per evitare device pur presenti ma senza memoria inserita
  33.         udev_info=`echo "$udev" | grep -E 'ATTR{size}=="[0-9]{1,}"' | tr -d ' ' `
  34.         [[ $udev_info != *"ATTR{size}=="* ]] && continue #not correct device
  35.         value=` echo "$udev_info" | cut -d "\"" -f2 | cut -d "\"" -f1 `
  36.         [[ $value -lt 1 ]] && continue #not correct device
  37.  
  38.         echo -e "Pendrive \e[1;39;41m$drive\e[0m (size $(($value *512/1000/1000/1000 ))GBi) detected."
  39.         #break #uncomment to stop search after the first
  40.  
  41.         n_found=$(( $n_found + 1 ))
  42.       fi
  43.     done
  44.     [[ $n_found -lt 1 ]] && echo "No pendrive found" || echo "$n_found pendrives total found."
  45.   #}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement