Bash script not parsing file with spaces in path

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script not parsing file with spaces in path
# 1  
Old 06-22-2014
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:


DB1="/mnt/qnap/Amarillo/Reckon/Point of Sale Lite 2013 Administrator/QBPOS.TXT"

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
	echo "Database has NOT been updated"

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:

// 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=,file_mode=0755,dir_mode=0755,nounix,serverino,rsize=61440,wsize=65536,actimeo=1)

Last edited by gjws; 06-22-2014 at 04:31 PM..
# 2  
Old 06-22-2014
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..

Last edited by Scrutinizer; 06-22-2014 at 04:42 PM.. Reason: Incorrect
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-22-2014
Originally Posted by Scrutinizer
The script using #!/bin/sh whose test builtin does not know -nt . Try using #!/bin/bash instead..
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

2. UNIX for Advanced & Expert Users

Parsing a file in bash

Hello All, I have the following input file that i'm trying to parse: hadoop 15526 15524 0 hadoop 15528 15526 0 hadoop 19747 4018 1 hadoop 2862 2861 0 hadoop 2864 2862 0 hadoop 12177 14376 1 I'm trying to get this in my output file: 15526 15528... (2 Replies)
Discussion started by: ramky79
2 Replies

3. UNIX for Dummies Questions & Answers

Getting path name using bash script

I have this code to extract the directory name ($0:h) in tcsh. I am converting the code to bash and need a way to get the equivalent setting for DefRaytracPath set DefRaytracPath = `echo $0:h | awk 'BEGIN {FS="/tcsh"} {print $1"/prog"}'` (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

5. Shell Programming and Scripting

Looking for help with parsing file contents in bash [newbie]

Hi I'm just messing around with bash and trying to learn it because I have a course next semester dealing with OS design where we need to know how to use SSH client and either bash or ksh. I've never done shell scripting before. I just started today and I was wondering how parsing files... (1 Reply)
Discussion started by: mehungry
1 Replies

6. Shell Programming and Scripting

parsing a config file using bash

Hi , I have a config _file that has 3 columns (Id Name Value ) with many rows . In my bash script i want to be able to parse the file and do a mapping of any Id value so if i have Id of say brand1 then i can use the name (server5X) and Value (CCCC) and so on ... Id Name ... (2 Replies)
Discussion started by: nano2
2 Replies

7. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

8. Shell Programming and Scripting

Importing a path/file into a bash script

Hey, I'm new here. Basically, I'm trying to make a bash script that affects a file of my choice. What I want to do is $./ /path/to/file.jpg and then the bash script will know that variable=/path/to/file.jpg Thanks! (4 Replies)
Discussion started by: TFB
4 Replies

9. Shell Programming and Scripting

Bash Script to display directories in Path?

Hello there, As a newbie: The directories in PATH can be hard to distinguish when printed out as one line with colon .Please, can i have a sample script to display them,one to a line. Thank you. (1 Reply)
Discussion started by: debut
1 Replies

10. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question

Featured Tech Videos