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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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! |
| |||
| 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 |
| |||
| 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") |
| |||
| 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") |
| |||
| 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") |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| ||||
| 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 |
| Thread Tools | |
| Display Modes | |
|
|