Unix Technical Forum

Newbie Scripting Variable Question

This is a discussion on Newbie Scripting Variable Question within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hello, In my script, I have defined this: CMONTH=`date | awk '{print $2}'` This gives me CMONTH=Aug My dilemma ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 10:09 AM
dawaves
 
Posts: n/a
Default Newbie Scripting Variable Question

Hello,

In my script, I have defined this:

CMONTH=`date | awk '{print $2}'`

This gives me CMONTH=Aug

My dilemma is that I need to correlate "Aug" with the numerical value
of "Aug", which is "8". If the current month is January, the above
variable will give me "Jan". I would then need the numerical
representation of the month of January, which is "1".

I'm unsure of how to get to that numerical value. Anyone know of a
clean and precise way of doing this?

Thanks!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 10:09 AM
aixdude@yahoo.com
 
Posts: n/a
Default Re: Newbie Scripting Variable Question


dawaves wrote:
> Hello,
>
> In my script, I have defined this:
>
> CMONTH=`date | awk '{print $2}'`
>
> This gives me CMONTH=Aug
>
> My dilemma is that I need to correlate "Aug" with the numerical value
> of "Aug", which is "8". If the current month is January, the above
> variable will give me "Jan". I would then need the numerical
> representation of the month of January, which is "1".
>
> I'm unsure of how to get to that numerical value. Anyone know of a
> clean and precise way of doing this?
>
> Thanks!


date +%m

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 10:09 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

dawaves wrote:
> Hello,
>
> In my script, I have defined this:
>
> CMONTH=`date | awk '{print $2}'`
>
> This gives me CMONTH=Aug
>
> My dilemma is that I need to correlate "Aug" with the numerical value
> of "Aug", which is "8". If the current month is January, the above
> variable will give me "Jan". I would then need the numerical
> representation of the month of January, which is "1".
>
> I'm unsure of how to get to that numerical value. Anyone know of a
> clean and precise way of doing this?



"man" is your friend ! :-) There are a lot of flags available for the
date command. Example:

typeset -i CMONTH
CMONTH=$(date +"%m")

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 10:09 AM
dawaves
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

Man you guys are fast! Ok, now that works.

How about this...


Is there another way to find out the last day of a month other than:

echo `cal` | awk '{print $NF}'

Please post if you know.

thanks!


steven_nospam at Yahoo! Canada wrote:
> dawaves wrote:
> > Hello,
> >
> > In my script, I have defined this:
> >
> > CMONTH=`date | awk '{print $2}'`
> >
> > This gives me CMONTH=Aug
> >
> > My dilemma is that I need to correlate "Aug" with the numerical value
> > of "Aug", which is "8". If the current month is January, the above
> > variable will give me "Jan". I would then need the numerical
> > representation of the month of January, which is "1".
> >
> > I'm unsure of how to get to that numerical value. Anyone know of a
> > clean and precise way of doing this?

>
>
> "man" is your friend ! :-) There are a lot of flags available for the
> date command. Example:
>
> typeset -i CMONTH
> CMONTH=$(date +"%m")


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 10:09 AM
dawaves
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

Why does this not work?

PDAY=echo `cal $PMONTH $PYEAR`|awk '{print $NF}'

I've defined the PMONTH value as the previous month and the PYEAR value
as teh previous year.


dawaves wrote:
> Man you guys are fast! Ok, now that works.
>
> How about this...
>
>
> Is there another way to find out the last day of a month other than:
>
> echo `cal` | awk '{print $NF}'
>
> Please post if you know.
>
> thanks!
>
>
> steven_nospam at Yahoo! Canada wrote:
> > dawaves wrote:
> > > Hello,
> > >
> > > In my script, I have defined this:
> > >
> > > CMONTH=`date | awk '{print $2}'`
> > >
> > > This gives me CMONTH=Aug
> > >
> > > My dilemma is that I need to correlate "Aug" with the numerical value
> > > of "Aug", which is "8". If the current month is January, the above
> > > variable will give me "Jan". I would then need the numerical
> > > representation of the month of January, which is "1".
> > >
> > > I'm unsure of how to get to that numerical value. Anyone know of a
> > > clean and precise way of doing this?

> >
> >
> > "man" is your friend ! :-) There are a lot of flags available for the
> > date command. Example:
> >
> > typeset -i CMONTH
> > CMONTH=$(date +"%m")


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-05-2008, 10:09 AM
Dan Foster
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

In article <1155069925.073315.70250@m73g2000cwd.googlegroups. com>, dawaves <dawaves@gmail.com> wrote:
> Why does this not work?
>
> PDAY=echo `cal $PMONTH $PYEAR`|awk '{print $NF}'
>
> I've defined the PMONTH value as the previous month and the PYEAR value
> as teh previous year.


If you want to do evaluate-and-substitute for a variable, you need to do
it something like this:

FOO=`commands to execute`

Basically, you were missing that ` ` in the right place so it didn't
evaluate the expression. Even if you had done it as:

PDAY=`echo `cal $PMONTH ...

It'd still have had failed because shell would have seen more than two
backquotes and gotten confused about how to parse it properly.

One way to do it if you want to do it as one line:

PDAY=`cal $PMONTH $PYEAR|tail -1|awk '{print $NF}'`

-Dan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-05-2008, 10:09 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: Newbie Scripting Variable Question


dawaves wrote:
> Why does this not work?
>
> PDAY=echo `cal $PMONTH $PYEAR`|awk '{print $NF}'



A lot of scripters use the single back quotes (`) to indicate a command
to be executed. When I first started out I found it very confusing at
times to see all the different quotes on a line, since you could have
various sets of ", ', and ` on the same line and try to track which are
for what...

You may want to try the following instead. You can use the $(command)
syntax to indicate that everything between the brackets should be
translated and returned to that line. For example:

PDAY=$(cal ${PMONTH} ${PYEAR} | awk '{print $NF}')

Reading this is just like math class. You do the work inside the ( )
first. So what happens is the calendar is produced, and the output is
used as input to the awk command. You then take the results of that and
assign it to PDAY.

By doing it this way, you can test what you are expecting to get on a
command line, then cut and paste the entire line and wrap it in
brackets and you should get the same results assigned to a variable.

Another way to do this is:

cal ${PMONTH} ${PYEAR} | awk '{print $NF}' | read PDAY

The cal is evauated, the output used as input for awk, and then the awk
output is used as input for the read statement to assign a value to
PDAY.

In UNIX, there are almost always 5 ways to accomplish the same goal.
You may wan tto get a copy of the O'Reilly and Associates series of
books....Good examples and well written, they help a lot. Another one
is the Korn Shell by (oddly enough) David Korn.

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-05-2008, 10:09 AM
dawaves
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

Wow all great replies...
Dan Foster: I figured it out exactly how you had it...thanks

Thanks so much guys...

How about this one....


How do I make a variable that I have, let's say it's, "1", into "01".
Or "8" into "08"?

I've tried using 'expr' to add "0" or "00" and those didn't work. Any
suggestions?

Thanks!


steven_nospam at Yahoo! Canada wrote:
> dawaves wrote:
> > Why does this not work?
> >
> > PDAY=echo `cal $PMONTH $PYEAR`|awk '{print $NF}'

>
>
> A lot of scripters use the single back quotes (`) to indicate a command
> to be executed. When I first started out I found it very confusing at
> times to see all the different quotes on a line, since you could have
> various sets of ", ', and ` on the same line and try to track which are
> for what...
>
> You may want to try the following instead. You can use the $(command)
> syntax to indicate that everything between the brackets should be
> translated and returned to that line. For example:
>
> PDAY=$(cal ${PMONTH} ${PYEAR} | awk '{print $NF}')
>
> Reading this is just like math class. You do the work inside the ( )
> first. So what happens is the calendar is produced, and the output is
> used as input to the awk command. You then take the results of that and
> assign it to PDAY.
>
> By doing it this way, you can test what you are expecting to get on a
> command line, then cut and paste the entire line and wrap it in
> brackets and you should get the same results assigned to a variable.
>
> Another way to do this is:
>
> cal ${PMONTH} ${PYEAR} | awk '{print $NF}' | read PDAY
>
> The cal is evauated, the output used as input for awk, and then the awk
> output is used as input for the read statement to assign a value to
> PDAY.
>
> In UNIX, there are almost always 5 ways to accomplish the same goal.
> You may wan tto get a copy of the O'Reilly and Associates series of
> books....Good examples and well written, they help a lot. Another one
> is the Korn Shell by (oddly enough) David Korn.
>
> Steve


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-05-2008, 10:10 AM
aix@mail.com
 
Posts: n/a
Default Re: Newbie Scripting Variable Question


dawaves wrote:
> Wow all great replies...
> Dan Foster: I figured it out exactly how you had it...thanks
>
> Thanks so much guys...
>
> How about this one....
>
>
> How do I make a variable that I have, let's say it's, "1", into "01".
> Or "8" into "08"?
>
> I've tried using 'expr' to add "0" or "00" and those didn't work. Any
> suggestions?
>
> Thanks!
>
>
> steven_nospam at Yahoo! Canada wrote:
> > dawaves wrote:
> > > Why does this not work?
> > >
> > > PDAY=echo `cal $PMONTH $PYEAR`|awk '{print $NF}'

> >
> >
> > A lot of scripters use the single back quotes (`) to indicate a command
> > to be executed. When I first started out I found it very confusing at
> > times to see all the different quotes on a line, since you could have
> > various sets of ", ', and ` on the same line and try to track which are
> > for what...
> >
> > You may want to try the following instead. You can use the $(command)
> > syntax to indicate that everything between the brackets should be
> > translated and returned to that line. For example:
> >
> > PDAY=$(cal ${PMONTH} ${PYEAR} | awk '{print $NF}')
> >
> > Reading this is just like math class. You do the work inside the ( )
> > first. So what happens is the calendar is produced, and the output is
> > used as input to the awk command. You then take the results of that and
> > assign it to PDAY.
> >
> > By doing it this way, you can test what you are expecting to get on a
> > command line, then cut and paste the entire line and wrap it in
> > brackets and you should get the same results assigned to a variable.
> >
> > Another way to do this is:
> >
> > cal ${PMONTH} ${PYEAR} | awk '{print $NF}' | read PDAY
> >
> > The cal is evauated, the output used as input for awk, and then the awk
> > output is used as input for the read statement to assign a value to
> > PDAY.
> >
> > In UNIX, there are almost always 5 ways to accomplish the same goal.
> > You may wan tto get a copy of the O'Reilly and Associates series of
> > books....Good examples and well written, they help a lot. Another one
> > is the Korn Shell by (oddly enough) David Korn.
> >
> > Steve



# typeset -Z2 abc
# abc=1
# echo $abc
01

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-05-2008, 10:10 AM
Paul Landay
 
Posts: n/a
Default Re: Newbie Scripting Variable Question

dawaves wrote:
> How about this one....
> How do I make a variable that I have, let's say it's, "1", into "01".
> Or "8" into "08"?


Be careful. The shell may interpret "0x" as an octal number,
not left-zero-padded-decimal. If it does you will find that
"08" is an non-numeric value altogether (not valid octal).

If you only use the "0x" value as a string, then you should
be fine, but in octal 07+01=10.

http://groups.google.com/group/comp....d82a377e66167d

Paul Landay
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:52 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com