Solved

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

Posted on 2012-04-12
7
726 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert MSI to MSM 1 59
What is assert.deepEqual? 4 46
Interactive Script in Scheduled Task not running 8 28
centos commands 6 51
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 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