File globbing order

Tags
file, globbing, order, script argument parameter, shell scripts

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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
Code:
#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.
# 2  
Old 1 Week Ago
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:
Code:
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).
# 3  
Old 1 Week Ago
Hi, the order is in collating order (the first-to-last order of characters in the code set).

Quote:
If the pattern matches any existing filenames or pathnames, the pattern shall be replaced with those filenames and pathnames, sorted according to the collating sequence in effect in the current locale. If the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames, the pattern string shall be left unchanged.
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
These 2 Users Gave Thanks to Scrutinizer For This Post:
Don Cragun (1 Week Ago) Neo (1 Week Ago)
# 4  
Old 1 Week Ago
Hi Scrutinizer,
Good catch. I didn't notice the leading XYX in one of the filenames!

Cheers,
Don
This User Gave Thanks to Don Cragun For This Post:
Neo (1 Week Ago)
# 5  
Old 1 Week Ago
Quote:
Originally Posted by poova
Code:
#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

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.
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.):

Code:
#! /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

Last edited by bakunin; 1 Week Ago at 07:21 AM.. Reason: corrected an error, thanks to RudiC for the catch
This User Gave Thanks to bakunin For This Post:
Neo (1 Week Ago)
# 6  
Old 1 Week Ago
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.
This User Gave Thanks to poova For This Post:
Neo (1 Week Ago)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Manipulation row order in file baris35 Shell Programming and Scripting 6 09-19-2018 10:32 AM
Order of data in Spool File Aparna.N Shell Programming and Scripting 4 09-07-2017 10:14 AM
Negation in Bash Globbing carloszhang Shell Programming and Scripting 2 11-10-2014 08:38 AM
File in ascending order by row giuliangiuseppe Shell Programming and Scripting 4 08-28-2014 09:55 AM
File globbing questions ment0smintz UNIX for Dummies Questions & Answers 1 12-20-2012 11:39 AM
Globbing or not globbing chebarbudo Shell Programming and Scripting 11 10-02-2012 07:59 AM
Write to a file in a specified order gpsridhar UNIX for Advanced & Expert Users 14 10-12-2010 05:34 PM
Order file by lines Max89 Shell Programming and Scripting 2 11-24-2009 12:37 PM
reversing order of lines in a file gfhgfnhhn Shell Programming and Scripting 26 07-30-2009 09:01 AM
Reversing file order using SED MBGPS Shell Programming and Scripting 3 01-07-2009 10:10 AM
creating a file in reverse order of another file srilaxmi Shell Programming and Scripting 3 11-28-2008 12:54 AM
globbing, $# is too high after wildcard expansion in bash script zoo591 Shell Programming and Scripting 3 07-25-2008 03:50 AM
Globbing slash Wildcarding Question scotbuff Shell Programming and Scripting 4 02-02-2007 12:10 AM
sort a file in reverse order frustrated1 Shell Programming and Scripting 11 09-21-2005 04:41 PM
awk / bash globbing question franzke Shell Programming and Scripting 3 07-15-2005 12:55 AM