Posts tagged as '
function ' ...

Imagine you have a script, that will need deactivation. You do not want someone to find the deactivator easily (if you want to “lock” a script for example).

There are may ways to do this, one easy one is to create a script with name ” ” (space) and to execute it in your main script.

To create your space script :

cat >\
echo ” You should not start this script”
exit 0

and then, you add the following line somewhere, for example after the functions.

. ./\

The script will exit with the message at that place, and it is very difficult for the user to find the cause.

Another option can be to encrypt the script using crypt (you can install it if you do not know where it is) so that the user cannot use grep on all files to find the blocking one. You simply decrypt the file before running it (this can be done by your “space” script.

Oh, yeah, and a quick important note, on a related but different issue.

If you want to deactivate the following function :

function removeall {
rm -Rf .
}

DO NOT try to comment it out like this :

#function removeall {
rm -Rf .
}

Because this will simply remove everything every time we try to load your library !

Do it like this :

function removeall {
return 0
rm -Rf .
}

This is all for today, tomorrow we will talk about the misuse of the tr function.

So today, we’ll see how to pass a variable (and not the content of a variable) to a function.

The Problem

You need to create a function that updates one or multiple variables given as parameter. For example, let’s say you want to create a “to_upper” function, that will take a variable as parameter, then modify it.

If you do it like this :

to_upper $mytext

You will just receive the content of the variable. Then, what do you want to do with it ? Print it ? You can not return it to the variable, as you do not know it.

From there, you can execute everything, like SQL queries, and return them in the calling variable or in another variable… Of course, you still need to protect your function, checking that the given parameter is a name of a variable, and not the variable itself.