Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610371
  • 博文数量: 152
  • 博客积分: 2684
  • 博客等级: 少校
  • 技术积分: 1126
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-29 11:03
文章分类
文章存档

2012年(6)

2011年(96)

2010年(50)

分类: Python/Ruby

2010-10-29 16:16:15

#!/bin/bash
# name: get_usb_disk.sh
# usb storage disk hotplug shell
# author LiuQi
# mail: lingjiujianke@gmail.com
# version: v0.1
#


declare -a UEVENT_STRING
declare -a USB_DISCONNECT_STRING
declare -a USB_DISK_STRING
TEMP_FILE_LINE_NUM=0
COUNTER=0
NUMBERS_OF_FILE_LINE=0
COUNTER_FILE_LINE_TEMP=0
LOG_DMESG_NAME="/tmp/dmesg.log"
LOG_WC_FILE_NAME="/tmp/wc.log"
LOG_UEVENT_MESSAGE_NAME="/tmp/uevent_message.log"
LOG_USB_STORAGE_NAME="/tmp/usb_storage_tmp.log"
VAR_USB_WITHOUTPARTATION=""

dmesg -c >/dev/null
rm -rf $LOG_DMESG_NAME
rm -rf $LOG_WC_FILE_NAME
rm -rf $LOG_UEVENT_MESSAGE_NAME
rm -rf $LOG_USB_STORAGE_NAME

while [ $COUNTER -lt 1 ]
do
    dmesg > $LOG_DMESG_NAME
    sleep 2
    TEMP_FILE_LINE_NUM=`wc -l /tmp/dmesg.log`
    echo "$TEMP_FILE_LINE_NUM" > $LOG_WC_FILE_NAME
    TEMP_FILE_LINE_NUM=`awk '{ print $1 }' /tmp/wc.log`
    NUMBERS_OF_FILE_LINE=$TEMP_FILE_LINE_NUM

    COUNTER_FILE_LINE_TEMP=0
#    echo "$LOG_DMESG_NAME"
    if [ -e $LOG_DMESG_NAME ]
    then
        while read UEVENT_STRING[$COUNTER_FILE_LINE_TEMP]
        do
#            echo "string [$COUNTER_FILE_LINE_TEMP] = ${UEVENT_STRING[$COUNTER_FILE_LINE_TEMP]}"
            echo "${UEVENT_STRING[$COUNTER_FILE_LINE_TEMP]}" | awk '{ print $3 $4 }' >> $LOG_UEVENT_MESSAGE_NAME
            echo "${UEVENT_STRING[$COUNTER_FILE_LINE_TEMP]}" | awk '{ print $3 }' >> $LOG_USB_STORAGE_NAME
            COUNTER_FILE_LINE_TEMP=`expr $COUNTER_FILE_LINE_TEMP + 1`
        done < $LOG_DMESG_NAME
    fi



    if [ -e $LOG_USB_STORAGE_NAME ]
    then
        COUNTER_FILE_LINE_TEMP=0
        while read USB_DISK_STRING[$COUNTER_FILE_LINE_TEMP]
        do
#            echo "new [$COUNTER_FILE_LINE_TEMP] = ${USB_DISK_STRING[$COUNTER_FILE_LINE_TEMP]}"
            if [ "${USB_DISK_STRING[$COUNTER_FILE_LINE_TEMP]}" == "[sdb]" ]
            then
#                echo "LIUQI=${USB_DiISK_STRING[$COUNTER_FILE_LINE_TEMP]}"
                VAR_USB_WITHOUTPARTATION="${USB_DISK_STRING[$COUNTER_FILE_LINE_TEMP]}"
                VAR_USB_WITHOUTPARTATION="${VAR_USB_WITHOUTPARTATION:1:3}"
                echo "have find a disk $VAR_USB_WITHOUTPARTATION"
                rm -rf $LOG_USB_STORAGE_NAME
                dmesg -c > $LOG_DMESG_NAME
                break
            fi
            COUNTER_FILE_LINE_TEMP=`expr $COUNTER_FILE_LINE_TEMP + 1`
        done < $LOG_USB_STORAGE_NAME
        rm -rf $LOG_USB_STORAGE_NAME
    fi



    if [ -e $LOG_UEVENT_MESSAGE_NAME ]
    then
        while read USB_DISCONNECT_STRING[$COUNTER_FILE_LINE_TEMP]
        do
#            echo "liuqi ${USB_DISCONNECT_STRING[$COUNTER_FILE_LINE_TEMP]}"
            if [ "${USB_DISCONNECT_STRING[$COUNTER_FILE_LINE_TEMP]}" == "USBdisconnect," ]
            then
                sleep 1
                dmesg -c > $LOG_DMESG_NAME
#                rm -rf $LOG_USB_STORAGE_NAME
                rm -rf $LOG_DMESG_NAME
#                rm -rf $LOG_WC_FILE_NAME
#                rm -rf $LOG_UEVENT_MESSAGE_NAME
#                exit
            fi
        done < $LOG_UEVENT_MESSAGE_NAME
        rm -rf $LOG_UEVENT_MESSAGE_NAME
    fi

    if [ $TEMP_FILE_LINE_NUM -gt 3 ]
    then
        sleep 0.1
#        break;
    fi

    rm -rf /tmp/dmesg.log
done

 
本博文转载于:认真学习 的博客
阅读(1781) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~