File globbing order

Hi ,
I'm facing a different behaviour with one of my shell script for last few days. It was working good before that.
here is my code for the script FileRemove.sh

#get the file name#
file1=$1
file2=$2
rm $file1 # delete the old file
mv $file2 <target path> #move the new file to the target path

execution syntax for FileRemove.sh in my program: sh FileRemove.sh XYZ*.txt

For all my past days first argument passed as my old file which was created one or few days before in the path and second argument passed as a new file created in the path.

For example if I have 2 files as below

XYX20181225.txt
XYZ20181226.txt

then $1 passed as XYX20181225.txt and $2 passed as XYZ20181226.txt for the input argument XYZ*.txt.

But nowadays $2 getting passed as old file and $1 getting passed as new file. Because of this I'm deleting my new file in script instead of old file.

I know this is not the good way of scripting to delete the old file but can anyone advise why it worked all these months fine and suddenly for last one week it is creating issue for me.

The standards specify that a shell should replace each pathname matching pattern with a list of pathnames that match that pattern printed in collation sequence order.

When in that directory, what output do you get from the commands:

printf '"%s"\n' XYZ*.txt
printf '"%s"\n' XYZ*.txt | od -bc

and please tell us what shell and operating system you're using.

One would expect the type of problem you're experiencing if some non-printing characters were being matched by the asterisk in one or more of your filenames or if the dates shown in your example were in MMDDYYYY format instead of in YYYYMMDD format (as in 01012019 comes before 12252018 ; but 20181225 comes before 20190101 as you want it to).

Hi, the order is in collating order (the first-to-last order of characters in the code set).

Shell Command Language

Note that XYZ*.txt will only match one of the two files that you mention, since the other one starts with XYX

2 Likes

Hi Scrutinizer,
Good catch. I didn't notice the leading XYX in one of the filenames!

Cheers,
Don

1 Like

First: what should the script do if the glob you use does not evaluate to exactly two files?

But supposing it does, if you want to sort according to time and not according to name then use the -tr (sort for time, reversed order) of ls and sort your arguments according to this, regardless of which position they arrive at. Notice that it does pay to build at least a modicum of error-checking into your code (i know, you haven't asked about that, but it still is a good idea.):

#! /bin/ksh

file1=$(ls -tr $* | head -n 1)
file2=$(ls -tr $* | tail -n 1)

if ! rm $file1 ; then
     print -u2 - "Error deleting $file1"
     exit 1
fi
if ! mv $file2 <target path> ; then
     print -u2 - "Error moving $file2"
     exit 2
fi
exit 0

Just in case you don't use Korn shell: you should have said which shell you use. The translation of this to any other shell is left as an exercise to the reader.

I hope this helps.

bakunin

1 Like

Thank you all. since I have the file in mm_dd_yyyy format last year december month number 12 is greater than this year january month 01. Thanks for the information about collation sequence order.

1 Like