read file name from text file in ksh and compress
I want to write a KornShell (ksh) script to read filename from a textfile(having list of prefixes) and compress it if it is found in the directory by looping.
ex: I will keep the prefix 'abcd' in the text file .
By reading it, I want to compress the files in the directory matching the name like this abcd###.YYYYMMDDXXXXXX.txt
I do not want to do anything with same prefix but with different extensions or different patterns like abcd###.YYYYMMDDXXXXXX.dat or abcd###.YYYYMMDDXXXXXX.txt.Z. I only want to compress matching like this abcd###.YYYYMMDDXXXXXX.txt only.
How to implement this in ksh?
Superficially, this should do:
: ${COMPRESS:=xz}
while read prefix
do
$COMPRESS ${prefix}[0-9][0-9][0-9].[12][09][0-9][0-9][01][0-9][0-3][0-9]??????.txt
done < file
Obviously, I'm having to make some guesses, that #
means a digit, that the YYYYMMDD
is a date, and that X
is any character (that's ?
in the answer). If X
is meant to be any upper-case alphabetic, or something else, adjust accordingly. The year rules will accept 19xx and 20xx (also 10xx and 29xx, but you're unlikely to have files dated like that; the month rules accept 00..19; the day rules accept 00..39. If you have to validate more, then you can't readily use a simple globbing regex.
I used xz
as the compress program. I would not use the compress
program for compression as it simply doesn't compare with gzip
, let alone bzip2
or xz
, etc.
上一篇: [和[[在Bash中有什么区别?