0

Ive had experiences running these statements in the past but not loads.

Im trying to run one that will look in the directory and if the file is a .txt then send it to testdir_2 else if it is a .csv file then send to testdir_3. Else if it is anything else echo "error".

What seems to be happening is when the code doesn't find the .txt file it will error out instead of continuing with the statement.

Im also getting a "else unexpected" message.

I may just be missing something obvious but I cannot seem to work out what is wrong. Thank you in advance.

#!/bin/ksh
#set -x
#
FILEDIR=/export/home/jaqst/training/testdir_1
DIR1=/export/home/jaqst/training/testdir_2
DIR2=/export/home/jaqst/training/testdir_3
TXT=*.txt
CSV=*.csv
#
# Main processing starts here
#
echo `date +%d\ %b\ %H:%M` "Job started"
#
# Go to directory
#
cd ${FILEDIR}
if [[ $? -ne 0 ]]
   then echo `date +%d\ %b\ %H:%M` "Failed to cd into ${FILEDIR}"
   exit 9
fi
#
# Check files and move to appropriate directories
#
if [ ! -e = ${TXT} ];
   then
   mv ${TXT} /export/home/jaqst/training/testdir_2
elif [ ! -e = ${CSV} ];
   then
   mv ${CSV} /export/home/jaqst/training/testdir_3
else
   echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
   exit 9
fi
#
echo `date +%d\ %b\ %H:%M` "Job completed"
exit 0
STAKD
  • 15
  • 4
  • _Im also getting a "else unexpected" message._ : In this case fix the syntax first. In ksh, `[` is an syntacic element and `-e` has a specific meaning to it. You have to explicitly quote it to treat it as an operand for comparision, i.e. `'-e'`. – user1934428 Jun 29 '23 at 13:07
  • I think OP wants `-e` for it's specific meaning. In which case, the `=` must be removed, and `-e` only operates on one file at a time and not on a pattern. – glenn jackman Jun 29 '23 at 13:43

2 Answers2

0

Use the subshell, update the codes in this way:

if [[ -n $(ls ${TXT}) ]];
   then
   mv ${TXT} /export/home/jaqst/training/testdir_2
elif [[ -n $(ls ${CSV}) ]];
   then
   mv ${CSV} /export/home/jaqst/training/testdir_3
else
   echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
   exit 9
fi
Xiang
  • 489
  • 5
  • 11
  • [don't parse ls](https://mywiki.wooledge.org/ParsingLs) – glenn jackman Jun 29 '23 at 12:34
  • @glennjackman, thanks for you pointing out the potential problems, regarding the filename's whitespace and non-posix compatible issues. I've seen that link many times in years ago since I learned the bash programming from [Advanced Bash Programming](https://tldp.org/LDP/abs/html/), however remember the prerequisite, s/he is using `ksh` and `TXT=*.txt` to specify files, this is the simplest solution for the original codes. – Xiang Jun 30 '23 at 01:56
0

A different approach: loop over the files and pattern match on the filename

count=0
for file in *; do
    case "$file" in
        *.txt)
            mv "$file" "$DIR1"
            ((++count))
            ;;
        *.csv)
            mv "$file" "$DIR2"
            ((++count))
            ;;
    esac
done
if [[ $count -eq 0 ]]; then
    echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
    exit 9
fi

By the way, ksh's printf can do date formatting, no need to call out to date:

log() {
    printf '%(%d %b %H:%M)T - %s\n' now "$*"
}
log Hello World    # => "29 Jun 08:41 - Hello World"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352