Solved

In the Unix bash shell script, how to use $? to check the SAS execution result?

Posted on 2012-04-12
7
716 Views
Last Modified: 2012-04-13
How can I test my SAS program execution for success or failed?
This is how I am running SAS program 'mytest.sas'  in the Unix bash shell script 'test.sh' and trying to check for success or failer.

mydir=/user/name
progstat=5

sas=/products/SAS/SASFoundation/9.2/sas
$sas mytest
RESULT=$(echo $"$?") 
echo RESULT= "$RESULT"
[ "$RESULT" ] && progstat=0 || progstat=1
echo the value of progstat in $mydir/test1 = $progstat 

if [ "progstat" ] != 0
 then 
    echo "----Program Failed----"
  else
     echo"----Program Success----"
fi  

Open in new window


I am getting the following error:
+ '[' 1 ']' '!=' 0
test1.sh: line 11: [: missing `]'

Any comments or suggestions will be very appreciated!
0
Comment
Question by:labradorchik
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37839704
if [ $progstat  -ne 0 ] ; then ...
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37839733
Do you want RESULT to contain the returncode? If so, just do

RESULT=$?
0
 

Author Comment

by:labradorchik
ID: 37840213
Thank you, woolmilkporc!
Just run script again with the changes from your suggestions.

This is what I am getting when there is an error in the SAS code:
+ '[' 2 =0 ']'
test1.sh: line 8: [: 2: unary operator expected
+ progstat=2
+ '[' 2 -ne 2 ']'
+ echo '----Program Success----'

This is what I am getting when there is no error in the SAS code:
+ '[' 0 =0 ']'
test1.sh: line 8: [: 0: unary operator expected
+ progstat=2
+ '[' 2 -ne 2 ']'
+ echo '----Program Success----'


What am I doing wrong?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37840255
That's because of your setting of RESULT.

What are you trying to achieve with this bizarre construct

RESULT=$(echo $"$?")

RESULT will contain something like "$2"  if the returncode was 2. Is this desired?

Why not just:

mydir=/user/name
sas=/products/SAS/SASFoundation/9.2/sas

$sas mytest
RESULT=$?

echo the value of the returncode in $mydir/test1 = $RESULT

if [ $RESULT -ne 0 ]
 then
    echo "----Program Failed----"
  else
     echo"----Program Success----"
fi
0
 

Author Comment

by:labradorchik
ID: 37840263
Script only works, when I change from progstat to RESULT and from -ne to -eq on line 11:

mydir=/user/name
progstat=5

sas=/products/SAS/SASFoundation/9.2/sas
$sas mytest
RESULT=$(echo $"$?") 
echo RESULT= "$RESULT"
[ "$RESULT" ] && progstat=0 || progstat=1
echo the value of progstat in $mydir/test1 = $progstat 

if [ $RESULT -eq 0 ]
 then 
    echo "----Program Failed----"
  else
     echo"----Program Success----"
fi  

Open in new window


But in this case I do not use progstat at all... I need to use progstat as in my origional script.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37840301
I really think I don't understand what you're after.

But if you insist in progstat:



mydir=/user/name
sas=/products/SAS/SASFoundation/9.2/sas

$sas mytest
RESULT=$?

echo the value of the returncode in $mydir/test1 = $RESULT

[ $RESULT -ne 0 ] && progstat=1 || progstat=0

echo the value of progstat in $mydir/test1 = $progstat

if [ $progstat -ne 0 ]
 then
    echo "----Program Failed----"
  else
     echo"----Program Success----"
fi
0
 

Author Comment

by:labradorchik
ID: 37844022
Works as a charm!! :)
Thank you very much for your help!!

Note: progstat variable will be later initialized in the SAS program as well, so that's the only reason I need to have progstat in this code.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

746 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