Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a command has executed? 1

Status
Not open for further replies.

SpongeBob1

Programmer
Mar 1, 2002
49
US
Hi All,

Is there a way to check to see if a command has been run. ie, I am executing something in cshell, and wish to see if it has ran, if it hasn't I wish to catch this as an error. Any Ideas?
 
Bob:

I don't know if I'm interpreting exactly what you want, but it sounds like you want to know if a unix command/program successfully executes.

Most well-designed unix program set an exit code whether it was successful:

your_external_program
if [ "$?" -eq 0 ]; then
echo "successful"
else
echo "unsuccessful"
fi

The exit code is 0 if it was successful and non-zero if not. I'm sorry, but I'm not a "C" shell expert, but I would think the exit code should still work.

Regards,

Ed
 
Thanks Bob,

How would I go about setting an exit code when my script ran?

 
The exit code is a paramter for exit. For example:
Code:
if [ $NumErr -eq 0 ]; then
    exit 0 # Script passed
else
    exit 1 # Script failed
fi
 
Hi,
I don't understand why people insist on giving answers to CSH questions with snippets that only work in SH or KSH.

In CSH the exit status is return in the variable

$status ( $? is for sh and ksh )

I typically put the $status into a local variable which can be used further down in the script. $status is overwritten by every command with the result of the that command so moving it to a local just prevents it from being lost.

Therefore the example above in CSH would be.....

your_external_program
set stat = $status
if ( $stat == 0 ) then
echo "program worked"
else
echo "program aborted with exit code --- $stat"
endif




then inside your_external_program

#! /bin/csh

@ NumErrs = 0

# do whatever and if it fails increment numerrs
@ NumErrs ++
.
.

# Ok set the exit criteria based upon the number of
# errors ecountered during the script.

if ( $NumErr == 0 ) then
# Script passed
exit 0
else
# Script failed
exit 1
endif



Hope This helps.....


 
Thanks everybody for their replies. One question, when running the example from tdatgod, I get the error

@: not found

Is there another way of running this script? Or can I assign the NumErrors another way?

 



Make sure the

#!/bin/csh

is the VERY FIRST line of the file. This tells the system that you what it to use /bin/csh to execute the script. Without it it may try to use /bin/sh or /bin/ksh to execute it.

What OS are you on?

@

is the CSH numeric processor. It only handles numeric parameters

@ x ++

will increment $a if it is numeric or it will CRASH if $a contains a non numeric value.

set x = "a"
@ x ++
@: Expression syntax

set x = "5"
@ x ++
echo $x
6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top