0

I have a script and within it I call a function. How can I use the exit status from the function to print a message, without incorporating the message inside the function?

I am supposed to write a script which has:

Your script should contain a function increasingNos that uses three parameters. All three parameters should be integers. The function is a "success" (with exit status 0) if there are exactly three parameters and they are numbers in increasing order. The function should have an exit status of 1 if there are three parameters but they are not in increasing order. The function should should have an exit status of 2 if there are fewer or more than 3 parameters.

and...

you should print an appropriate message to the standard output after calling increasingNos with parameters 17 5 23 to say whether or not there were three parameters and whether or not they were numbers in increasing order. Use an if conditional and the exit status on your function call to do this. This if conditional may not be in the function increasingNos.

This is what I have come up; whenever I run the script, it exits when the function call hits an exit status. How can I execute the rest of the script?

increasingNos(){
   if [ $# -ne 3 ];then
      exit 2
   fi
   if [ $1 -ge $2 ] || [ $2 -ge $3 ];then
      exit 1
   else
      exit 0
   fi
}
increasingNos 17 5 23
if [ $? -eq 2 ];then
   echo "You did not supply exactly 3 integer parameters!"
fi
if [ $? -eq 1 ];then
   echo "Your parameters were not input in increasing order!"
fi
if [ $? -eq 0 ];then
   echo "Congrats, you supplied 3 integers in increasing order!"
fi 

2 Answers 2

3

Use return instead of exit and save the value of $? in a variable, because it will change after the first test.

This works:

increasingNos(){
   if [ $# -ne 3 ];then
      return 2
   fi
   if [ $1 -ge $2 ] || [ $2 -ge $3 ];then
      return 1
   else
      return 0
   fi
}
increasingNos 17 5 23
stat=$?
if [ $stat -eq 2 ];then
   echo "You did not supply exactly 3 integer parameters!"
fi
if [ $stat -eq 1 ];then
   echo "Your parameters were not input in increasing order!"
fi
if [ $stat -eq 0 ];then
   echo "Congrats, you supplied 3 integers in increasing order!"
fi 
Sign up to request clarification or add additional context in comments.

3 Comments

Cool; a simpler way of dealing with the problem of $? getting reset is to use case "$?" in ... esac in lieu of the indiv. if statements.
I was just trying to stay as close as possible to the original script from the question and that there was an additional problem. Thanks for pointing out case "$?" in ... esac.
Got it. The problem of $? getting reset was indeed worth pointing out.
2

You need to use return rather than exit in your functions.

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.