The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-25-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
add a line
Code:
MNTH=$(printf "%02d" $MNTH)
Reply With Quote
  #3  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
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.
Reply With Quote
  #4  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
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.
Reply With Quote
  #5  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
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 [ ].
Reply With Quote
  #6  
Old 01-26-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
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:
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

Last edited by cml2008; 01-26-2008 at 08:42 AM. Reason: grammatical errors
Reply With Quote
  #7  
Old 01-26-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Each item in the test must be separated by spaces.

wrong: [ $MNTH!=01 ]
right: [ $MNTH != 01 ]
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0