I'm trying to write my first ever shell script. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point. Here is the script I have written to date:
#!/bin/sh
DB1=/mnt/reckon/"Point of Sale Lite 2013 Administrator"/QBPOS.PDB
ls -la "$DB1"
if [ "$DB1" -nt "~/scripts/file1" ]; then
echo "Database has updated since last run"
echo "Do some stuff here"
touch file1
else
echo "Database has not been updated"
fi
Unfortunately, no matter what, the script ALWAYS equates to false. This is an ls of DB1 at the beginning of the script just for debugging so I can see if the DB1 declaration is successful.
This is an ls for my file1:
pi@mckinnonPi ~/scripts $ ls -l file1
-rw-r--r-- 1 pi pi 0 Jun 20 10:27 file1
This is an ls for the database:
pi@mckinnonPi ~/scripts $ ls -l /mnt/reckon/"Point of Sale Lite 2013 Administrator"/QBPOS.PDB
-rwxr-xr-x 1 root root 2048000 Jun 22 14:30 /mnt/reckon/Point of Sale Lite 2013 Administrator/QBPOS.PDB
As you can see the database file is certainly newer than file1, however if I now run the script this is what I get:
pi@mckinnonPi ~/scripts $ ./fileage
-rwxr-xr-x 1 root root 2048000 Jun 22 14:36 /mnt/reckon/Point of Sale Lite 2013 Administrator/QBPOS.PDB
Database has not been updated
pi@mckinnonPi ~/scripts $
So clearly the declaration for DB1 is working, as the ls command in the script succeeds, but for some reason the file age test fails. I've been working on this for a few days now, and I've researched as much as I can but have hit a brick wall. Any help would be very much appreciated.
Thank you!
- Update
I've re-written the script in order to simplify as much as possible. When using paths that do NOT contain a space everything works exactly as expected so when I run this script:
#!/bin/sh
# DB1=/mnt/qnap/Amarillo/Reckon/"Point of Sale Lite 2013 Administrator"/QBPOS.TXT
DB1=/tmp/test/file2
TF1=/home/pi/scripts/file1
ls -la "$DB1"
ls -la "$TF1"
if [ "$DB1" -nt "$TF1" ]; then
echo "Database has been updated since last run"
echo "Do some stuff here"
touch /home/pi/scripts/file1
else
echo "Database has NOT been updated"
fi
The conditional statement works exactly as I would expect. Unfortunately, when I change the test to use the path including spaces it fails again. This is so frustrating! I've also tried using a symbolic link but the same problem occurs.
OK, so I'm new to stack overflow, so I don't understand how this has been marked as duplicate and answered? As I have explained in my update above, removing all reference to the tilde makes NO DIFFERENCE, this problem has nothing to do with the tilde expansion, as you can see in the updated code above.
Please don't mark something as answered if you have not read and understood the question fully .