If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > if loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-13-08, 15:58
nandinir nandinir is offline
Registered User
 
Join Date: Jul 2005
Posts: 276
if loop

Hi All,

acct_prd is passed in as a parameter. Assume if acct_prd= 200712
it should loop through the if loop but its not. Instead its going to the else loop. Any idea if I'm going wrong anywhere here?

Quote:
temp_yr=`echo $acct_prd|cut -c 1-4`

if [[$temp_mth -eq 12 ]]; then
temp_mth=01
temp_yr=$((temp_yr+1))
else
temp_mth=$((temp_mth+01))
Reply With Quote
  #2 (permalink)  
Old 11-13-08, 16:23
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
First, "if" is not a loop - it tests a condition and branches.

Regarding your question, you may want to have a look at the variables. You do not set temp_mth (btw, do you have a reason for abbreviating the variable name?) but you compare with it. Thus, the evaluation will be false (if it doesn't even throw a syntax error due to the unset variable) and the execution goes to the "else" branch. Also, you are missing an "fi", so that the script would throw another syntax error there.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 11-13-08, 16:37
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool Need integers?

1) Leave space after the '[['
Try this:
Code:
#!/bin/ksh
acct_prd=$1
typeset -i temp_yr=`echo $acct_prd|cut -c 1-4`
typeset -i temp_mth=`echo $acct_prd|cut -c 5-6`
echo "Y= $temp_yr, M=$temp_mth"
if [ temp_mth -eq 12 ]; then
  temp_mth=1
 ((  temp_yr += 1 ))
else
 (( temp_mth += 1 ))
fi
echo "Y= $temp_yr, M=$temp_mth"
## -- Results:
$ ./m0 200712
Y= 2007, M=12
Y= 2008, M=1
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 11-13-08, 16:55
nandinir nandinir is offline
Registered User
 
Join Date: Jul 2005
Posts: 276
Thank you so much LKBrwn_DBA. It was the space after [[
it works now.
Reply With Quote
  #5 (permalink)  
Old 11-17-08, 04:17
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
FYI: There is no need to call the external cut command
Code:
temp_yr=${acct_prd%??}     # chop trailing 2 chars
temp_mth=${acct_prd#????}  # chop leading 4 chars
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On