#!/bin/bash
# Extract the Ethernet portion of the ifconfig command
ESTRING=$(ifconfig | grep -A1 eth0)

# Extract the last 4 digits of the Ethernet MAC address from the string
EXT=$(expr substr "$ESTRING" 51 2)$(expr substr "$ESTRING" 54 2)

a=0

# while :
# do
#        value=$(lsusb | wc -l)
#        if [ "$value" != "2" ]
#        then
 #           	if [[ "$a" -ne 1 ]]
 #               then
  #                      echo "USB detected"
#			echo "Waiting for 2 seconds for USB to be mounted automatically"
#			sleep 2
#			mount /dev/sda /run/media/sda1

			# Capture the ifconfig output and put it in a file named with the last 4 Ethenet MAC digits
			echo "$1ifconfig_$EXT.txt written to USB" 
			ifconfig > $1ifconfig_$EXT.txt
			
			sync
			echo "ifconfig_$EXT.txt written"
                        a=1
                # fi
#        else
		# if [[ "$a" -eq "1" ]]		
		# then
		# 	echo "USB removed"
	        #     	a=0
		# fi
		
        # fi
# done &

