Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2343603
  • 博文数量: 141
  • 博客积分: 3552
  • 博客等级: 中校
  • 技术积分: 4148
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-15 14:39
个人简介

熟悉Linux下程序设计及各种应用程序 熟悉C Language 熟悉Glusterfs、FFmpeg、CDN 系统设计,计算机图形系统设计、分布式程序设计 目前主要研究方向:流媒体

文章分类

全部博文(141)

分类:

2010-10-23 17:11:50

#!/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


阅读(7893) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~