ERROR message in the Unix bash shell script. Any comments?

I am running my SAS program in within Unix bash script shell script and getting the following message =>
Syntax error: unexpected end of file

I checked everything in the script and looks like it's correct. I did some research online, but this error is very common and may mean different error problems.
Please let me know if you see anything in this script incorrect.
 

mydir=/start/dir
plog=/start/log
filename=sasprog1.log
search=ERROR
outfile=file1.txt
sas=/products/SAS/SASFoundation/9.2/sas

$sas $mydir/sasprog1.sas >$plog/$filename    
find $plog -type f -name $filename |while read file
  do
    RESULT= `grep $search $file`         
       if [["$RESULT" ==""]];
         then
            echo "Error(s) in $file: $RESULT" >> $outfile
     fi
  done
exit 0

Open in new window

labradorchikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
if [["$RESULT" ==""]];

You must put spaces around [[ and ]]

 if [[ "$RESULT" == "" ]] ;
0
labradorchikAuthor Commented:
Thank you, woolmilkporc! I just placed spaces around ]] and executed my script again. Same message come up. Anyhing else looks incorrect?
0
simon3270Commented:
The "==" should be "!=", and you need spaces between the != and the "" too.

You must *not* have a space after the = in the "RESULT=`grep..." line.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

woolmilkporcCommented:
Oh, seems I didn't look close enough!

As opposed to [[ ]] where spaces are required, spaces arund "=" when assigning variables are forbidden:

RESULT= `grep $search $file`

change to

RESULT=`grep $search $file`
0
nociSoftware EngineerCommented:
seems a bit overkill to use find for only one file?

if [ -f $file ]
then
....
fi

should do the trick to test for a single file...
or you may need some wildcarding added to the -name $filename part.

The above doesn't apply if /start/log is actualy a whole tree of directories with some or all containing a file named sasprog1.log
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Just a tip about your "I checked everything in the script", when debugging a script, you can see the verbose output of your script like this:

sh -xv <your script>

This will let you see at what line and when the error appears, more importantly: you can see how variables are assigned and how tests are evaluated.
0
labradorchikAuthor Commented:
After making all these changes, I am still expiriencing this
ERROR: unexpected end of file
Anyone else would like to comment on my code or this error before I close this question today?
0
nociSoftware EngineerCommented:
First I modified the blanks around some quoting..
Then I made it a bash script (which show tracing ) #! line.
Also $() is preferred to using ``
I have no SAS so i added some sas=echo line to prevent an error from that
+ an echo to add some message with ERROR ... Below is your version of the  script. [ upgraded]

#!/bin/bash -xv

mydir=/start/dir
plog=/start/log
filename=sasprog1.log
search=ERROR
outfile=file1.txt
sas=/products/SAS/SASFoundation/9.2/sas


$sas $mydir/sasprog1.sas >$plog/$filename

find $plog -type f -name $filename | while read file
  do
    RESULT=$(grep $search $file)
       if [[ "$RESULT" != "" ]]
       then
            echo "Error(s) in $file: $RESULT" >> $outfile
       fi
  done
exit 0

Open in new window


That script shows no error by itself... BUT
is fetches lines with ERROR from sasprog1.log, the first error will be shown after the Errors (s) in ...
But the other errors will start at the left margin...

My expectation is that sas tries to tell you it is missing input...?
Please verify sasprog1.log to verify the line is there.

And can you state what you expected from the script please
The error: ... line seems unfamiliar with shell scripting as most unix errors
are strict lowercase.
f.y.i. My [ private version ] script produced the following output :

#!/bin/bash -xv

mydir=~/start/dir
+ mydir=/start/dir
plog=~/start/log
+ plog=/start/log
filename=sasprog1.log
+ filename=sasprog1.log
search=ERROR
+ search=ERROR
outfile=file1.txt
+ outfile=file1.txt
#sas=/products/SAS/SASFoundation/9.2/sas
sas="echo"
+ sas=echo

$sas $mydir/sasprog1.sas >$plog/$filename
+ echo /start/dir/sasprog1.sas
echo "ERROR xxx" >>$plog/$filename
+ echo 'ERROR xxx'
find $plog -type f -name $filename | while read file
  do
    RESULT=$(grep $search $file)
       if [[ "$RESULT" != "" ]]
       then
            echo "Error(s) in $file: $RESULT" >> $outfile
       fi
  done
+ read file
+ find /start/log -type f -name sasprog1.log
grep $search $file
++ grep ERROR /start/log/sasprog1.log
+ RESULT='ERROR xxx'
+ [[ ERROR xxx != '' ]]
+ echo 'Error(s) in /start/log/sasprog1.log: ERROR xxx'
+ read file
exit 0
+ exit 0

Open in new window


File1.txt contained:
Error(s) in /home/nico/start/log/sasprog1.log: ERROR xxx

Open in new window

0
Gerwin Jansen, EE MVETopic Advisor Commented:
When using "bash -xv <your script>" - where do you get the error? Run your script like this and post the part where you get the error message. Thanks.
0
labradorchikAuthor Commented:
When I am using "bash -xv <my script>", - I get my error "ERROR: unexpected end of file" at the end of the script (the very last line with "exit 0").

Please note: when I run my sas program (sasprog1.sas) by itself, -  it works fine. I do not get any error messages and I can see that I am using correct input files and producing a correct output data.

I just noticed that my sas log file (sasprog1.log) is totaly empty. I am not sure why is this happening??    

Below is the part where my sas log file is created. I know that sas log is created automaticaly, but I am only doing this if for example a sas log will have a different name than my sas program, beacuse I still have to search my sas log for some errors.

$sas $mydir/sasprog1.sas >$plog/$filename

Open in new window


Is my script wrong?
0
nociSoftware EngineerCommented:
As i said before bash ( the script interpreter) doesn't use uppercase ERROR:  in some error message more like:
bash: script: end of file....   [ only lowercase ]
f.e. the next script
Create a file t.sh:
echo " 

Open in new window


(echo followed by a single double quote)
when run with    bash t.sh    will yield an error like:
t.sh: line 1: unexpected EOF while looking for matching `"'
t.sh: line 2: syntax error: unexpected end of file

So the error must be from SAS, what does SAS say when the inputscript cannot be found?
0
labradorchikAuthor Commented:
I am not sure what I am doing wrong, but I am still keep getting this error...
line 18: syntax error: unexpected end of file   - all in lower case
line 18 is the last line and this line is blank (next line after "exit 0").

I can run SAS program fine, - input and output files are correct and are showing up in my directories, but when I run my script with SAS program, I am still getting this error.

The only thing that I can tell is wrong is my SAS log file (sasprog1.log), because it is blank.

What else can it be??
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi, I'm suspecting that your 'find' finds files that you have no access to. Can you try modifying your 'find' line 13 like this:

find 2>/dev/null $plog -type f -name $filename | while read file

I tested your script like this and it works just fine:

#!/bin/bash

mydir=/start/dir
plog=/var/tmp
filename=sasprog1.log
search=ERROR
outfile=file1.txt

echo ERROR >$plog/$filename

find 2>/dev/null $plog -type f -name $filename | while read file
  do
    RESULT=$(grep $search $file)
       if [[ "$RESULT" != "" ]]
       then
            echo "Error(s) in $file: $RESULT" >> $outfile
       fi
  done
exit 0

Open in new window


For testing, I replaced your SAS program with an echo that creates the logfile with ERROR in it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nociSoftware EngineerCommented:
labradorchick, you script has changed from the original, as it now gives a different message, can you show the script along with the error message? Possibly the changes to the procedure added some new issues.
0
labradorchikAuthor Commented:
Thank you!!
This error message is not appearing anymore. I was missing the following:

export mydir
export plog

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.