linuxbashusbstatusbarx-dwm

detecting when a usb drive is connected script not working as intended


im using DWM (Tiling Window Manager) with dwmblocks for the statusbar on my laptop. i have 3 usb ports on this laptop. im using this script to detect and show a block(icon) on the statusbar when i connect a usb drive.

if [[ "$(dmesg -S | tail | grep "usb-storage")" =~ "USB Mass Storage device detected" ]]; then
           icon=" 🎙️ "
           else
           icon=""
   fi

   printf "%s%s\\n" "$icon"

but this script is not good enough. when i connect 1 usb it gives this

[  151.134547] usb-storage 1-2:1.0: USB Mass Storage device detected
[  151.138129] scsi host2: usb-storage 1-2:1.0
[  152.157810] scsi 2:0:0:0: Direct-Access     MBIL SSM Moser Baer Disk  8.07 PQ: 0 ANSI: 4
[  152.158051] sd 2:0:0:0: Attached scsi generic sg2 type 0
[  152.158711] sd 2:0:0:0: [sdb] 31027200 512-byte logical blocks: (15.9 GB/14.8 GiB)
[  152.159381] sd 2:0:0:0: [sdb] Write Protect is off
[  152.159387] sd 2:0:0:0: [sdb] Mode Sense: 23 00 00 00
[  152.160066] sd 2:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[  152.924036]  sdb: sdb1 sdb2 sdb3
[  152.926370] sd 2:0:0:0: [sdb] Attached SCSI removable disk

when i connect another usb at the same time it gives this

[  364.948449] scsi host3: usb-storage 1-1:1.0
[  366.206529] scsi 3:0:0:0: Direct-Access     SRT      USB              1100 PQ: 0 ANSI: 4
[  366.206834] sd 3:0:0:0: Attached scsi generic sg3 type 0
[  366.207126] sd 3:0:0:0: [sdc] 15818752 512-byte logical blocks: (8.10 GB/7.54 GiB)
[  366.207629] sd 3:0:0:0: [sdc] Write Protect is off
[  366.207631] sd 3:0:0:0: [sdc] Mode Sense: 43 00 00 00
[  366.208117] sd 3:0:0:0: [sdc] No Caching mode page found
[  366.208119] sd 3:0:0:0: [sdc] Assuming drive cache: write through
[  366.237610]  sdc: sdc1
[  366.239332] sd 3:0:0:0: [sdc] Attached SCSI removable disk

which doesn't have usb-storage to grep so the icon disappears even though i have 2 usb connected.

when i connect a usb portable 2.5" hdd it gives this ( i extended tail range to -n 15 )

[  659.426376] usb 1-3: Product: USB 3.0 Device
[  659.426377] usb 1-3: Manufacturer: USB 3.0 Device
[  659.426379] usb 1-3: SerialNumber: 00000000B032
[  659.433060] scsi host4: uas
[  659.433839] scsi 4:0:0:0: Direct-Access     WDC WD10 SPZX-00Z10T0     0117 PQ: 0 ANSI: 6
[  659.435213] sd 4:0:0:0: Attached scsi generic sg4 type 0
[  659.435840] sd 4:0:0:0: [sdd] 1953525168 512-byte logical blocks: (1.00 TB/932 GiB)
[  659.435842] sd 4:0:0:0: [sdd] 4096-byte physical blocks
[  659.436038] sd 4:0:0:0: [sdd] Write Protect is off
[  659.436041] sd 4:0:0:0: [sdd] Mode Sense: 53 00 10 08
[  659.436349] sd 4:0:0:0: [sdd] Disabling FUA
[  659.436351] sd 4:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  659.436476] sd 4:0:0:0: [sdd] Optimal transfer size 33553920 bytes not a multiple of physical block size (4096 bytes)
[  659.578561]  sdd: sdd1
[  659.580012] sd 4:0:0:0: [sdd] Attached SCSI disk

usb hdd doesn't even have a usb-storage to grep.

also when 1 of the flash drive was connected and i rebooted the icon didn't show because the dmesg tail was about my wifi being connected and networkmanager.

any idea's for improving this script, as this is not reliable.


Solution

  • I found the solution to this on linuxquestions.org.

    Solution -

    #!/bin/bash
    
    if [[ "$(udisksctl dump|awk -F':\n' -vRS= '/[ \t]*HintAuto:[ \t]*true/&&/\.Filesystem:/{print "found"}')" != "" ]]; then
        icon=" "
    else
        icon=""
    fi
    
    printf "%s%s\\n" "$icon"