[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1238
  • Last Modified:

Setting Date/Time Variable In A File Name

Hello,

I am trying to attach a time to the file that is being gzip'ed, I am just not sure how to. I have tried to explore using the date command and then try and only display the current time but I haven't been able to get this right.

My thoughts are to pull the date variable, only display the time, set this as a new variable, and then attach it after $thedate.

Any suggestions?

Thanks,
Ryan
# This script will ask for the variance amount in dollars. Then it will pass
# the amount to the awk script as a command line variable using the -v option
 
variance=100
thedate=`date|cut -c5-7,9-10,25-28`
clear
cd /db2/ye2008
cp /db2/spl01/ascii/VAR05 /db2/ye2008
mv VAR05 iri05$thedate
echo "Creating LE Variance....."
awk -f irivar.awk -v init_variance=$variance iri05$thedate > le.iri
echo "Printing LE Variance....."
#cat le.iri > /dev/ttyc02
#cat le.iri > /dev/ttya24
#cat le.iri > /dev/ttya24
#awk -f iribycls.awk iri05$thedate > le.class
#cat le.class > /dev/ttya24
#cat le.class > /dev/ttyc02
gzip -f iri05$thedate
echo "Done"

Open in new window

0
Ryan Rood
Asked:
Ryan Rood
  • 12
  • 11
  • 2
  • +1
1 Solution
 
woolmilkporcCommented:
You can easily format date ouput, please have a look at 'man date'
e.g. for 071108 it's date +"%d%m%y"
To have it in a variable, simply do
MYDATE=$(date +"+"%d%m%y")
them work with $MYDATE as usual.
 
0
 
woolmilkporcCommented:
sorry, a typo,
MYDATE=$(date +"%d%m%y")
 
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi coppsbuildall,

Your approach will certainly work.  (That's one of the better features of shell scripting -- a lot of things work.)

You'll probably find that the script is more compact if you'll let the *date* command return the format that you want.

date +%Y%m%d         returns  20081107

date +%y%m%d         returns 081107

Run *man date* to see all of the available formats in your distribution.


Kent
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ryan RoodAuthor Commented:
ok so it is definately doable, but I just want to pass the time in this instance, or create a proper variable that passed both the date and time, in MMDDYY-HHMMSS.
0
 
omarfaridCommented:
if you are going to use command like zip then

file=file`date +%d%m%y`
zip $file file1 file2 file3

if you want to use gzip then

date=`date +%d%m%y`
gzip file
mv file.gz file.gz.$date
0
 
woolmilkporcCommented:
So it's
thedate=$(date +"%b%d%y-%H%M%S")
0
 
Ryan RoodAuthor Commented:
updated the variable, this is what I got.

# ./leruniri.sh
./leruniri.sh: syntax error at line 5: `thedate=$' unexpected

attached most recent code
# This script will ask for the variance amount in dollars. Then it will pass
# the amount to the awk script as a command line variable using the -v option
 
variance=100
thedate=$(date +"%b%d%y-%H%M%S")
 
clear
cd /db2/ye2008
cp /db2/spl01/ascii/VAR05 /db2/ye2008
mv VAR05 iri05$thedate
echo "Creating LE Variance....."
awk -f irivar.awk -v init_variance=$variance iri05$thedate > le.iri
echo "Printing LE Variance....."
#cat le.iri > /dev/ttyc02
#cat le.iri > /dev/ttya24
#cat le.iri > /dev/ttya24
#awk -f iribycls.awk iri05$thedate > le.class
#cat le.class > /dev/ttya24
#cat le.class > /dev/ttyc02
gzip -f iri05$thedate
echo "Done"

Open in new window

0
 
omarfaridCommented:
if you want to include the HHMMSS then change

file=file`date +%d%m%y-%H%M%S`
date=`date +%d%m%y-%H%M%S`
0
 
woolmilkporcCommented:
OK, %b gives  'Nov', I misread you, please use %m instead.
And use `` instead of $( ), if your shell doesn't support $()
0
 
Ryan RoodAuthor Commented:
ok, consider for a second that I know nothing about unix scripting ...

Creating LE Variance.....
Printing LE Variance.....
iri05071108-110634: No such file or directory
Done

# This script will ask for the variance amount in dollars. Then it will pass
# the amount to the awk script as a command line variable using the -v option
 
variance=100
file=file`date +%d%m%y-%H%M%S`
date=`date +%d%m%y-%H%M%S`
 
clear
cd /db2/ye2008
cp /db2/spl01/ascii/VAR05 /db2/ye2008
mv VAR05 iri05$thedate
echo "Creating LE Variance....."
awk -f irivar.awk -v init_variance=$variance iri05$thedate > le.iri
echo "Printing LE Variance....."
#cat le.iri > /dev/ttyc02
#cat le.iri > /dev/ttya24
#cat le.iri > /dev/ttya24
#awk -f iribycls.awk iri05$thedate > le.class
#cat le.class > /dev/ttya24
#cat le.class > /dev/ttyc02
gzip -f iri05$date
echo "Done"

Open in new window

0
 
woolmilkporcCommented:
if you want to have le.irimmddyy-hhmmss
you should use  > le.iri$thedate
 
I don't understand what iri05$thedate should do.
 
0
 
Ryan RoodAuthor Commented:
le.iri is what is being generated when it goes through awk ... iri05 is the original file it is pulling from. both need to print and be archived.
0
 
woolmilkporcCommented:
So it's for your line 21
gzip le.iri
mv le.iri le.iri$thedate
 
The awk thing is not clear to me ...
 
0
 
Ryan RoodAuthor Commented:
# ./leruniri.sh

mv: target (+"%m%d%y.%H%M") must be a directory
Usage: mv [-fi1] source_file target_file
       mv [-fi] source_file... target_file
Creating LE Variance.....
awk: can't open file iri05date: No such file or directory (error 2)
 source line 21 of file irivar.awk
Printing LE Variance.....
awk: can't open file iri05date: No such file or directory (error 2)
 source line 18 of file iribycls.awk
gzip: unrecognized option `+"%m%d%y.%H%M"'
gzip 1.1.1 (1 Jun 93)
usage: gzip [-cdfhLrtvV19] [-S suffix] [file ...]
 -c --stdout      write on standard output, keep original files unchanged
 -d --decompress  decompress
 -f --force       force overwrite of output file and compress links
 -h --help        give this help
 -L --license     display software license
 -q --quiet       suppress all warnings
 -r --recurse     recurse through directories
 -S --suffix .z   use suffix .z instead of .gz
 -t --test        test compressed file integrity
 -v --verbose     verbose mode
 -V --version     display version number
 -1 --fast        compress faster
 -9 --best        compress better
 file...          files to (de)compress. If none given, use standard input.
Done
#

# This script will ask for the variance amount in dollars. Then it will pass
# the amount to the awk script as a command line variable using the -v option
 
variance=100
thedate='date +"%m%d%y.%H%M"'
 
clear
cd /db2/ye2008
cp /db2/spl01/ascii/VAR05 /db2/ye2008
mv VAR05 iri05$thedate
echo "Creating LE Variance....."
awk -f irivar.awk -v init_variance=$variance iri05$thedate > le.iri
echo "Printing LE Variance....."
#cat le.iri > /dev/ttyc02
#cat le.iri > /dev/ttya24
awk -f iribycls.awk iri05$thedate > le.class
#cat le.class > /dev/ttya24
#cat le.class > /dev/ttyc02
gzip -f iri05$thedate
echo "Done"

Open in new window

0
 
woolmilkporcCommented:
I just see you mixed date and thedate.
Please use only one of them, preferrably 'thedate'
0
 
Ryan RoodAuthor Commented:
ok ... so it looks like we cant call the date in the middle of awk ... we would have to do it after the fact ...
0
 
Ryan RoodAuthor Commented:
nope, changed the var and it is still erroring out on the awk ... and gzip. probably have to remove and add the date var in the mv after the gzip is created.
0
 
woolmilkporcCommented:
The 'mv' in your script (mv VAR05 iri05$thedate) goes wrong because VAR05 is empty. Where should it come from?
 
And, right, do the mv after the gzip, as I wrote in 22905805
0
 
Ryan RoodAuthor Commented:
almost there, just missing something in the mv command.

Creating LE Variance.....
Printing LE Variance.....
mv: target (+"%m%d%y.%H%M".gz) must be a directory
Usage: mv [-fi1] source_file target_file
       mv [-fi] source_file... target_file
Done
#

# This script will ask for the variance amount in dollars. Then it will pass
# the amount to the awk script as a command line variable using the -v option
 
variance=100
thedate='date +"%m%d%y.%H%M"'
 
clear
cd /db2/ye2008
cp /db2/spl01/ascii/VAR05 /db2/ye2008
mv VAR05 iri05
echo "Creating LE Variance....."
awk -f irivar.awk -v init_variance=$variance iri05 > le.iri
echo "Printing LE Variance....."
#cat le.iri > /dev/ttyc02
#cat le.iri > /dev/ttya24
awk -f iribycls.awk iri05 > le.class
#cat le.class > /dev/ttya24
#cat le.class > /dev/ttyc02
gzip -f iri05
mv iri05.gz iri05$thedate.gz
echo "Done"

Open in new window

0
 
Ryan RoodAuthor Commented:
VAR05 is a file that is created from our POS ... contains inventory numbers.
0
 
Ryan RoodAuthor Commented:
it looks like the mv is seeing the date variable as real data and not calculating it out before applying the move.
0
 
woolmilkporcCommented:
Two things>
In thedate=... please use `  ` (backticks) around the date +....'thing, not ' ' (apostrophe)
 
use
mv iri05.gz iri05${thedate}.gz
to clearly separate your variable from the .gz
0
 
Ryan RoodAuthor Commented:
awesome ... ok, one more question.

mv iri05.gz iri05(insert some character here like a - to seperate the name from the date)${thedate}.gz
0
 
Ryan RoodAuthor Commented:
got it ... thanks for the help on this! :)
0
 
woolmilkporcCommented:
It's simple
mv iri05.gz  iri05-${thedate}.gz
or any other character than '-' except  things like $ or * (special treatment necessary)
You can also have extra characters in your date/time string, e.g.
thedate=`date +"%m-%d.%H:%M"`
will give 11-07.14:42
0
 
woolmilkporcCommented:
You're welcome!
Have fun and success and a nice weekend!
 
Norbert (wmp)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 12
  • 11
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now