I have a script that accepts 3 input parameters (source directory, list file text, sleep time) and checks for the presence of files. If not there, script goes to sleep for certain amount of time provided by 3rd input.
list file text contains 1 entry but may contain more (file prefix):
MF_FF
#!/usr/bin/ksh
# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
UPLD_DIR_BASE=/data/data2/staging
while read line
do
source_file=`echo $line | awk '{print $1}'`
while [[ ! -e ${UPLD_DIR_BASE}/${source_directory}/${source_file}*[0-9]*.[dD][
aA][tT]* ]]
do
echo "sleeping..."
sleep ${sleep_time}
done
done < ${UPLDSH_DIR}/${src_list_files}
Globbing like that will also cause it to throw an error if there's more than one file that matches.
You can eliminate the use of awk by using read's built-in argument splitting features.
Without knowing the contents of that file I can't tell why it's not matching those files, but echoing the string when it doesn't will at least tell you what it is actually looking for.
#!/usr/bin/ksh
# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
UPLD_DIR_BASE=/data/data2/staging
# 'read' is capable of splitting by itself.
# the first parameter goes into LINE, everything else into G.
while read source_file G
do
# Shove it all into an array. If there's more than one that's not an error.
ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )
while [[ -z "${ARR[0]}" ]]
do
echo "No match for ${UPLD_DIR_BASE}/${source_directory}/${source_file}*[0-9]*.[dD][aA][tT]*"
sleep 10
ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )
done
done < "${UPLDSH_DIR}/${src_list_files}"
---------- Post updated at 12:44 PM ---------- Previous update was at 12:32 PM ----------
Hi Corona688.
I'm getting the following error when executing the script:
0403-057 Syntax error at line 29 : `(' is not expected.
This is the line in question:
ARR=( "${UPLD_DIR_BASE}/${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* ]] )
Please help.
Can you also explain after the sleep command, why we are capturing the entry into the ARR array again?
After I removed the ']]' from the ARR syntax, I'm still getting the same error -
0403-057 Syntax error at line 29 : `(' is not expected.
See my code below:
# Setup working variables
source_directory="$1"
src_list_files="$2"
sleep_time=$3
while read source_file G
do
# Insert all entries into an array. If there's more than one that's not an error.
ARR=( "${UPLD_DIR_BASE}${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* )
while [[ -z "${ARR[0]}" ]]
do
echo "No match for ${UPLD_DIR_BASE}${source_directory}/${source_file}*[0-9]*.[dD][aA][tT]*"
# Sleep for x amount of time
sleep $sleep_time
ARR=( "${UPLD_DIR_BASE}${source_directory}/${source_file}"*[0-9]*.[dD][aA][tT]* )
done
done < "${UPLDSH_DIR}/${src_list_files}"
Also I wasn't clear on my first posting, basically, script should check for existence of file(s) and if not there go to sleep for x mins.
After x mins, the script should terminate.
thanks for helping out a fellow Canadian/Torontonian.
Only 1 entry in the file with 2 columns. First column represents a file prefix (as we get filename with different dates on a daily basis).
What the intent of the script is suppose to do - is sometime we don't get files on time, so the script is suppose to delay our process a bit if the file(s) arrive late.
This has the side-effect of destroying your commandline parameters. Which is okay since you saved all the ones you cared about already; but just so you know.
Also, you'd need to do if [ -f "$1" ] , not if [ -f "${ARR[0]}" ]
---------- Post updated at 11:56 AM ---------- Previous update was at 11:52 AM ----------
The same limitation may exist for arrays anyway. Since we're only checking if the first element exists it's probably immaterial.