Can't find the bug in my code - bombing with rename

Hi Folks -

I'm encountering an issue:

Scenario:
We have automated GL data loads utilizing FDMEE. The problem is that some of our Locations could have multiple files. I think we are running into a situation where the script is trying to name the 2 files the same name and it is bombing out.

Question:
Have you ever had to deal with this? Do you have any ideas as to how I could modify the script to accommodate renaming multiple files?

The example file names that we are loading are below.

HYP_GL_EXT_Maurices_CA_Primary_Ledger_NOV-17
HYP_GL_EXT_Maurices_US_Primary_Ledger_NOV-17

Script is below:

#!/bin/bash
## OracleGL_ASC_MAU_Batch.sh: Load MAU OracleGL

source /u01/app/Automation/env.sh
source $MaxlExePath/setEnv.sh

export Location_Name=OracleGL_ASC_MAU
export Batch_Name=OracleGL_ASC_MAU
export LoadRule_Name=OracleGL_ASC_MAU_Actual
export outputlog=$SCENARIOMANAGEMENT_AUTOMATION_PATH/logs/$Location_Name"_"`date +%Y``date +%m``date +%d`"_"`date +%H``date +%M`.log

echo ------------------------------------------------------------------------- > $outputlog
# Generate 2 digit month, day and 4 digit year:
Day=`date +%d`
Month=`date +%m`
Year=`date +%Y`
All=$Year$Day$Month

# If the system date's day number is 15 or less, then use the prior month, (and prior year if month is January)
if [ $Day -le 15 ]
    then 
        if [ $Month == 01 ]; then
            Output_Month="12"
            Output_Year=$(date -d "-1 year" +%Y)
        else     
            Output_Month=$(date -d "-1 month" +%m)    
            Output_Year=`date +%Y`
        fi
    
fi


if [ $Day -gt 15 ]; then
    Output_Month=$Month
    Output_Year=`date +%Y`
fi

echo "$Output_Month"
echo $Output_Year


#Get the 3 character Month:
if [[ $Output_Month == 01 ]] 
    then MMM="Jan"
elif [[ $Output_Month == 02 ]] 
    then MMM="Feb"
elif [[ $Output_Month == 03 ]]  
    then MMM="Mar"
elif [[ $Output_Month == 04 ]]  
    then MMM="Apr"
elif [[ $Output_Month == 05 ]]  
    then MMM="May"
elif [[ $Output_Month == 06 ]] 
    then MMM="Jun"
elif [[ $Output_Month == 07 ]]  
    then MMM="Jul"
elif [[ $Output_Month == 08 ]]  
    then MMM="Aug"
elif [[ $Output_Month == 09 ]]  
    then MMM="Sep"
elif [[ $Output_Month == 10 ]]  
    then MMM="Oct"
elif [[ $Output_Month == 11 ]]  
    then MMM="Nov"
elif [[ $Output_Month == 12 ]]  
    then MMM="Dec"
fi

echo $MMM

#Rename the file:

cd $FDM_Inbox/$Location_Name/

if [[ -f *.* ]]
then
cat *.* > $FDM_Inbox/$Location_Name/"A~$LoadRule_Name~$MMM-$Output_Year~RR.txt"
#mv -f *.* $FDM_Inbox/$Location_Name/"A~$LoadRule_Name~$MMM-$Output_Year~RR.txt"
fi

LatestFile="A~$LoadRule_Name~$MMM-$Output_Year~RR.txt"

echo GL File Rename - $LatestFile Complete >> $outputlog

echo Start Processing file $LatestFile for FDM Rule $LoadRule_Name on $Date at `date +%H`:`date +%M` >> $outputlog
echo . >> $outputlog

#$FDM_INBOX$Location_Name
cp $LatestFile $FDM_Inbox/batches/openbatch

#Move the renamed file to the archive directory:
mv $LatestFile $FDM_ARCHIVE/$Location_Name/$LatestFile"_"`date +%Y``date +%m``date +%d`"_"`date +%H``date +%M`.txt

cd $Run_Batch_Location


./runbatch.sh admin welcome1 $Batch_Name

echo . >> $outputlog
echo Completed Processing file $LatestFile for FDM Location $Location_Name on $Date at $time >> $outputlog
echo ------------------------------------------------------------------------- >> $outputlog

Thank you for your help!

When you say "bombing out", what exactly happens?

I see a few suspect things in here. Particularly if [[ -f *.* ]] , which will bomb out when there's more than one file, -f only accepts a single name.

Also, *.* is a DOS-ism, all that's needed in UNIX is *

A better way to do it, I think, would be:

set -- *
if [ ! -z "$1" ]
then
        cat * > $FDM_Inbox/$Location_Name/"A~$LoadRule_Name~$MMM-$Output_Year~RR.txt"
fi

What do you mean " it is bombing out". Please give the precise error message or describe the unwanted behaviour in detail.

Thank you for the replies!

here is the error message Im getting: ( see attached)

Please don't post pictures! Post text that can be analysed.

Are you sure A~OracleGL_ASC_MAU_Actual~Nov-2016~RR.txt does exist in your actual working directory? You said your files were

HYP_GL_EXT_Maurices_CA_Primary_Ledger_NOV-17
HYP_GL_EXT_Maurices_US_Primary_Ledger_NOV-17

.

Thank you Corona688, I will give this a shot!

What is the purpose of Set -- * in this instance though?

set is a shell built in. It can be used to define environmental variables however in this instance it is being used to evaluate positional parameters based upon what * expands to. For example, if * expands to: file1 file2 file3 file4 file5 , then set will resolve file1 as positional parameter 1 (i.e. $1=file1), file2 as positional parameter 2 (i.e. $2=file2) etc...

The -- is used to denote the end of the options. Meaning that set will consider everything after -- as parameters, not options (as set can take numerous options such as -x). This is particularly useful if any of the parameters start with a -

Thank you all for your help and explanations! working as expected!

1 Like

How many arguments can set handle?
Perhaps the file_exists function scales better?

file_exists() {
  for _i do [ -f "$_i" ] && return
  done
  return 1
}

if file_exists *.*
then
  ...
fi

--
The if-elsif-fi chain is a typical case-esac chain

case $Output_Month in
(01) MMM="Jan";;
(02) MMM="Feb";;
...
esac