Tuesday, September 24, 2013

This program will auto mount USB device and count number when systme power on.

#/bin/bash
# Program:
#       This program will auto mount USB device and count number when systme power on.
# History:
# 2013/09/24 Mace First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#test file path
file_path="/tmp"

# Open an file to save fdisk information.
touch /mnt/information

# Store fdisk -l info to file information.
fdisk -l | grep "FAT32" > /mnt/information

# cut dev path to dev_path.
dev_path="`head /mnt/information | tr -s ' ' | cut -f1 -d' '` "

# Judge USB device is on plug.
if [ "$dev_path" == " " ];then
  echo "Can't found USB device." 
  exit 0
else
  echo "Found USB device at: "$dev_path
fi

# mount USB to /tmp folder.
mount -t vfat $dev_path $file_path

if [ -f "$file_path"/test ];then
  count_num="`head $file_path/test`"
  echo "$count_num"
else
  echo "file not found. Create test file init number to 0."
  touch $file_path/test
fi

if [ "$count_num" == " " ];then
  count_num=0
  echo "test file is empty. init count_unm=0"
else
  count_num=$(($count_num + 1))
  #echo "$count_num"
  echo $count_num > $file_path/test
fi 

#umount USB device.
umount "$file_path"
 
exit 0

No comments: