5

I have the following unix shell script, in which i have two integer variables namely a and b.

If a is greater then or equal to b then shell script should exit with returning 0.

Else it should exit with returning 1.

My try:

Script: ConditionTest.sh

#!/bin/sh

a=10
b=20

if [ $a -ge $b ]
then
        exit 0
else
        exit 1
fi
    ....
    ....
    ....

Running Script:

$ ./ConditionTest.sh
$ 

Note: I am not getting any return value after executing the file.

2
  • 1
    Exit value is stored in $?. Do echo $? after running the script. If you want to see the value as part of the script output then print it: echo 0; exit 0 and echo 1; exit 1. Commented Jun 20, 2016 at 11:57
  • 1
    "getting any return value" means "no printed value" for you? Commented Jun 20, 2016 at 11:57

3 Answers 3

7

The shell puts the exit status of the last command in the variable ?. You could simply inspect it:

mycommand
echo $?

... or you could use it to do something else depending on its value:

mycommand && echo "ok" || echo "failed"

or alternatively, and slightly more readable:

if mycommand; then
  # exit with 0
  echo "ok"
else
  # exit with non-zero
  echo "failed"
if
Sign up to request clarification or add additional context in comments.

2 Comments

I'm being picky, $? expands to the value, not the variable. The variable is called ?.
@cdarke Right you are! I will amend my answer.
4

Your script looks fine; you did everything right.

#!/bin/sh

a=10
b=20

if [ $a -ge $b ]
then
        exit 0
else
        exit 1
fi

So here's where we run it and check the return value:

$ sh test.sh
$ echo $?
1
$

10 is not greater than or equal to 20.

Another way to test it would be like this:

$ sh test.sh && echo "succeeded" || echo "failed"
failed

As noted in the comments, you should also quote your variables, always:

if [ $a -ge $b ]

Should be:

if [ "$a" -ge "$b" ]

2 Comments

Minor improvement: ALWAYS use doublequotes around variables. "$a" -ge "$b". Otherwide you will get mean syntax errors which are hard to spot in the code, when a variable is empty.
Noted in an edit (and agreed; I always do this). I had originally just copied and pasted OP's code.
1

To add to the previous answers, the key idea you should understand is that every program provides a number when exiting. That number is used as a way to report if the command has completed its operation successfully, and if not, what type of error has occurred.

Like mentioned, the exit code of the last command executed can be accessed with $?.

The reason nothing was printed by your script, is that your script returned 1, but the exit code of a command is not printed. (This is analogous to calling a function, you get a return value from the function but it's not printed)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.