Link to home
Start Free TrialLog in
Avatar of rvsBhanu
rvsBhanuFlag for United States of America

asked on

write a UNIX script for the following

can someone help me to write a single UNIX script for the following two scripts :

script1 file1.d<today's date in the format yyyymmdd>
script2 <one date back in the format:mmmddyy>

Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,

I think I don't really understand your requirement.

What I can say

"today's date in the format yyyymmdd" is date +"%Y%m%d"
for concatenation with a given string: use e.g. filename="file1.d"$(date +"%Y%m%d") ; echo $filename

"one date back in the format:mmmddyy" (I assume "mmm" means "month with a leading zero") is date -d "1 day ago" +"0%m%d%y"
concatenate like above:  filename="file1.d"$(date -d "1 day ago" +"0%m%d%y") ; echo $filename

Note - for the second command above to work you need GNU date. What is your operating system?

wmp


For one day back, try to play with your TZ env variable:

TZ=GMT-24 ; export TZ
that was an example for GMT time zone
Avatar of rvsBhanu

ASKER

the operating system is UNIX.
mmm in the second script means first three alphabets for the month..is it possble to get it that way..?

basically I want to combine these two script into a single script .
So I am looking for the script that can be used at one go..instead of two separate ones.
You will get the first three characters of the month's name with date +%b"

But I'm still far from understanding what you're trying to achieve.

What is the script supposed to do? What should be its output? What the input, if any?


what system do you need this for, and what version of the date command does it ues?
if you have the gnu version of date, you can do
date -d yesterday +"%b%d%y"
some other systems may allow a 24 hour timezone offset, some may not.
(and even there you have have to be careful within an hour of midnight the day after a daylight saving switch)
I basically have these two scripts which I want to make as one.As of now the output of the first one is used as the input for the second one.

So basically I need a script wherein with just one command I am able to execute both of them.Hope that helps.

Wud require help in writing such a script.
as an exaample the commands that are run as of now are :

script1 file1.d20090712
script2 jul1209

I want to write a single script for both of them,wherein the output of the first is given as input to the second and I get the final o/p.
I fear you will have to post both of your scripts.
Without knowing their content I can't get the clue of what you're trying to achieve.

I want to combine the two scripts into one,ie.by running a single script both of these should be executed.

#!/bin/csh
filename="file1.d"$(date +"%Y%m%d") ; echo $filename
script1 file1name
echo "script1 completed"
date1=date -d yesterday +"%b%d%y"
script date1
echo "script2 completed"
#

Please let me know if the above is correct ?
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes,actually the file1 created has date as the primary diffrentiator.

So I want the script1 to work taking today's date as an parameter concatenated with a filename.
And script2 to run with yesterday's date as a parameter in the format mmmddyy,where mmm is the first 3 letters of the name of the month.

So is the script correct for this purpose ?
Yes it is!
I tried to run the below script,but it gives me an error : Variable syntax

#!/bin/csh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
date1=$(date -d yesterday +"%b%d%y")
./script2 $date1
echo "script2 completed"
#

can someone please check and let me know where it is to be corrected ?
when I  give date command I get the following o/p :

Tue Jul 14 09:54:38 EDT 2009

So is the above script correct for this version of date ?However,it gives me variable syntax error.

How can I get this to be working ?
Oh, sorry,
just saw that you're using a shell which is incompatible with the syntax used.
Simply change the very first line of the script to contain
#!/bin/ksh
and it will work!
wmp
It still gives me an error.finalscript3 is the name given to the script .

It gives me the following error :
-----------------------------------------------------------------
./finalscript3[2]: date+%Y%m%d:  not found
Enter only one valid file name
Syntax: ./script1 <filename>
script1 completed
./finalscript3[5]: date-d:  not found
enter date suffix for the filename in the format mmmyy..
script2 completed
-------------------------------------------------------------------------------
Can you pls chk and let me know what is causing the error ?
This should not happen if you ran the script as you posted it in 24849295.
Could you please post the script exactly as you try to run it? It just seems as if there were some spaces or quotes missing.
here is the script that I am running :

#!/bin/ksh
filename="file1.d"$(date+"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date-d yesterday+"%b%d%y")
./script2 $ydate
echo "script2 completed"
#
-------------------------------------------------------
Pls chk and let me know what is causing the error ?
filename="file1.d"$(date +"%Y%m%d")
ydate=$(date -d yesterday +"%b%d%y")
There must be spaces between
date and +  
yesterday and +
date and -d


Script1 runs successfully now.

But I still get an error when I try to run the script2.
Below is the error that I am getting :
---------------------------------------------------------------------------------
./finalscript3[5]: date-d:  not found
enter date suffix for the filename in the format mmmyy..
script2 completed
----------------------------------------------------------------
Well,
I told you that you need a space between date and -d
well my script now looks like this,but still gives the error:

---------------------------------------------------------------------
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date -d yesterday +"%b%d%y")
./script2 $ydate
echo "script2 completed"
#

----------------------------------------------------------------------
error:
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
enter date suffix for the filename in the format mmmyy..

Please suggest ?
how can I determine whether my OS supports the GNU date command ?if not,wat can be given in its place to resolve the above error ?
You could install GNU date if it's not there. GNU date is available for almost any UNIX platform. Now I need to know exactly which platform you're on. Please post the output of
uname -a
% uname -a
SunOS dvc7600 5.9 Generic_118558-38 sun4u sparc SUNW,Netra-T12
any resolution for this ?

well my script now looks like this,but still gives the error:

---------------------------------------------------------------------
#!/bin/ksh
filename="file1.d"$(date +"%Y%m%d")
./script1 $filename
echo "script1 completed"
ydate=$(date -d yesterday +"%b%d%y")
./script2 $ydate
echo "script2 completed"
#

----------------------------------------------------------------------
error:
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
enter date suffix for the filename in the format mmmyy..

GNU date is part of GNU coreutls.

Find the version for your OS here:

http://www.sunfreeware.com/programlistsparc9.html#coreutils

Download- and installation instructions are here:

http://www.sunfreeware.com/download.html

wmp



what is the other alternative in case I cannot get this installed as I do not have perms on the box.?
does the error show that GNU is not installed..?
is there any other alternative script that can be written ?If yes,what can that be ?
can someone pls help with the alternative script ?
On my system I have TZ=GMT+4 , so when I run date command e.g. I get

Wed Jul 15 06:39:22 GMT 2009

When I change TZ to

TZ=GMT+28

and run

date

I get

Tue Jul 14 06:40:49 GMT 2009

which is one day back

can you replace the line below

ydate=$(date -d yesterday +"%b%d%y")

with something like

TZ=GMT-24
ydate=$(date +"%b%d%y")

Please see your actual time zone with

echo $TZ

Below is the o/p that I get when I give date and TZ command :

% date
Wed Jul 15 09:41:59 EDT 2009
% echo $TZ
TZ: Undefined variable

Pls look into this and suggest something ?
can you set it as

TZ=EDT+24 ; export TZ

and then run

date

and see results (This is what I could see from your date output that the Timezone for you is EDT)
% TZ=EDT+24;export TZ
TZ=EDT+24: Command not found
export: Command not found

Pls suggest ?
what is your shell? run

env

OR

set

and see vaiable like SHELL or shell
Avatar of Tintin
Tintin

rvsBhanu is using csh, in which  case, either do

setenv TZ EDT+24
date

or

ksh
export TZ=EDT+24
date
would TZ work for all days like for DST also ?
else is there some other alternative to the below line of script :
ydate=$(date -d yesterday +"%b%d%y")
 
I think this is gnu date which looks like you don't have it installed on your system.
I think this is gnu date which looks like you don't have it installed on your system.
I think this is gnu date which looks like you don't have it installed on your system.