As of right now, I've got my udev setup to execute scripts when a USB flash drive is plugged in or removed, but I'm stuck trying to figure out if there is some way that I can execute a script to read a file from the most recent USB device plugged in.
到目前为止,我已经设置了udev设置来在插入或移除USB闪存驱动器时执行脚本,但是我试图找出是否有某种方法可以执行脚本来读取文件从插入的最新USB设备。
I am using usbmount to automatically mount all of my flash drives, and they are mounted according to this scheme:
我正在使用usbmount自动安装我的所有闪存驱动器,并根据这个方案安装它们:
/dev/sdb1 15G 8.0K 15G 1% /media/usb0 /dev/sdc1 15G 8.0K 15G 1% /media/usb1 /dev/sdd1 15G 8.0K 15G 1% /media/usb2 /dev/sde1 15G 8.0K 15G 1% /media/usb3
/ dev / sdb1 15G 8.0K 15G 1%/ media / usb0 / dev / sdc1 15G 8.0K 15G 1%/ media / usb1 / dev / sdd1 15G 8.0K 15G 1%/ media / usb2 / dev / sde1 15G 8.0K 15G 1%/ media / usb3
So for example, when I plug in USB flash drive #5, it gets automounted to /media/usb4, then I would like to say execute 'cat /media/usb4/data.txt > /tmp/output.txt' and only that drive that was just plugged in. Ideally I would like this to work no matter the number assigned to /media/usbx, so that if I replug in device 2, it would execute the script just for that device and not the rest.
例如,当我插入USB闪存驱动器#5时,它会自动挂载到/ media / usb4,然后我想说执行'cat /media/usb4/data.txt> /tmp/output.txt'并且仅那个刚刚插入的驱动器。理想情况下,无论分配给/ media / usbx的编号,我都希望这个工作正常工作,这样如果我重新插入设备2,它将只为该设备执行脚本,而不是其余部分。
Any ideas of how this can be done through bash scripting preferably but open to other ideas.
任何关于如何通过bash脚本完成此操作的想法最好但对其他想法开放。
Thank you for your time.
感谢您的时间。
------------- EDIT
I figured out a way although it's definitely not the prettiest or maybe even the most reliable:
我想出了一种方法,虽然它绝对不是最漂亮的,甚至可能是最可靠的:
$ sudo tail -n2 /var/log/syslog
Oct 4 14:40:58 development usbmount[32250]: executing command: mount -tvfat -osync,noexec,nodev,noatime,nodiratime /dev/sda1 /media/usb0
Oct 4 14:40:58 development usbmount[32250]: executing command: run-parts /etc/usbmount/mount.d
$
OK, so now to cut that down to just the media mount point,
好了,现在把它减少到媒体挂载点,
$ sudo tail -n2 /var/log/syslog |grep media | awk '{print $12}'
/media/usb0
$
With this assuming no other errors or anything filling the last two spots on the syslog, I can execute scripts using something like:
假设没有其他错误或任何填充syslog上最后两个位置的内容,我可以使用以下内容执行脚本:
#!/bin/bash
device=`sudo tail -n2 /var/log/syslog |grep media | awk '{print $12}'`
cat $device/data.txt > /tmp/output.txt
1 个解决方案
#1
1
The run-parts
bit is a tip-off...you can create a file /etc/usbmount/mount.d/50_copydata
run-parts位是一个提示...你可以创建一个文件/etc/usbmount/mount.d/50_copydata
Something like this:
像这样的东西:
#!/bin/bash
set -u
[[ -f "${UM_MOUNTPOINT}/data.txt" ]] && cat "${UM_MOUNTPOINT}/data.txt" > /tmp/output.txt
usbmount
will set $UM_MOUNTPOINT to i.e. /media/usb0. I use set -u
to make sure it only executes if $UM_MOUNTPOINT is set.
usbmount将$ UM_MOUNTPOINT设置为ie / media / usb0。我使用set -u确保它只在设置$ UM_MOUNTPOINT时才执行。
I assume you are going to filter the data - if you are only going to cat
the file you might as well use cp
.
我假设你要过滤数据 - 如果你只是要捕获文件,你也可以使用cp。
Remember to make the file executable:
记得让文件可执行:
chmod +x /etc/usbmount/mount.d/50_copydata
Unplug and re-plug your device(s) to test. Hope this helps!
拔下并重新插入您的设备进行测试。希望这可以帮助!
#1
1
The run-parts
bit is a tip-off...you can create a file /etc/usbmount/mount.d/50_copydata
run-parts位是一个提示...你可以创建一个文件/etc/usbmount/mount.d/50_copydata
Something like this:
像这样的东西:
#!/bin/bash
set -u
[[ -f "${UM_MOUNTPOINT}/data.txt" ]] && cat "${UM_MOUNTPOINT}/data.txt" > /tmp/output.txt
usbmount
will set $UM_MOUNTPOINT to i.e. /media/usb0. I use set -u
to make sure it only executes if $UM_MOUNTPOINT is set.
usbmount将$ UM_MOUNTPOINT设置为ie / media / usb0。我使用set -u确保它只在设置$ UM_MOUNTPOINT时才执行。
I assume you are going to filter the data - if you are only going to cat
the file you might as well use cp
.
我假设你要过滤数据 - 如果你只是要捕获文件,你也可以使用cp。
Remember to make the file executable:
记得让文件可执行:
chmod +x /etc/usbmount/mount.d/50_copydata
Unplug and re-plug your device(s) to test. Hope this helps!
拔下并重新插入您的设备进行测试。希望这可以帮助!