Operating System - HP-UX
1833521 Members
2832 Online
110061 Solutions
New Discussion

Re: Very simplistic script not working

 
SOLVED
Go to solution
Derek Brown
Frequent Advisor

Very simplistic script not working

Hi, I've written a tiny little script and the output is not what I am expecting .... can anyone lend any advice please ?

#!/bin/ksh
#
dd=`date '+%d'`
mm=`date '+%m'`
yy=`date | awk '{print $6}'`
#
echo "The current day is $dd"
echo "The current month is $mm"
echo "The current day is $yy"
#
sapfn="SAP_PAYROLL_FILE_$yy$mm$dd_$mm$yy.CSV"
echo $sapfn



The output from this script is not evaluating $dd and the underscore (_) within $sapfn properly..... this is the output :

The current day is 03
The current month is 10
The current year is 2005
SAP_PAYROLL_FILE_200510102005.CSV

Any idea why the variable $dd and the _ is not being evaulated correctly within $sapfn ?
Many thanks



7 REPLIES 7
Muthukumar_5
Honored Contributor

Re: Very simplistic script not working

It is easy to do this simply as,

# sapfn="SAP_PAYROLL_FILE_$(date +'%Y%m%d_%m%Y').CSV"
# echo $sapfn
SAP_PAYROLL_FILE_20051003_102005.CSV

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Very simplistic script not working

Problem is,

sapfn="SAP_PAYROLL_FILE_$yy$mm$dd_$mm$yy.CSV"

$dd_$mm --> Trying to treat as $dd as variable and _ is not detecting. You remove that as,

sapfn="SAP_PAYROLL_FILE_$yy$mm${dd}_$}mm}$yy.CSV"

hth.

Easy to suggest when don't know about the problem!
Robert-Jan Goossens_1
Honored Contributor

Re: Very simplistic script not working

or use two variables.

#!/bin/ksh
#
dd=`date '+%d'`
mm=`date '+%m'`
yy=`date | awk '{print $6}'`
#
echo "The current day is $dd"
echo "The current month is $mm"
echo "The current day is $yy"
#
sapfn="SAP_PAYROLL_FILE_$yy$mm$dd"
sapfnex="_$mm$yy.CSV"
echo "$sapfn$sapfnex"

tmp# ./d
The current day is 03
The current month is 10
The current day is 2005
SAP_PAYROLL_FILE_20051003_102005.CSV

HTH,
Robert-Jan
James R. Ferguson
Acclaimed Contributor

Re: Very simplistic script not working

Derek:

You can avoid variable name versus variable substitution ambiquity by enclosing the variable names in curly braces, like this:

sapfn="SAP_PAYROLL_FILE_${yy}${mm}${dd}_${mm}${yy}.CSV"

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor
Solution

Re: Very simplistic script not working

As an addition to other replies
try to take a little bit care while using
and naming variable...
For example;
dd=`date '+%d'`
and please try to see man page of dd.
"dd" is one the unix command so system may
easily mixe it...
:-)

Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Derek Brown
Frequent Advisor

Re: Very simplistic script not working

Brilliant answers ... thank you
Cem Tugrul
Esteemed Contributor

Re: Very simplistic script not working

no points for me
:-(
Our greatest duty in this life is to help others. And please, if you can't