Hi,
I'm trying to get the name of a file that has the current date in the name.
The filename is on the form A<current date>01.DC, for example A2012110501.DC
I have this script so far
date +%y%m%d
D=$(date +%y%m%d)
N=A20$D
echo $N
N2={$N}01.DC
echo $N2
And gets the following output
When I try to add the last part "01.DC" to N something weird happens.
Any ideas on how to get the wanted result?
D=$(date +'%Y%m%d')
echo "A${D}01.DC"
A2012110501.DC
~/$ D=$(date +%Y%m%d)
~/$ echo $D
20121105
~/$ N=A${D}
~/$ echo $N
A20121105
~/$ N2=${N}01.dc
~/$ echo $N2
A2012110501.dc
When add both scripts in a file, test.ksh, I still get the same issue, i.e. get the following output 01.DC1105
However when I run them directly in the terminal I get the wanted result. Sadly I need to be able to use the script in a file (.ksh).
Any suggestions on how to make it work?
RudiC
November 5, 2012, 10:51am
5
Your N2 assignment N2={$N}01.DC
is incorrect, try using
N2=${N}01.DC
BTW, does the %Y
(uppercase!) format work with your date version:
/bin/date +%Y%m%d
20121105
You may want to try it in one go:
N="A$(date +%Y%m%d)01.DC"; echo $N
A2012110501.DC
rudic:
Your N2 assignment N2={$N}01.DC
is incorrect, try using
N2=${N}01.DC
BTW, does the %Y
(uppercase!) format work with your date version:
/bin/date +%Y%m%d
20121105
%Y works.
However using N2=${N}01.DC still gives me the following result: 01.DC1105
---------- Post updated at 04:58 PM ---------- Previous update was at 04:56 PM ----------
That worked fine.
Thank you.
RudiC
November 5, 2012, 11:01am
7
This seems like there's a <carriage return> character in N, as it prints A20121105, returns to char #1 , and prints 01.DC. Try echo $N|od -tx1
and publish the result here.
rudic:
This seems like there's a <carriage return> character in N, as it prints A20121105, returns to char #1 , and prints 01.DC. Try echo $N|od -tx1
and publish the result here.
That did not work. Just got a bunch of numbers as output.
RudiC
November 6, 2012, 3:13am
9
That's exactly what I'm out for! The numbers represent the characters in your string, ALL chars, including invisible control chars.
Using
echo $N|od -tx1
(if using code similar to the first one I posted) does not work. However using
echo $N2|od -tx1
gives the following output
0000000 41 32 30 32 30 31 32 31 31 30 36 0d 0d 30 31 2e
0000020 44 43 0d 0a
0000024
bmk
November 6, 2012, 3:57am
11
Try like..
ls -ltr |echo A`date '+%Y%m%d'`01.txt
RudiC
November 6, 2012, 4:03am
12
You've got (multiple!) <CR> chars in your string variable, wherever they may come from. You need to check the development of that variable (and its "parent" variables) from the first assignment to eliminate those. Then your printout will be OK.
Yes, I just realized that two. Seems that creating the scripts in Windows and then export them to the Unix environment is not the best idea.
Created the script in emacs and the <CR> chars disappeared.
Thanks for all the help.