Solved

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

Posted on 2012-03-30
15
863 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
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 50 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 19

Assisted Solution

by:simon3270
simon3270 earned 50 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
 
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 39

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 37

Expert Comment

by:Gerwin Jansen
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 39

Assisted Solution

by:noci
noci earned 200 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 37

Expert Comment

by:Gerwin Jansen
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 39

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 37

Accepted Solution

by:
Gerwin Jansen earned 200 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 39

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now