What you want basically is a plain keylogger.
Your device does keyboard emulation, and on Linux the keyboard device is a file, so you can do standard input redirect.
Alternatively you can use a program like logkeys (may already be available as a package in your distro). Some more doc here too.
Assuming you only want to capture input from that specific device and not any keyboard installed on your system, you need to identify the device.
I would have a look at /dev/input/
for example then locate your device by path or by id.
Using logkeys the -d, --device=device
option should help you filter events on the right device.
There are also other useful tools like evtest, xinput that can help you with testing and locating the correct device identifier.
It is also possible to set up a udev rule for your device, that runs a program or does something when plugging in the device (an intro tutorial on udev). I am not experienced enough with udev but it should be possible to set up the redirection this way. The only hurdle is to make sure you get plain characters and not raw input, or you have to take care of decoding it.
Another option is using the python-evdev library if you are into Python, and you monitor events from /dev/input/whatever-your-keyboard-is-using.
I would try a udev rule. I would not bother with a temp text file either, just monitor events from the device and react to them.
Update
I have done some testing with a barcode scanner, which behaves as a keyboard and is therefore technically similar to your card reader from the system's point of view.
First of all, one interesting thing to know is that it's possible to gain exclusive access to an HID device using the EVIOCGRAB
function. That means you can have an application take over the device so that it will not send unwanted input and interfere with other windows. You will probably want this.
Note that evtest
(mentioned above) has a --grab
parameter too.
I have also previous mentioned python-evdev, that can be used to capture input from the device. It has a grab()
function as well.
It's not needed to be root to use python-evdev but your permissions may need adjusting.
From their doc:
If you do not see any devices, ensure that your user is in the correct
group (typically input) to have read/write access.
To add yourself to the group:
usermod -a -G input user
then log in again. (Once you've done this, evtest will work for the non-root user too).
Here is one example of code here: How to “route” barcode TTY input to Python? and another one that worked for me: exclusive-keyboard-evdev.py.
Right now I don't know if there is a way to do that elegantly using bash only. But Python is a viable approach, or C.