#!bin/bash
let k=9
if [ $k -eq 9 ]
then
echo "Start"
Hello
echo "End"
else
echo "failed"
fi
function Hello() {
echo "hello !!!!"
}
I got the below error :
Start
Testtt.sh: line 6: Hello: command not found
End
Could any one help me how can I define the function after the main process. If I put the function definition before the main process it would work but I need the program structure as it is. If you could help to delcare any prototype to intimate the shell the I am going to use the function in later part of the program (as we declare prototype in C++).
Isn't possibe to decare prototype kind of statement as we do in C++ ?
Because the code I have posted should be as it is. I can not put the functions before calling.
Can we let the program know that we are going to use Hello function before funtion command executed.
C++ can do that because it's a compiled language. It can assume that "okay, there's a function named xyzzy, I can find its contents later". When the program gets linked together into an exe, it has to actually go find all these libraries to do so.
Shell language runs immediately with no compilation step. If those functions don't already exist, they don't exist. (A few shells might have special features here. These features do not exist in general.)
Even C++ can't manage if you don't tell it what the functions actually are though! At the very least you have to #include <something> before you use them, for it to even assume they exist. You can do something like #include in shell. Put your function, or a bunch of functions, in another file then do this:
. /path/to/script-name
Note the space between the dot and everything else, that's essential.
script-name will be run in your current shell, effectively loading the functions in it.