Solved

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

Posted on 2012-04-12
7
732 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Technology Partners: 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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
The viewer will learn how to count occurrences of each item in an array.

752 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