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:

./a.sh: 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 $?

UNIX/Linux shell and returning values from functions

Here is an interesting fact.

Let's assume you want to write a function myfun_initialize()

If the function is supposed to initialize some variables then DO NOT RETURN result of this function through the `function caller` (well, I have just made up this name.)

In other words...

right:
myfun_initialize parameters
[ "$var_initialize" = "ok" ] {
echo "Oops!"
return
}

wrong:
[ "`myfun_initialize parameters`" = "ok" ] {
echo "Oops!"
return
}


Why is it wrong? I wish I had a simple explanation, but if the myfun_initialize function initializes some external variables you would like to use later on, then by calling `myfun_initialize` you will initialize temporary variables, and these outside of the call will be unchanged!

Test it yourself:


Here is the text

MYVAR="nothing"

# $1 - value
myfun() {
MYVAR="$1"
}

echo "MYVAR before calling myfun is $MYVAR"
if [ "`myfun something`" = "" ]
then
echo "myfun returned an empty string"
echo "MYVAR after calling \`myfun something \` is $MYVAR"
else
echo "myfun hasn't returned an empty string?!"
echo "MYVAR after calling \`myfun something\` is $MYVAR"
fi

myfun something
echo "MYVAR after calling myfun something is $MYVAR"



So what is the solution?
Use a variable to return the result.
Example:


myfunc() {
....
var_myfunc="ok"
}

myfunc
[ "$myfunc" = "ok" ] && {
...
}

HP-UX - group permissions issue

Let's assume that there is a guy user name paul who's main group is developer.
His home directory belongs to paul:developer.

Now, let's assume my main group is users, and the developer group is my secondary one.

You would expect, probably, that I could do cd ~paul if only paul's home directory had g+rx attribute, right?

Well, on HP-UX (at least) it does not have to be like that. I don't know if this is by default or not but what I have found out on our HP-UX machine is that there are two files in the /etc dirtectory - group, and logingroups.

For sure you know the /etc/group file!
I bet you might not see the /etc/logingroups file, though.

Now, if you want to make HP-UX behaving like other machines, you can simply create a link:

ln -s /etc/logingroups /etc/group

After this change my user will be able to do cd ~paul.

Let's begin

The first but hopefuly not last post will, in my deep hope, open a new era for my knowledge storage.
So far, blogging wasn't my thing.
But hey, I have just realized that I can store my knowledge in here and have an access to it from various places.
If you, my dear reader, hope for big time secrets then you're up to big time disapointment.
Yes. The stuff I'm about to put in here is not about how to conquer the world (if I only knew how to spell it!)
It's a simple stuff regarding various products and solutions/workarounds I'm finding. A little bit of coding.