How to capture status code and echo a message

Im trying to execute application and its return code is below

IF Status code=o
echo "........"

else Staus Code =-2 DJRE

then echo "......"

Can any one help me how to handle the status code and echo some message.

I guess you call your function like this:

result=`Your_function`

# From your question it is a character 'o', if so then use the below condition
if [ "$result" = "o" ]
Just in case you meant the number 0 (zero) then use the below condition
if [ $result -eq 0 ]
then
echo "....."
else
echo "....."
fi

BTW, can you tell me how to post a new question in this forum. I appear too dumb as I spent already 15 mins finding the place for submitting questions.

Nanu_Manju: when you enter a forum, like Shell Programming and Scripting - The UNIX Forums, there's a "New Thread" button above the list of "Threads in Forum", and one below the list too.

Thanks, I hate when I appear dumb. You have a good day

ls -lrt *20080430 | grep -v | wc -l

ls: 0653-341 The file *20080430 does not exist.
0

We need to skip File does not exist like like total using grep command

can any one help how to acheive this

The output will be zero lines if there are no files, so wouldn't it suffice just to discard the error message?

ls -lrt *20080430 2>/dev/null | wc -l

PS. Please start a new thread for a new question.

HI,

Returncode = `cd $Path;ls -lrt *$fdate 2>/dev/null | wc -l`
if [ "$Returncode" = 0 ]
then
echo '.......'
else

cd $Path;ls -lrt *$fdate 2>/dev/null

but it showing the error Returncode not found
help me please

what's the difference between this:

result=`Your_function`

and this

result = `Your_function`

???

Thanks lot it worked fine.
my requirement is returncode !=0
then list the all files in a directory.

But the files are not listing
Help me.Im new to unix

Returncode = `cd $Path;ls -lrt *$fdate 2>/dev/null | wc -l`
if [ "$Returncode" = 0 ]
then
echo '.......'
else
`cd $Path;ls -lrt *$fdate 2>/dev/null`

Im trying to execute the above code but its not Displaying the files.

Buddy,

You can also use "$?" inorder to check the status code for the last executed command.

Pl dont direct the result to some dummy terminal(/dev/null). We generally do this when we dont need the output and even we dont want that output to be displayed on the screen. Please use the below code:

Returncode = `cd $Path;ls -lrt *$fdate 2>/dev/null | wc -l`
if [ "$Returncode" = 0 ]
then
echo '.......'
else

cd $Path;ls -lrt *$fdate

Now it should display the list of files.

I beleive to discard the error codes alone 2>/dev/null
but even output will display

Vicky Narayan: in this case, $? will be the result code from wc -l, not from ls! But the idea to capture the result with backticks and not echo anything if it's zero is workable.

You still need to remove the spaces around the equals sign, it's a syntax error to leave them out.

However, if you really do need to distinguish between "error" and "no output" (wc -l will be zero in both cases). you will need to decompose it somehow. Maybe something like this:

postprocess="wc -l"
if ! ls *$frdate >/dev/null; then
  postprocess=false
fi | $postprocess

If you need to do something more if ls fails, just add stuff to the "then" clause. You can also add an "else" clause to only do things when ls is successful. But notice that output from the whole if construction gets piped to wc -l or false (hope that works for you; not sure how portable that is, but this is a proof of concept anyway; change it to "cat >/dev/null" or something if you can't make it work with false.)

Wait a minute. So you don't really care about wc -l at all?

ls files 2>/dev/null will list the files if they are there, and not display a warning if they are not, and set a non-zero exit code if it failed. If that's all you want then there you are.