Help needed on wrapper script

Hi Gurus,

I need to build a wrapper script which will be passing the loading date and the data file name (provides option to the user to load a single data file or load all the data files) to the actual loader data_load.ksh to load in the database.

  1. I want to execute the loader script data_load.ksh one after another (one at a time), that is once data_load.ksh File1 finishes data_load.ksh File2 will start so and so forth. Loading a single file is not a concern. For full/all data files load, I am reading the data file names from a text file 'file.list' and executing the data_load.ksh from a for loop with an ampersand (sending to the background) & wait signal for each file, hoping that wait command will meet my expectation. Is there any alternate way of doing this?

  2. My date validate part is not quite working as expected. Technically 20141411 date format should be incorrect but according to my date validation check echo ${dt}|grep -E -q '^201[1-9][01][0-9][0-3][0-9]$' it shows correct. How can I fix the MM part, so that any number other than 1 to 12 determines incorrect format?

  3. In my fn_SingleFileLoad function, file name is passed through the input prompt. The input can not be more than one file name (i.e the program should send error or exit if someone provides more than one file. Example of an input: FileA FileB). Can someone please share some expertise on this part?

I would like to thank you all in advance for sharing your suggestions or help here.

#!/bin/ksh

###function for a complete data load  (i.e. all data files loading into database)
fn_AllFileLoad()
	{
           echo "enter the loadings date in YYYYMMDD format: "
           read dt
         
           # validates the date format
           echo ${dt}|grep -E -q '^201[1-9][01][0-9][0-3][0-9]$'
               if [[ $? != 0 ]]; then
                  echo "========================================================================="
         	  echo "Please enter the date in YYYYMMDD format. Terminating...No action done..."
         	  exit 1
               else
            
            	 .${HOME}/dbscripts/set_date.ksh $dt &
            	
 		for i in `cat ${HOME}/dbscripts/file.list`
                    do
            	    .${HOME}/dbscripts/data_load.ksh $i &
                    done
	            wait
         	fi
 
         }

fn_SingleFileLoad()
	 {
            echo "Enter the file name to load"
            read file
            echo "enter the loadings date in YYYYMMDD format: "
            read dt
        
            # validates the date format
            echo ${dt}|grep -E -q '^201[1-9][01][0-9][0-3][0-9]$'
                if [[ $? != 0 ]]; then
                   echo "========================================================================="
         	   echo "Please enter the date in YYYYMMDD format. Terminating...No action done..."
         	   exit 0
         	else
            
            	.${HOME}/dbscripts/set_date.ksh $dt &
            	.${HOME}/dbscripts/data_load.ksh $file & 
		fi
        }
 

#Main program starts here....

echo "Do you want to run a complete load? [Y/N]"
read yn
option=$yn
case $option in 
   Y|y ) fn_AllFileLoad;;
   N|n ) fn_SingleFileLoad;;
   * ) echo "Please answer only 'Y/y' for yes or 'N/n' for no.....terminating"
   exit;;
 esac

Try changing:

'^201[1-9][01][0-9][0-3][0-9]$'

to:

'^201[1-9](0[1-9]|1[0-2])(0[[1-9]|[12][0-9]|3[01])$'

to allow months 01-12 and days 01-31. This will still incorrectly allow some days in short months (e.g., 20140230 and 20140431), but it won't allow days 00 and 32-39 nor months 00 and 13-19.

And, assuming that .${HOME}/dbscripts/data_load.ksh uses quotes properly and yields an appropriate diagnostic if its file operand isn't found, change:

.${HOME}/dbscripts/data_load.ksh $file& 

to:

.${HOME}/dbscripts/data_load.ksh "$file" &

although I'm guessing that what you really wanted is:

. ${HOME}/dbscripts/data_load.ksh "$file" &

(Note the space added between the . and the $ .)

Thank you Don for your suggestions and reply. Yes, ill be run the files/directories in the each steps.

For #2 dateformat: It worked to resolve the month MM format. If I do not get solutions from anyone else for the shorter months, ill continue with the limitations.

to allow months 01-12 and days 01-31. This will still incorrectly allow some days in short months (e.g., 20140230 and 20140431), but it won't allow days 00 and 32-39 nor months 00 and 13-19.

For#3 - I need to allow only one file as an input and would like to through error & exit if multiple file names are entered. I am still not able figure that out. Any thoughts or suggestions will be great help.

Once you have the format assured, you can verify the date with the cal command.

            echo "enter the loadings date in YYYYMMDD format: "
            read dt
       
            # validates the date format
            echo "$dt" | grep -E -q '^201[1-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'
            if [[ $? -ne 0 ]]; then
                   echo "========================================================================="
                   echo "Please enter the date in YYYYMMDD format. Terminating...No action done..."
                   exit
            else
                   year=${dt%????}
                   md=${dt#????}
                   month=${md%??}
                   day=${md#??}
                   cal $month $year | grep -q -w $((day))
                   if [[ $? -ne 0 ]]; then
                        echo "This date does not exist. Terminating...No action done." 
                        exit
                   else
                        .${HOME}/dbscripts/set_date.ksh $dt &
                        .${HOME}/dbscripts/data_load.ksh $file & 
                   fi
            fi

NB I have removed the double [ from the ERE that would allow a [ character in the input.

1 Like

Thank you MadeInGermany! It works like a charm!

Now that I am close to my achievement, any idea how can I ensure #3 challenge.

For#3 - I need to allow only one file as an input and would like to through error & exit if multiple file names are entered. I am still not able figure that out. Any thoughts or suggestions will be great help.

echo "Enter the file name to load"
read file

Since spaces, tabs, and filename pattern special characters are all legal characters in a filename, finding out if a string matches more than one filename is not as easy as it sounds. But trying to figure out that you have more than one filename doesn't make any sense; why not just verify that what you were given names an existing file? If you just want to know if $file does not expand to the name of a file of any type:

if [ ! -e "$file" ]
then    printf "No such file: \"%s\"\n" "$file"
        exit 1
fi

If you only want regular files, change -e to -f .
----------
And, I forgot to mention that you also need to change:

                        .${HOME}/dbscripts/data_load.ksh $file &

to:

                        .${HOME}/dbscripts/data_load.ksh "$file" &
1 Like

Thank you Don for your help :slight_smile: