Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of David
David
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of labradorchik

ASKER

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

Anyone else would like to comment on the obove Unix script?
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.
Avatar of Tintin
Tintin

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

Great!! Thank you very much for your comments, dlethe!!