检测 USB 驱动器何时连接脚本未按预期工作
detecting when a usb drive is connected script not working as intended
我正在使用 DWM(平铺 Window 管理器),dwmblocks
用于我笔记本电脑上的状态栏。我在这台笔记本电脑上有 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
没有 usb-storage grep,所以即使我连接了 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 硬盘甚至没有 usb-storage grep。
同样,当闪存驱动器之一已连接并且我重新启动时,图标没有显示,因为 dmesg 尾巴是关于我的 wifi 正在连接和网络管理器。
任何改进此脚本的想法,因为它不可靠。
我在 linuxquestions.org 上找到了解决方案。
解决方案-
#!/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"
我正在使用 DWM(平铺 Window 管理器),dwmblocks
用于我笔记本电脑上的状态栏。我在这台笔记本电脑上有 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
没有 usb-storage grep,所以即使我连接了 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 硬盘甚至没有 usb-storage grep。
同样,当闪存驱动器之一已连接并且我重新启动时,图标没有显示,因为 dmesg 尾巴是关于我的 wifi 正在连接和网络管理器。
任何改进此脚本的想法,因为它不可靠。
我在 linuxquestions.org 上找到了解决方案。
解决方案-
#!/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"