Solved

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

Posted on 2012-04-12
7
730 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

756 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