Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-12
7
Medium Priority
?
738 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows 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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

722 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