Converting and Moving a file using a simple bash script.
・1 min read
I recently needed to receive .txt files from a laboratory analyser, convert it to PDF and then transfer it to a local SMB share - but - the analyser results text file has to be over 30 minutes old before the whole process begins. Once the file has been converted, it needs to be deleted from the analyser server sl that it doesn’t fill up the local hard drive. The analyser server in this instance was a Raspberry Pi 3 B+ with a LAN connection. The reason for the 30 minute delay is due to the analyser exporting the results of a sample line-by-line and a full sample analysis can take up to 30 minutes. So if we transferred the file in less than 30 minutes then it may not be the complete results.
#!/bin/bash
## Danny McClelland 2020
shopt -s nullglob
for f in /home/pi/results/eclipse/*.txt; do
FILENAME=$f
PDFNAME=$(basename $f .txt)
# If file is over 30 minutes old then create PDF and remove TXT
if test `find $FILENAME -mmin +30`
then
# Create PDF from TXT
/usr/bin/enscript $f -B -o - | /usr/bin/ps2pdf - $PDFNAME.pdf
# Remove original TXT file
rm $f
# Copy file to SMB share
/usr/bin/smbclient //192.168.50.100/virtualdrive -U DOMAIN/username%password -D LAB -c "put $PDFNAME.pdf"
# Remove PDF file from local directory
rm $PDFNAME
else
echo $f too new - skipping...
fi
done