Wednesday, April 9, 2008

Returning error code from a script in Linux

Most people moving to Linux are getting surprised by the fact that a simple operation "return 1" can cause an error:

./ line 1: return: can only `return' from a function or sourced script

Not all Linuxes return this problem, however, it's enough anoying if you want to keep the script for all the platforms.

There is a simple solution for this, though!

# $1 - error code
fun_return() {
return $1

fun_return 1

You can check this out by yourself. Write a sctipt with the above example, run it and check the value of $?

No comments: