Solved

Why Unix bash shell script does not run my SAS program?

Posted on 2012-03-29
7
836 Views
Last Modified: 2012-03-29
In my Unix bash shell script I am trying to run my SAS program "sasprog1.sas" and then search within the Unix directory for a file "sasprog1.LOG" and then checking for an ERROR within "sasprog1.LOG" file and then open another file ("file1.txt") and then writing some messages to "file1.txt" file?

Please note: "file1.txt" and "sasprog1.LOG" may or may not currently exist in the Unix directory.

Please see why my Unix bash shell script does not run/execute my SAS program. What am I doing wrong here?
Any examples or comments will be very appriciated!


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     # is this a proper way to run a SAS program and then create SAS log??
find $plog -type f -name $filename |while read file
  do
    RESULT=$(grep $search $file)         # is this correct?      
       if [[ ! -z $RESULT ]]
         then
            echo "Error(s) in $file: $RESULT" >> $outfile
     fi
  done
exit

Open in new window


Curently my script does not even run/execute sasprog1.sas
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
  • 3
  • 3
7 Comments
 
LVL 47

Accepted Solution

by:
dlethe earned 500 total points
ID: 37783347
Assuming you would ordinarily execute:
/products/SAS/SASFoundation/9.2/sas  > /star/log/sasprog1.log

then you would enter:

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


Then the RESULT line nedes to be

RESULT=`grep $search $file`
if [ "$RESULT" == "" ] ; then
    echo error ...
fi



You should also add a PATH line at beginning so it can find grep
PATH=/usr/bin:/bin:/sbin    # make sure the path where grep is installed on your host is in the list
0
 
LVL 47

Expert Comment

by:dlethe
ID: 37783362
you can add a "set -x"  at beginning of shell script and it will give a debug dump of exactly what it is doing as it executes.  this will tell you values of all variables as it runs.  Best way to debug a script
0
 

Author Comment

by:labradorchik
ID: 37783430
Thank you very much for your comments, dlethe!!
What do you mean by: "Assuming you would ordinarily execute:
/products/SAS/SASFoundation/9.2/sas  > /star/log/sasprog1.log

Open in new window

"?

I am assignning and openning SAS software with the following line:
 
sas=/products/SAS/SASFoundation/9.2/sas

Open in new window

and then will be running SAS program and creating my sasprog1.log file with the following line:
 
$sas $mydir/sasprog1.sas> $plog/$filename   

Open in new window



So, is this correct?

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

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:labradorchik
ID: 37783886
Anyone else would like to comment on the obove Unix script?
0
 
LVL 47

Expert Comment

by:dlethe
ID: 37784110
no,  you need `   not  the single quote.

many ways to do the if, depending on exact version of shell, but most portable would be

if [ "$RESULT" == "" ]
then

fi
Note the whitespace

I  have no idea if the find $plog ... line will do what you want, that is a function of the output syntax of the sas command.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37784327
There's no need for the find, assuming you don't have multiple sasprog1.log in sub-directories.

That being the case, you can simply make the script

#!/bin/bash
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

result=$(grep $search $plog/$filename) && echo "Error(s) in $plog/$filename: $result" >>$outfile

Open in new window

0
 

Author Comment

by:labradorchik
ID: 37784353
Great!! Thank you very much for your comments, dlethe!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

726 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