syntax error at line and invalid reference list for the KSH script syntax

72 Views Asked by At

cd $file_dir

what I am trying to do with this is to do an attachment of a file to from a directory and if the attachment is not found there then look into the other directory and do the attachment

if [ -f "${CLAIM_FILE_PREFIX}"*$(date +"%Y%m%d")*.html ] then
  for FILE in ${CLAIM_FILE_PREFIX}*$(date +"%Y%m%d")*.html do
    ATTACHMENT_ARGS="$ATTACHMENT_ARGS $FILE"
    echo "$ATTACHMENT_ARGS"
  done
else
  cd $ARCHIVE_DIR
  for FILE in ${CLAIM_FILE_PREFIX}*.html do
   ATTACHMENT_ARGS="$ATTACHMENT_ARGS $FILE"
    echo "$ATTACHMENT_ARGS"
   done
fi

what I am trying to do with this is to do an attachment of a file to from a directory and if the attachment is not found there then look into the other directory and do the attachment

Please if someone could help with this syntax error which I am facing

1

There are 1 best solutions below

0
glenn jackman On

When you cuddle then and do on the same line as if and for, you need a semicolon:

if ... ; then
   for x in y; do

Also, this is not right (beyond the above):

if [ -f "${CLAIM_FILE_PREFIX}"*$(date +"%Y%m%d")*.html ] then

-f is used to test a single filename, not a list of them.

Create an array of the files, using the ~(N) prefix so that the array is empty if the glob pattern matches nothing:

typeset -a files=( ~(N)"${CLAIM_FILE_PREFIX}"*$(date +"%Y%m%d")*.html )

Then, your if condition will examine the number of files:

today=$(printf '%(%Y%m%d)T' now)
typeset -a files=( ~(N)"${CLAIM_FILE_PREFIX}"*"${today}"*.html )

if [ "${#files[@]}" -gt 0 ]; then
  for file in "${files[@]}"; do
    ATTACHMENT_ARGS+=" $file"
    echo "$ATTACHMENT_ARGS"
  done
else
  cd "$ARCHIVE_DIR"
  for file in ~(N)"${CLAIM_FILE_PREFIX}"*.html; do
   ATTACHMENT_ARGS+=" $file"
    echo "$ATTACHMENT_ARGS"
   done
fi

Notes