How to write automated umount operations in Shell?

One option for automating the umount operation is by writing a simple script. Here is an example script:

#!/bin/bash

# 检查是否有挂载的目录
if [[ $(mount | grep "^/dev/") ]]; then
    # 循环遍历挂载的目录并执行umount操作
    while read -r line; do
        mount_point=$(echo $line | awk '{print $3}')
        umount $mount_point
        echo "Unmounted $mount_point"
    done <<< $(mount | grep "^/dev/")
else
    echo "No mounted directories found"
fi

Save the above code as a .sh file, then execute the script in the Shell to automatically perform the umount operation. Please note that performing the umount operation requires root permissions, so you may need to use the sudo command when running the script.

Leave a Reply 0

Your email address will not be published. Required fields are marked *