Zero Padding Bash/Shell
I need to the last 3 chars say 002 and decrement it by 1 001. I currently have a problem whereby it is converting it to an int. So 001 becomes 1. What I am trying to do is use the printf and convert it back to 001. However this seems to not work for me. I have the following: 

    set -x
     
    substring()
    {
        VAR=$1
        OFFSET=$2
        LENGTH=$3
        RET_VAL=${VAR:$OFFSET:$LENGTH}
    }
     
    # $1 filename
     
    # Change to PF Gazetteer directory
    cd $CARRIER_PF_UPDATES_DIR
     
    LOG=log.txt
    DownloadedFile=$1
    OldFile=
    DATE=$(date +"%Y-%m-%d-%H:%M:%S")
    RET=0
     
    if [ "${2+1}" == "1" ]
    then
        echo "Date: " $DATE > $LOG
        echo "filename: " $DownloadedFile >> $LOG
    fi
     
    substring ${DownloadedFile} -3 3
    extension=$RET_VAL
    old_ext=$((${extension}+1))
     
    substring ${DownloadedFile} 0 9
    echo $RET_VAL
    oldFile="${RET_VAL}${old_ext}"
    oldFile= `printf "%020d" ${RET_VAL}`
    oldFile="${oldFile}.${old_ext}"
     
    #printf '%03d\n' 1
     
    echo $oldFile
     
    # move any temporary files if they exist
    mv "${DownloadedFile}~" ${DownloadedFile}
     
    # check to see if we got a file
    result=$(grep 226 run.ilog)
     
     
    if [ "${2+1}" == "1" ]
    then
        echo "grep found: " $result >> $LOG
    fi
     
    if [ -e "${DownloadedFile}" ]
    then
       #add back onto filename
       #store back onto var = $OldFile
       #Check if OldFile exist
       #Cat files together and - Cat oldFile DownloadedFile > tempFile
    echo ""
    fi
     
    if [ "$result" == "" ]
    then
         # File not found
         RET=100
    else
         RET=10
         # START F4202
         sorted=$(sort -t'+' -k7 ${fileName} > ${fileName}.sorted)
         if [ -e "${fileName}.sorted" ]
         then
            mv "${fileName}" "unsorted/${fileName}.unsorted"
            mv "${fileName}.sorted" ${fileName}
         fi
         # END F4202
    fi
     
    if [ "${2+1}" == "1" ]
    then
        echo "script ret: " $RET >> $LOG
    fi

To see the output click the following link [Ideone][1] I seem to be getting 1.1 instead of 1.1 when I should be getting 001
 


  [1]: http://ideone.com/dceC4
Zero Padding Bash/Shell
===

I need to the last 3 chars say 002 and decrement it by 1 001. I currently have a problem whereby it is converting it to an int. So 001 becomes 1. What I am trying to do is use the printf and convert it back to 001. However this seems to not work for me. I have the following: 

    set -x
     
    substring()
    {
        VAR=$1
        OFFSET=$2
        LENGTH=$3
        RET_VAL=${VAR:$OFFSET:$LENGTH}
    }
     
    # $1 filename
     
    # Change to PF Gazetteer directory
    cd $CARRIER_PF_UPDATES_DIR
     
    LOG=log.txt
    DownloadedFile=$1
    OldFile=
    DATE=$(date +"%Y-%m-%d-%H:%M:%S")
    RET=0
     
    if [ "${2+1}" == "1" ]
    then
        echo "Date: " $DATE > $LOG
        echo "filename: " $DownloadedFile >> $LOG
    fi
     
    substring ${DownloadedFile} -3 3
    extension=$RET_VAL
    old_ext=$((${extension}+1))
     
    substring ${DownloadedFile} 0 9
    echo $RET_VAL
    oldFile="${RET_VAL}${old_ext}"
    oldFile= `printf "%020d" ${RET_VAL}`
    oldFile="${oldFile}.${old_ext}"
     
    #printf '%03d\n' 1
     
    echo $oldFile
     
    # move any temporary files if they exist
    mv "${DownloadedFile}~" ${DownloadedFile}
     
    # check to see if we got a file
    result=$(grep 226 run.ilog)
     
     
    if [ "${2+1}" == "1" ]
    then
        echo "grep found: " $result >> $LOG
    fi
     
    if [ -e "${DownloadedFile}" ]
    then
       #add back onto filename
       #store back onto var = $OldFile
       #Check if OldFile exist
       #Cat files together and - Cat oldFile DownloadedFile > tempFile
    echo ""
    fi
     
    if [ "$result" == "" ]
    then
         # File not found
         RET=100
    else
         RET=10
         # START F4202
         sorted=$(sort -t'+' -k7 ${fileName} > ${fileName}.sorted)
         if [ -e "${fileName}.sorted" ]
         then
            mv "${fileName}" "unsorted/${fileName}.unsorted"
            mv "${fileName}.sorted" ${fileName}
         fi
         # END F4202
    fi
     
    if [ "${2+1}" == "1" ]
    then
        echo "script ret: " $RET >> $LOG
    fi

To see the output click the following link [Ideone][1] I seem to be getting 1.1 instead of 1.1 when I should be getting 001
 


  [1]: http://ideone.com/dceC4