我需要强制弹出任何未加密的 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