I am trying to write my first bash script. It checks if a USB-to-Seria bridge and an USB-Storage device is connected and then starts writing the incoming data on the /dev/ttyUSB0 into a file on the attached storage device.
The problem is this part of my code:
{
cat -v $SERIAL > $USBDRIVE/first_test.txt
} || {
exit 0
}
It works as long the storage device and serial converter is connected. Cat writes the file continuously.
When I disconnect one of the devices an error occurs and my script exits. But the already written content in the file is also deleted.
I want to exit the script, close the written file and not lose the content.