Linux
檢測 USB 驅動器何時連接腳本未按預期工作
dwmblocks
我在筆記型電腦的狀態欄上使用了 DWM(平鋪視窗管理器) 。我在這台筆記型電腦上有 3 個 USB 埠。當我連接 USB 驅動器時,我使用此腳本檢測並在狀態欄上顯示一個塊(圖示)。if [[ "$(dmesg -S | tail | grep "usb-storage")" =~ "USB Mass Storage device detected" ]]; then icon=" 🎙️ " else icon="" fi printf "%s%s\\n" "$icon"
但是這個腳本還不夠好。當我連接 1 個 USB 時,它給出了這個
[ 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
當我同時連接另一個 USB 時,它給出了這個
[ 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
它沒有用於 grep 的 USB 儲存,因此即使我連接了 2 個 USB,圖示也會消失。
當我連接一個 USB 攜帶式 2.5" 硬碟時,它給出了這個(我將尾部範圍擴展到 -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 硬碟甚至沒有用於 grep 的 USB 儲存。
同樣,當連接了 1 個快閃記憶體驅動器並且我重新啟動時,圖示沒有顯示,因為 dmesg 尾部是關於我的 wifi 已連接和網路管理器。
任何想法都可以改進此腳本,因為這不可靠。
當然
通過使用 udiskctl 轉儲
#!/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"
你可以逃脫
udisksctl dump|grep 'HintAuto:\s*true'
如果您沒有 cd/dvd 驅動器或讀卡器