Shell Scripts (Renaming file names with sequential numbers)

Hi there,

Firstly, I have no experience with shell scripts so would really appreciate some help.

I have the following shell script that is causing some problems:

moveit()
{
    [ ${DEBG} = "1" ] && set -x

    if [ -f ${JOURNAL}.${ASOF_DATE} ]  
    then
        DOUBLE_DELIVERY=$(grep "${SW_ID}.*${P_DIR}.*${TARGET_NAME}.*${N_OF_REC}.*${SZ_KI}" ${JOURNAL}.${ASOF_DATE})
    fi
    if [ ! -z $DOUBLE_DELIVERY ]
    then
        LOC_TAG="ASIFUNC_01"
        ERROR_MSG="File $2 was sent already" 
        msg_error ${ERROR_MSG} && temp_err_handler FI_DBLE
    fi

    LOC_TAG="ASIFUNC_02" 
    ERROR_MSG="Destination $2 already exists" 
    ls "$2" >/dev/null 2>&1 && msg_error ${ERROR_MSG} && temp_err_handler FI_EXST
    msg_message "\"$1\" has been moved to \"$2\""
    mv "$1" "$2"
    if [ "$?" -ne "0" ] 
    then
        LOC_TAG="ASIFUNC_03"
        ERROR_MSG="Can't move file $1 to $2"
        msg_error ${ERROR_MSG}
        temp_err_handler MV_FILE
    else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE} 
            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else
            msg_message "Could not generate a Journal file for $2"
        fi
        logStatus
    fi
}
#

The problem is, the current setup is causing problems when we have duplicate archiving retrievals, as I can see the log file being written does not allow duplicate entries with the same. I need to change this by creating a sequential number and renaming the file with say _1, then the next file can be called _2. This would enable us to retrieve an archiving period as many times as we need to without the errors.

I would propose to delete the first statement:

if [ -f ${JOURNAL}.${ASOF_DATE} ] 
    then
        DOUBLE_DELIVERY=$(grep "${SW_ID}.*${P_DIR}.*${TARGET_NAME}.*${N_OF_REC}.*${SZ_KI}" ${JOURNAL}.${ASOF_DATE})
    fi
    if [ ! -z $DOUBLE_DELIVERY ]
    then
        LOC_TAG="ASIFUNC_01"
        ERROR_MSG="File $2 was sent already" 
        msg_error ${ERROR_MSG} && temp_err_handler FI_DBLE
    fi

then I would place the command in the lower part of the code. However, I am unsure of any shell commands, I have written in bold below the logical explanation of what I want to achieve, if there's someone that can help with the code it would be greatly appreciated.

else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE}._1 
            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else

if file name ends in "_1"
then rename file (_1 + 1) i.e. _2
if file name ends in "_2"
then rename file (_2 + 1) i.e. _3 and so on.....  
end 

            msg_message "Could not generate a Journal file for $2" 
        fi
        logStatus
    fi
in your code itself u can declare variable like this.
n=$(( a + 1 ))
here a is the value that you are defining like _1 or _2 etc....so the same can be increamented and then use in mv oldfile(without _1) to newfile_"$n"

Thanks
SHa

Thanks for your help.

So my code would be: (it still needs some work :slight_smile: )

else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE}.${_}
.${n}
where n=$(( a + 1 ))

            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else

if file name ends in "_1"
then mv oldfile(${JOURNAL}.${ASOF_DATE}.${_}.${n} 
to newfile_"$n"
end 

            msg_message "Could not generate a Journal file for $2" 
        fi
        logStatus
    fi

I am afraid I do not get hold of your code completely.

Given sample shell script with same kind of logice hope this should give you better understanding..

sample shell script which might help you.
Below are the list of files which I want to just rename by increamenting the numbers with already exist files.

 
filename_1
filename_3
filename_5
 
for f in `ls -lrth file*|nawk '{print $NF}'`
do
file=`echo $f|nawk -F"_" '{print $1}'` # store filename without _[0-9] numbers
a=`echo $f|nawk -F"_" '{print $NF}'` store variable which I want to increment by 1
n=$(( a + 1 ))   
mv $f $file_"$n"  # renaming file with new _numbers[0-9]
done

Output will be

 
filename_2
filename_4
filename_6

Thanks
Sha

@Shahul
It's simpler:

for f in *[0-9]; do
  mv "$f" "`echo "$f" | awk -F'_' '{print $1 "_" $2+1}'`"
done

Thanks guys, can you help with the initial naming of the first file to ****_1

createFile ${JOURNAL}.${ASOF_DATE}

The code given by both should work and still Yep I shall advice you to go ahead with yazu format ...that should also work for the first file as well...

:slight_smile: