Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-30
15
Medium Priority
?
894 Views
Last Modified: 2012-04-05
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

0
Comment
Question by:labradorchik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
ID: 37789071
if [["$RESULT" ==""]];

You must put spaces around [[ and ]]

 if [[ "$RESULT" == "" ]] ;
0
 

Author Comment

by:labradorchik
ID: 37789193
Thank you, woolmilkporc! I just placed spaces around ]] and executed my script again. Same message come up. Anyhing else looks incorrect?
0
 
LVL 20

Assisted Solution

by:simon3270
simon3270 earned 200 total points
ID: 37789267
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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37789278
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
 
LVL 40

Expert Comment

by:noci
ID: 37789490
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 37791800
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
 

Author Comment

by:labradorchik
ID: 37796049
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
 
LVL 40

Assisted Solution

by:noci
noci earned 800 total points
ID: 37796797
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 37797799
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
 

Author Comment

by:labradorchik
ID: 37803012
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
 
LVL 40

Expert Comment

by:noci
ID: 37803177
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
 

Author Comment

by:labradorchik
ID: 37808216
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
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 800 total points
ID: 37808602
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
 
LVL 40

Expert Comment

by:noci
ID: 37808653
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
 

Author Comment

by:labradorchik
ID: 37814304
Thank you!!
This error message is not appearing anymore. I was missing the following:

export mydir
export plog

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question