Re: Newbie Scripting Variable Question
- From: aix@xxxxxxxx
- Date: 9 Aug 2006 11:21:05 -0700
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
.
- Follow-Ups:
- Re: Newbie Scripting Variable Question
- From: steven_nospam at Yahoo! Canada
- Re: Newbie Scripting Variable Question
- References:
- Newbie Scripting Variable Question
- From: dawaves
- Re: Newbie Scripting Variable Question
- From: steven_nospam at Yahoo! Canada
- Re: Newbie Scripting Variable Question
- From: dawaves
- Re: Newbie Scripting Variable Question
- From: dawaves
- Re: Newbie Scripting Variable Question
- From: steven_nospam at Yahoo! Canada
- Re: Newbie Scripting Variable Question
- From: dawaves
- Newbie Scripting Variable Question
- Prev by Date: Re: how to get a file from the Switch6509 back to Unix server ?
- Next by Date: Re: AIX5.1 on 43P 140
- Previous by thread: Re: Newbie Scripting Variable Question
- Next by thread: Re: Newbie Scripting Variable Question
- Index(es):
Relevant Pages
|