#!/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
|
本博文转载于:认真学习 的博客
阅读(1246) | 评论(0) | 转发(0) |