Operating System - HP-UX
1834395 Members
1850 Online
110066 Solutions
New Discussion

Re: Status of Previous command ?

 
SOLVED
Go to solution
rveri
Super Advisor

Status of Previous command ?

How to see if the prevous command was successful or not.

ex:

$ ls -l abc
abc not found
$

but abc not exit. So its unsuccesful.

Hence how to find out that , last command [ ls -l abc ] wasn't successful. Any command is there.

Thanks ,
Regards,
Veri.

8 REPLIES 8
RAC_1
Honored Contributor

Re: Status of Previous command ?

echo $?

man sh-posix
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: Status of Previous command ?

ls -l abc
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Command Ok"
else
echo "Failed; Status = ${STAT}"
fi

${?} is the return value of the last executed comand. It's a good idea to capture it in another variable because the next comand will alter ${?}. By convention, 0 indicates a good result although there are a few exceptions.
If it ain't broke, I can fix that.
TwoProc
Honored Contributor

Re: Status of Previous command ?

ls -l abc
if [ $? -ne 0 ]; then
echo abc not found
fi

or,
ls -l abc||echo abc not found.

We are the people our parents warned us about --Jimmy Buffett
Geoff Wild
Honored Contributor

Re: Status of Previous command ?

And another way:

for i in `sed -n '/^LV/p' $CNTL |awk -F\" '{print $4}'`
do
mount |grep $i >/dev/null

if [ $? -ne 0 ]
then
echo "$i is not mounted!!!"
fi
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
rveri
Super Advisor

Re: Status of Previous command ?

Hi , Anil, Clay , John and Geof ,

Thanks for all the quick reply.

echo $? and the next two script works fine.

But I could not understand the last script by Geof.

Thanks to all,

Regards,
Veri.
---------

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Status of Previous command ?

I should also add that for this particular task, calling ls and checking the result is a bit cumbersome. The better approach is to use the shell's builtin test command using the [[ ]]'s (double brackets). This way you don't have stderr's outpiut cluttering up your terminal -- although you can redirect stderr to get rid of that.

FNAME="abc"
if [[ -e ${FNAME} ]]
then
echo "File ${FNAME}" exists"
else
echo "File ${FNAME} not found"
fi

You can also do things like:
if [[ -w ${FNAME} && -x ${FNAME} ]]
then
echo "File ${FNAME} exists and is writable and executable"
fi
If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: Status of Previous command ?

Okay:


#!/bin/sh
#
# script to check all file systems are mounted
# gwild

CNTL=/etc/cmcluster/IPRDBCI/iprdbci.cntl

for i in `sed -n '/^LV/p' $CNTL |awk -F\" '{print $4}'`
do
# ok - here I check to see if a filesystem $i is mounted
# don't want output - so I send it to /dev/null
mount |grep $i >/dev/null

# however - $? still has the return code
# so, I check to see if it is not equal to 0 (successs)
if [ $? -ne 0 ]

# if it isn't then echo the fact that it isn't mounted
then
echo "$i is not mounted!!!"
fi
done

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
rveri
Super Advisor

Re: Status of Previous command ?

Thanks Stephenson , for further explanations. echo $? is applicable for any command.

ex: # ps -ef | grep pmon
# userdel abc

and thanks for showing the built in test for file testing , if exists , writable or executable etc.

Thanks so much. Thanks Geof and all who replied.

Points Assigned **

Regards,
Veri.