Results 1 to 5 of 5

Thread: if loop

  1. #1
    Join Date
    Jul 2005
    Posts
    276

    Unanswered: 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?

    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))

  2. #2
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  4. #4
    Join Date
    Jul 2005
    Posts
    276
    Thank you so much LKBrwn_DBA. It was the space after [[
    it works now.

  5. #5
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •