Function prototype declaration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function prototype declaration
# 1  
Old 10-22-2013
Function prototype declaration

Hi All,

I have the script as below
Code:
#!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++).

Thanks in advance!!!
# 2  
Old 10-22-2013
Quote:
Originally Posted by Balasankar
Hi All,

I have the script as below
Code:
#!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++).

Thanks in advance!!!


Function should be defined before calling. as you might aware commands are executed sequentially.

modify like this

Code:
#!bin/bash

function Hello(){
        echo "hello "
            }

let k=9
if [ $k -eq 9 ]
then
    echo "Start"
    Hello
    echo "End"
else
    echo "failed"
fi

# 3  
Old 10-22-2013
Akshay thanks for quick reply.

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.

Thanks!!!
# 4  
Old 10-22-2013
Quote:
Originally Posted by Balasankar
Because the code I have posted should be as it is. I can not put the functions before calling.
Why not?

Quote:
Originally Posted by Balasankar
Can we let the program know that we are going to use Hello function before funtion command executed.
No. sh parses and executes each command as it's read.

Regards,
Alister
# 5  
Old 10-22-2013
Quote:
Originally Posted by Balasankar
Isn't possibe to decare prototype kind of statement as we do in C++ ?
Nope.

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:

Code:
. /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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. What is on Your Mind?

Major Changes in New UserCP (v0.63) Prototype

Regarding the latest version of the UserCP prototype (version 0.63) I have made a lot of major changes, including Added a "Posts Timeline" table for the recent posts, complimenting the non-table version earlier, which has been moved off the main menu (link at the bottom of the table). Added a... (4 Replies)
Discussion started by: Neo
4 Replies

2. What is on Your Mind?

My Charts in the Prototype Vue.js UserCP

Yea.... something I thought would take me an hour ended up taking most of the day. Well, it's not like those YT video tutorials where it take a week or more to make a video and the guys (gals) make it look so easy. But having said that, I'm happy to share with forum members the first "My... (6 Replies)
Discussion started by: Neo
6 Replies

3. UNIX for Dummies Questions & Answers

difficult problem with function declaration

Hello, I have a problem with the declaration of a function. This is how I declare the function : c:63: void foo(threadpool *tp,void (*func)(void*), (void*)arg); Inside main, I call it like this: main(){ .......... threadpool y; c:104: ... (4 Replies)
Discussion started by: garag11
4 Replies

4. Programming

implicit declaration of function 'reboot'

Hi, I'm tying to use the following function to reboot the system as part of my code #include <unistd.h> #include <linux/reboot.h> int restart(unsigned int delay) { sleep(delay); return reboot(LINUX_REBOOT_CMD_RESTART); } When I try to compile the code I get the warning in the... (2 Replies)
Discussion started by: galapogos
2 Replies

5. UNIX for Dummies Questions & Answers

Pkgmk....question on prototype file

I've read the man page, but still unclear a bit.... I'm making some packages of files. My pkginfo file has a line BASEDIR=/base/path. I also have several prototype files (depending on the package), that either list the destination path as absolute: f non /abs/path/to/go/here/file1 or are... (0 Replies)
Discussion started by: Yinzer955i
0 Replies

6. Programming

gcc warnings: implicit declaration of function...

I am having strange warnings from gcc compiler, which I don't think should come while cmpiling. Can anyone help? The warnings are: - warning: implicit declaration of function 'bzero' - warning: implicit declaration of function 'inet_addr' The code is as below: int main(int argc, char... (2 Replies)
Discussion started by: Ahsan
2 Replies

7. UNIX for Advanced & Expert Users

Solaris pkgmk -> prototype errors...

I'm attempting to create another package (i.e. for utilizing pkgadd/pkgrm/pkgchk/etc.) But after creating the prototype file, I noticed that some of my files begin with the '=' character. How do I get pkgmk to interpret the "/path/=file" as a single file, rather than interpreting it as... (7 Replies)
Discussion started by: mslightn
7 Replies
Login or Register to Ask a Question