Solved

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

Posted on 2012-03-29
7
832 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

744 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

13 Experts available now in Live!

Get 1:1 Help Now