一个mount U盘的脚本
冷胜魁(Seaquester)
lengshengkui@gmail.com
2007-12-12
由于工作需要,经常在redhat 9下使用U盘,redhat 9下U盘无法自动mount,所以,写了一个脚本来简化操作。
通常情况下,U盘设备应该是 /dev/sda1, 但是,有时候先后使用两个不同的U盘时,后面插入的一个是 /dev/sdb1,所以,使用了 fdisk -l | grep sd[b-z]1 | awk '{print $1}' 来取得它的设备名。代码只能mount 一个U盘,因为我基本上不会同时插入两个U盘,有这一需求的朋友可以自己修改。
#! /bin/sh
#
# Mount/Unmount a USB disk.
#
# Mount:
# We try to mount /dev/sda1 first, if failed, search /dev/sdb1, /dev/sdc1 ...,
# and try to mount the 1st one we found.
#
MOUNT_DIR=/mnt/usb
MOUNT_DEV=/dev/sda1
mount | grep $MOUNT_DIR
if [ "$?" = "0" ]
then
# Kill all process locked the USB disk
fuser -m -v -k -i $MOUNT_DIR
umount $MOUNT_DIR
if [ "$?" = "0" ]
then
echo USB disk at $MOUNT_DIR has been unmounted
else
echo Can not unmount $MOUNT_DIR
fi
else
mount -t vfat /dev/sda1 $MOUNT_DIR -o codepage=936,iocharset=cp936 2>/dev/null
if [ "$?" = "0" ]
then
echo USB disk $MOUNT_DEV has been mounted at $MOUNT_DIR
else
#
# Search all USB disk devices, and mount the first one
#
for MOUNT_DEV in $(fdisk -l | grep sd[b-z]1 | awk '{print $1}')
do
if [ "$?" = "0" ]
then
mount -t vfat $MOUNT_DEV $MOUNT_DIR -o codepage=936,iocharset=cp936 2>/dev/null
if [ "$?" = "0" ]
then
echo USB disk $MOUNT_DEV has been mounted at $MOUNT_DIR
exit 0
fi
fi
done
echo Can not mount $MOUNT_DEV to $MOUNT_DIR
fi
fi
exit 0
注意:如果U盘是在Windows下格式化的,则上面可能没有分区,所以,U盘设备是/dev/sda (而不是/dev/sda1),则上面的脚本不合适,需要修改。
阅读(1617) | 评论(2) | 转发(0) |