![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" | iBot | UNIX and Linux RSS News | 0 | 01-04-2008 12:00 PM |
| Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" | Lokesha | UNIX for Dummies Questions & Answers | 4 | 12-19-2007 10:52 PM |
| how could i make a program mixed with many "|", "<" and ">" | strugglingman | High Level Programming | 2 | 04-29-2006 05:11 AM |
| No utpmx entry: you must exec "login" from lowest level "shell" | peterpan | UNIX for Dummies Questions & Answers | 0 | 01-18-2006 01:15 AM |
| how to request a "read" or "delivered" receipt for mails | plelie2 | Shell Programming and Scripting | 1 | 08-06-2002 12:26 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
need to keep the "0" in "01"
System = AIX
scripting = ksh me = fairly new to this. I hope this has not been asked already. Scenario: grep'ing for information out of files with a naming convention as such.... 2008_01*. I will be performing my grep on these file names during the month after (i.e. 02). Problem: When I try to subtract "02" - "01", get "1". This makes sense seeing as how most calculations drop the preceding "0" I need to keep that "0" to perform my grep. Code: # ======> GET YEAR & MONTH *** #export YEAR=`date +%Y` #export MNTH=`date +%m` export YEAR=2008 export MNTH=02 # ************************************** # *** FIND PREVIOUS MONTH/YEAR VALUE *** # ************************************** if [ $MNTH!=01 ] then (( MNTH=$MNTH-01 )) YEAR=$YEAR else if [ $MNTH=01 ] then MNTH=12 (( YEAR=$YEAR-1 )) fi fi ***NOTE ~ (( YEAR=$YEAR-1 )) works perfectly. !!!!!The above statement is now incorrect!!!!! I would prefer to keep it as simple as it is now, however all help is appreciated. I would consider using sed or awk to add the "0" in, however an example or two of how to do that would be great. Last edited by cml2008; 01-25-2008 at 09:15 PM. Reason: statement is now incorrect |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
add a line
Code:
MNTH=$(printf "%02d" $MNTH) |
|
#3
|
|||
|
|||
|
Jim,
Excellent, that worked! I see that I will have to take some time to decipher all of the parameters for printf in the man pages! That being said, maybe you can help me know with the later half of the "if" statement. if [ $MNTH!=01 ] then (( MNTH=$MNTH-01 )) MNTH=$(printf "%02d" $MNTH) YEAR=$YEAR else if [ $MNTH=01 ] then MNTH=12 (( YEAR=$YEAR-1 )) in red now prints out as such 00/2009 again that is very cool about the printf, thanks for you help. |
|
#4
|
|||
|
|||
|
just guessing, but do you need to use "==" when comparing instead of "="? Most scripts are the same in that regard "=" is for assigning values, "==" is for comparing values.
|
|
#5
|
|||
|
|||
|
KevinADC,
I looked into your suggestion and added the extra "=" however it did not make a difference in comparing the "value". I believe this because the compare happens within [ ]. |
|
#6
|
|||
|
|||
|
My theory is that the "printf" command is overriding the second "if" statement.
I have tried to read through the man pages on printf, however that seems to be a bit cumbersome, does anybody know if the "printf" command is overriding my second "if" statement? If so do I have to put in another "printf" command to format the variable "$MNTH" to = 12? Quote:
Last edited by cml2008; 01-26-2008 at 08:42 AM. Reason: grammatical errors |
|
#7
|
||||
|
||||
|
Each item in the test must be separated by spaces.
wrong: [ $MNTH!=01 ] right: [ $MNTH != 01 ] |
||||
| Google The UNIX and Linux Forums |