Bash script not parsing file with spaces in path

Hi everyone,

I'm trying to write my first ever shell script, the OS is Raspbian. 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, and I have no control over the Windows server at all so changing directory names is not an option unfortunately. Here is the script I have written to date:

#!/bin/sh

DB1="/mnt/qnap/Amarillo/Reckon/Point of Sale Lite 2013 Administrator/QBPOS.TXT"
TF1=/home/pi/scripts/file1

ls --full-time "$DB1"
ls --full-time "$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

Unfortunately, no matter what, the script ALWAYS equates to false. I have included the 'ls' commands in the script purely for debugging purposes so I can check the variable declaration is working correctly and also to verify dates on the files. So when I run the script, this is the output I receive:

pi@mckinnonPi ~/scripts $ ./fish 
-rwxr-xr-x 1 root root 936 2014-06-22 17:47:15.583175000 +1000 /mnt/qnap/Amarillo/Reckon/Point of Sale Lite 2013 Administrator/QBPOS.TXT
-rw-r--r-- 1 pi pi 0 2014-01-01 12:00:00.000000000 +1100 /home/pi/scripts/file1
Database has NOT been updated 

So clearly the declaration for DB1 is working, as the ls command in the script succeeds, and the output also clearly shows that DB1 is indeed the newer of the 2 files, but for some reason the file age test fails. If I change the script to perform the test based on files that do NOT contain spaces in the path then it works as expected, so there is no error in the logic of the code, but clearly the spaces in the pathname are causing a problem.

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 re-wrote the script to point to files on the local server only, taking the CIFS share out of the equation, and IT WORKS! So it would appear that the problem is caused by the fact the database resides on a CIFS share, but why on earth would that be the case when the ls output clearly shows the file is accessible? Here is the mount information just incase it helps to shed some light on the problem:

//192.168.0.4/backup on /mnt/qnap type cifs (rw,relatime,vers=1.0,sec=ntlmssp,cache=strict,username=guest,domain=WORKGROUP,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.0.4,file_mode=0755,dir_mode=0755,nounix,serverino,rsize=61440,wsize=65536,actimeo=1)

The script using #!/bin/sh whose test builtin probably does not know -nt (I presume /bin/sh points to dash ). Try using #!/bin/bash instead..

1 Like

You sir, are a GENIUS!!!!!

Those 2 small letter made all the difference, and it now works. I can not thank you enough.

I still can not understand why on earth the script would work on local files, yet fail when referencing a file located on a CIFS share? Anyway, IT WORKS!

Many thanks Scrutinizer!