Solved

write a UNIX script for the following

Posted on 2009-07-12
42
333 Views
Last Modified: 2013-12-26
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>

0
Comment
Question by:rvsBhanu
  • 19
  • 11
  • 8
  • +2
42 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24835028
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


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24835378
For one day back, try to play with your TZ env variable:

TZ=GMT-24 ; export TZ
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24835401
that was an example for GMT time zone
0
 

Author Comment

by:rvsBhanu
ID: 24835952
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24835968
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?


0
 
LVL 84

Expert Comment

by:ozo
ID: 24836065
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)
0
 

Author Comment

by:rvsBhanu
ID: 24836532
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.
0
 

Author Comment

by:rvsBhanu
ID: 24836547
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.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24837785
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.

0
 

Author Comment

by:rvsBhanu
ID: 24842084
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 ?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 24843436
OK,
I still don't understand what the scripts are supposed to do,
but here some corrections:

#!/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"
#
exit

I removed the echo from the second line, I put it in in my post above only for illustration purposes.
The date strings we composed form commandline parameters for your scripts 'script1' and 'script2'.
Is this what you intended?

wmp
0
 

Author Comment

by:rvsBhanu
ID: 24843916
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 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24843947
Yes it is!
0
 

Author Comment

by:rvsBhanu
ID: 24849295
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 ?
0
 

Author Comment

by:rvsBhanu
ID: 24849511
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 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24849562
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
0
 

Author Comment

by:rvsBhanu
ID: 24849771
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 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24849864
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.
0
 

Author Comment

by:rvsBhanu
ID: 24850320
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 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24850388
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


0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rvsBhanu
ID: 24850778
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
----------------------------------------------------------------
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24850825
Well,
I told you that you need a space between date and -d
0
 

Author Comment

by:rvsBhanu
ID: 24851984
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 ?
0
 

Author Comment

by:rvsBhanu
ID: 24852469
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 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24852863
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
0
 

Author Comment

by:rvsBhanu
ID: 24852981
% uname -a
SunOS dvc7600 5.9 Generic_118558-38 sun4u sparc SUNW,Netra-T12
0
 

Author Comment

by:rvsBhanu
ID: 24853399
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..

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24854888
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



0
 

Author Comment

by:rvsBhanu
ID: 24855090
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 ?
0
 

Author Comment

by:rvsBhanu
ID: 24855723
can someone pls help with the alternative script ?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24856157
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

0
 

Author Comment

by:rvsBhanu
ID: 24859239
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 ?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24863948
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)
0
 

Author Comment

by:rvsBhanu
ID: 24864388
% TZ=EDT+24;export TZ
TZ=EDT+24: Command not found
export: Command not found

Pls suggest ?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24864413
what is your shell? run

env

OR

set

and see vaiable like SHELL or shell
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24864813
rvsBhanu is using csh, in which  case, either do

setenv TZ EDT+24
date

or

ksh
export TZ=EDT+24
date
0
 

Author Comment

by:rvsBhanu
ID: 24865447
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")
 
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24865793
I think this is gnu date which looks like you don't have it installed on your system.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24865795
I think this is gnu date which looks like you don't have it installed on your system.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24865797
I think this is gnu date which looks like you don't have it installed on your system.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now