Shell scripting looping issue

#!bin/ksh


---------------------------------------------------------------------------------------------
--                                      Get sequence number from database 
---------------------------------------------------------------------------------------------

.os rm ${BASE_DIRECTORY}/datain/mediation_edm/landing/getsequence.txt 
.export file ${BASE_DIRECTORY}/datain/mediation_edm/landing/getsequence.txt

SELECT MIN(data_file_name)(TITLE '') FROM ${CTLFW_VIEW_DB}.CTLFW_SYSTEM_FILE_EXTRACT WHERE CTL_ID=690 AND BUSINESS_DATE ge DATE-31 AND FILE_ID =88;
.IF ACTIVITYCOUNT <> 1 THEN SELECT MAX(data_file_name)(TITLE '') FROM ${CTLFW_VIEW_DB}.CTLFW_SYSTEM_FILE_EXTRACT WHERE CTL_ID=690 AND BUSINESS_DATE lt DATE-31 AND FILE_ID =88;
.IF ACTIVITYCOUNT <> 1 THEN QUIT 1
.export reset
.logoff
.quit 0
!!END!!

Mediation_MSC_TO_VODA12_Daily()
{
set -x
DataFileName=$(awk 'NR == 1 { print; exit 0 }' getsequence.txt)
SequenceNum=${DataFileName:23:8}
for file in `ls`
do
if test ${file:21:2} -gt 10;
then
ServerNumber=01
IncSequence="$((SequenceNum=SequenceNum+1))"
PaddedSequence=$IncSequence
while [[ $(echo -n ${PaddedSequence} | wc -c) -lt 8 ]] ; do
    PaddedSequence="0${PaddedSequence}"
done
new=${file:0:21}$ServerNumber$PaddedSequence${file:31:4} 
mv $file $new
fi
done
}

show_menus() {
    clear
    echo "~~~~~~~~~~~~~~~~~~~~~"    
    echo " Flow Number Menu"
    echo "~~~~~~~~~~~~~~~~~~~~~"
    echo "Select the number to replace name of the flow"
    echo "1. Mediation_MSC_TO_VODA12_Daily"
    echo "2. Mediation_MTAS_TO_VODA12_Daily"
    echo "3. Mediation_SMS_TO_VODA12_Daily"
    echo "4. Mediation_EPGW_PCDR_to_VODA12_Daily"
    echo "5. Mediation_ESGSN_SCDR_to_VODA12_Daily"
    echo "6. Mediation_ESGW_SCDR_to_VODA12_Daily"
    echo "7. MediationOR_To_Voda12_Daily"
    echo "8. Exit"
}

read_options(){
    echo "Enter choice [ 1 - 8] "
    read choice    
    case $choice in
        1) Mediation_MSC_TO_VODA12_Daily ;;
        2) two ;;
        3) three ;;
        4) four ;;
        5) five ;;
        6) six ;;
        7) seven ;;
        8) exit 0;;
        *) echo -e "Enter a Valid Input"
    esac
}

show_menus
read_options

The above code loops for each file ,but i want to loop for every 2 files shown below

---------- Post updated at 11:29 PM ---------- Previous update was at 10:09 PM ----------

E_MSC_EKMS1_20160501_1100000001.dat E_MSC_EKMS1_20160501_1100000001.ctl
  E_MSC_EKMS1_20160501_1200000001.dat E_MSC_EKMS1_20160501_1200000001.ctl
  E_MSC_EKMS1_20160501_1300000001.dat E_MSC_EKMS1_20160501_1300000001.ctl

the above files should be renamed to

E_MSC_EKMS1_20160501_0100000005.dat
E_MSC_EKMS1_20160501_0100000005.ctl

E_MSC_EKMS1_20160501_0100000004.dat
E_MSC_EKMS1_20160501_0100000004.ctl

E_MSC_EKMS1_20160501_0100000003.dat
E_MSC_EKMS1_20160501_0100000003.ctl

There are lots of missing pieces of information that are crucial if you want us to help you:

  1. What operating system are you using?
  2. How is your script supposed to determine which pairs of files you want to process?
  3. After running your database query, what is the format of the contents of getsequence.txt ?
  4. What files are present in the directory in which this script will be run?
  5. Are we correct in assuming that you are only interested in what happens when the user types in 1 when read_options runs?

Hi Don,

Thanks for your response

1)we are using unix

3) getsequence.tx t will contain format like ' E_MSC_EKMS1_20171013_0100127269.dat '
4)these are the files in my directory

E_MSC_EKMS1_20160501_1100023455.dat
E_MSC_EKMS1_20160501_1100023455.ctl

E_MSC_EKMS1_20160501_1100023456.dat
E_MSC_EKMS1_20160501_1100023456.ctl

E_MSC_EKMS1_20160501_1100023457.dat
E_MSC_EKMS1_20160501_1100023457.ctl

they need to be renamed like

E_MSC_EKMS1_20160501_0100000005.dat
E_MSC_EKMS1_20160501_0100000005.ctl

E_MSC_EKMS1_20160501_0100000004.dat
E_MSC_EKMS1_20160501_0100000004.ctl

E_MSC_EKMS1_20160501_0100000003.dat
E_MSC_EKMS1_20160501_0100000003.ctl

5)yes when user types 1

The way to tell us what operating system you're using is to show us the output from the command uname -a .

If I understand your code correctly (and I am not sure that I do), if the first line of the contents of getsequence.txt is:

E_MSC_EKMS1_20171013_0100127269.dat

where did the IncSequence values 5, 4, and 3 come from??? Shouldn't the files be renamed as follows?:

Old name:				New name:
===================================	===================================
E_MSC_EKMS1_20160501_1100023455.ctl	E_MSC_EKMS1_20160501_0100127270.ctl
E_MSC_EKMS1_20160501_1100023455.dat	E_MSC_EKMS1_20160501_0100127270.dat
E_MSC_EKMS1_20160501_1100023456.ctl	E_MSC_EKMS1_20160501_0100127271.ctl
E_MSC_EKMS1_20160501_1100023456.dat	E_MSC_EKMS1_20160501_0100127271.dat
E_MSC_EKMS1_20160501_1100023457.ctl	E_MSC_EKMS1_20160501_0100127272.ctl
E_MSC_EKMS1_20160501_1100023457.dat	E_MSC_EKMS1_20160501_0100127272.dat