Help Needed with File Checker Korn Shell Script

I am attempting to write a korn shell script that does the following, but I am getting errors. I'm new to korn shell scripting and am not sure what I am doing wrong. Please help with example scripts. Thanks.

1) check for day of the week
2) if day of the week is Monday then check for the existence of three files
3) if all three files are not found then loop infinitely until all three are found
4) when found then execute a .sas program
5) else if the day if the week is not Monday then check for the existence of two files
6) if the two files are not found then loop infinitely until both files are found
7) when both files are found execute a .sas program

Here's what I have thus far:

#!/usr/bin/ksh
. ~/.profile

DAYOFWEEK=$(date +"%u")
echo DAYOFWEEK: $DAYOFWEEK

if ["$DAYOFWEEK" == 1];
then do
        if [-f /files/are/here/FILE1.txt
            -f /files/are/here/FILE2.txt
            -f /files/are/here/FILE3.txt]; then
sas /files/are/here/process.sas
sleep 10

else if ["$DAYOFWEEK" !==1];
then do
          if [-f /files/are/here/FILE2.txt
              -f /files/are/here/FILE3.txt]; then
sas /files/are/here/process.sas
sleep 10
done

Welcome to the forums!
A number of things is wrong (to start with):

  1. a logical construct [ logical ] should have trailing/leading spaces, e.g.: if [ "$DAYOFWEEK" == 1 ]
  2. numerical comparison within the [] should be done with -eq/-ge/-le/etc... , eg: if [ "$DAYOFWEEK" -eq 1 ]
  3. the grouping of the logicals within the if , should be done as follows, e.g. if [ logical1 ] && [ logical2 ]
  4. you're missing a 'closing' fi for your leading if - I bet your trailing done should be an fi

On top of vgersh99's comments to your syntax, you also might want to consider a slightly different approach to the logics, which translates to

  • if FILE2 and FILE3 exist, and
    ...- the weekday is not Monday
    ...- weekday is Monday and FILE1 exists

, execute the sas program.

How about

if [ -f ./file2 -a  -f ./file3 -a \( -f ./file1 -o $DAYOFWEEK -ne 1 \) ]  
  then  sas 1 /files/are/here/process.sas
fi

Be aware that the -a and -o operators are deprecated and should be replaced by && and || (but I'm not sure how to do the parentheses grouping).

Hi.

You can find some of your errors by using shellcheck :

In <yourfile> line 7:
if ["$DAYOFWEEK" == 1];
^-- SC1009: The mentioned parser error was in this if expression.
   ^-- SC1073: Couldn't parse this test expression.
    ^-- SC1035: You need spaces after the opening [ and before the closing ].
                      ^-- SC1020: You need a space before the ].
                      ^-- SC1072: Unexpected ";". Fix any mentioned problems and try again.

It will be an iterative process to avoid cascades of error messages.

Some details for shellcheck :

shellcheck      analyse shell scripts (man)
Path    : /usr/bin/shellcheck
Version : 0.3.4
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYS ...)
Help    : probably available with -h
Repo    : Debian 8.11 (jessie) 
Home    : http://hackage.haskell.org/package/ShellCheck (pm)

Best wishes ... cheers, drl

Can be done as a { } group. (Note: there must be a semicolon or newline before the closing brace! And of cause spaces next to the [ ] { } )

if [ -f ./file2 ] && [  -f ./file3 ] && { [ -f ./file1 ] || [ $DAYOFWEEK -ne 1  ]; }
  then  sas 1 /files/are/here/process.sas
fi
4 Likes