I think i'd use mount
to textually infer the connected usb's dir using sed
or awk
.
Maybe even save mount
's result when nothing is connected and 'subtract' it from mount
's result after connecting a new usb device.
Or even better, run your script before you connect the device:
- the script will run mount
every second and will wait for a change in the result.
- when a change is detected, the newly added device is your usb.
Something like:
#!/bin/bash
mount_old="$(mount)"
mount_new="${mount_old}"
while [[ "${mount_new}" == "${mount_old}" ]]; do
sleep 1
mount_new="$(mount)"
done
# getting added line using sort & uniq
sort <(echo "${mount_old}") <(echo "${mount_new}") | uniq -u | awk '{ print $3 }'
# another way to achieve this using diff & grep
# diff <(echo "${mount_old}") <(echo "${mount_new}") | grep ">" | awk '{ print $4 }'
It's merely a sketch, you might need/want to refine it.