![用于弹出未加密的 USB 驱动器的 Bash 脚本](https://linux22.com/image/1563698/%E7%94%A8%E4%BA%8E%E5%BC%B9%E5%87%BA%E6%9C%AA%E5%8A%A0%E5%AF%86%E7%9A%84%20USB%20%E9%A9%B1%E5%8A%A8%E5%99%A8%E7%9A%84%20Bash%20%E8%84%9A%E6%9C%AC.png)
我需要强制弹出任何未加密的 USB 驱动器。我已经有一个可以弹出任何 USB 驱动器的脚本,但我需要修改它以在弹出之前检查磁盘的加密状态。
下面是我的脚本,它可以自动弹出任何 USB 驱动器:
for disk in $(diskutil list | awk '/disk[1-9]s/{ print $NF }' | grep -v /dev); do
if [[ $(diskutil info $disk | awk '/Protocol/{ print $2 }') == "USB" ]]; then
echo "Device $disk is a USB removable disk"
diskName=$(diskutil info $disk | awk -F"/" '/Mount Point/{ print $NF }')'
diskutil unmountDisk $diskName
fi
done
到目前为止,以下命令将显示加密状态:
diskutil apfs list
但我在解析输出以找到要弹出的未加密磁盘时遇到了麻烦。救命!
答案1
以下是可以运行的脚本:
#!/bin/bash
osascript -e 'display notification "Checking USB" with title "USB Notification"'
for disk in $(diskutil list | awk '/disk[1-9]s/{ print $NF }' | grep -v /dev); do
if [[ $(diskutil info $disk | awk '/Protocol/{ print $2 }') == "USB" ]]; then
echo "Device $disk is a USB removable disk"
diskName=$(diskutil info $disk | awk -F"/" '/Mount Point/{ print $NF }')
if [[ $(diskutil apfs list ${disk:0:5} | awk '/FileVault/{ print $2 }') == "No" ]]; then
osascript -e 'display notification "Detected a USB removable disk that is not encrypted, ejecting" with title "USB Notification"'
echo "Unmounting ${disk:0:5}"
diskutil unmountDisk ${disk:0:5}
fi
fi
done