File globbing order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File globbing order
# 1  
Old 01-11-2019
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 01-11-2019
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 01-11-2019
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:
# 4  
Old 01-11-2019
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:
# 5  
Old 01-11-2019
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; 01-12-2019 at 07:21 AM.. Reason: corrected an error, thanks to RudiC for the catch
This User Gave Thanks to bakunin For This Post:
# 6  
Old 01-13-2019
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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulation row order in file

Hello, I am trying to replace the position of each row by the next row. OS: Ubuntu 18.04, bionic I'd appreciate your help. input_file: -O fileA wget http://x.y.z./a -O fileB wget http://a.b.c./d -O fileC wget http://q.f.s/t .. .. .. -O fileZZ wget http://r.t.y/u I expect: (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Negation in Bash Globbing

$ ls -1 a.1 b.1 x_a.1 x_b.1 $ ls -1 * b.1 x_a.1 x_b.1 $ ls -1 ** a.1 b.1 x_a.1 x_b.1The last result is not as expected. Why? Thanks. (2 Replies)
Discussion started by: carloszhang
2 Replies

3. UNIX for Dummies Questions & Answers

File globbing questions

hi guys, jus some file globbing questions sed "s/^.*on//" what does the full stop and asterisk means? i onli know that ^ means inverse or not (1 Reply)
Discussion started by: ment0smintz
1 Replies

4. Shell Programming and Scripting

Globbing or not globbing

Hi guys, Here is a simple script. It writes the current time to specific files in a directory. The arguments are the names of the files to write the date to (without path nor extension). root:~# cat /usr/local/bin/dummy.sh #!/bin/sh -e for file in $@; do date >> /var/lib/$file.dat... (11 Replies)
Discussion started by: chebarbudo
11 Replies

5. UNIX for Advanced & Expert Users

Write to a file in a specified order

This is my input file : FCBAFE572C5E4BA29B3F8030BD480A94,907 ,1/1/2003,12/31/2005,ACT,1,2010-05-07 04:42:26.380000 2345AD5D2BFB29C821C1BC3DE8B746A7,907 ,1/1/2004,1/31/2005,ACT,1,2010-05-07 04:42:26.380000 E45B7371EEC0D1AB00E1750B5BC661F7,907 ,1/1/2004,12/31/2006,ACT,1,2010-05-07... (14 Replies)
Discussion started by: gpsridhar
14 Replies

6. Shell Programming and Scripting

Order file by lines

My script(3 arguments $1 = folder,$2 extension,$3 string) should do the following things: -Enter in the folder of $1(if exists). -Put ls *.$2 > temp.txt ( I use a temp file to store the result of ls command and if $2 = txt in this file I'll have all the .txt files of the folder) -Now I want to... (2 Replies)
Discussion started by: Max89
2 Replies

7. Shell Programming and Scripting

creating a file in reverse order of another file

Hi, I have a requirement where i need to write a script to create the new file from the given input file with the data in reverse order (bottom to top) Sample data: Input File-------------- Java VB Oracle Teradata Informatica Output file:----------------- Informatica Teradata Oracle... (3 Replies)
Discussion started by: srilaxmi
3 Replies

8. Shell Programming and Scripting

globbing, $# is too high after wildcard expansion in bash script

How can I pass in an argument such as "*.k" to a bash script without having to double-quote *.k and not having *.k `glob` to match all files in the pattern? I tried using noglob in my script but this didn't work the way I thought it would.. expansion is still occuring, $# is higher than I... (3 Replies)
Discussion started by: zoo591
3 Replies

9. Shell Programming and Scripting

Globbing slash Wildcarding Question

I am on HP-UX and I am trying to come up with a method to call in a list of files named like so. filename020107.dat filename020207.dat filename020307.dat Obviously I can list them ls them like so, ls filename*.dat. In case you did not notice the number is a date and I was hoping to match... (4 Replies)
Discussion started by: scotbuff
4 Replies

10. Shell Programming and Scripting

awk / bash globbing question

I would like to process a list of files matching: GPS*\.xyz with an awk script. I would then like to output the files to GPS*\.xyz.out (e.g. the same file name appended with .out). Something like: awk '{if(NR==1) {offset=-$1}; $1=$1+offset; print }' GPS*.xyz this does exactly what I want EXCEPT... (3 Replies)
Discussion started by: franzke
3 Replies
Login or Register to Ask a Question