LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compiling shell programs (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-shell-programs-4175427473/)

shambhavi 09-16-2012 01:59 AM

compiling shell programs
 
hi
friends i have just installed fedora 17 on a vmware player.But,when i try to run shell program,it is showing
error as "unexpected token near symbol '('" and " int main()". so,how to correct it.
can anyone help me?:confused:

rhklinux 09-16-2012 02:13 AM

First of all welcome to LQ
I guess you are from india , me too :)
Moving to your question , are you writing a 'C' program or shell script?(we call it shell script not shell program)
You are writing a c program and calling it a shell script
'C' program has main method shell scripts dont,to compile 'C' program..
open terminal go to your c program file directory using cd(change directory) then type
gcc filename.c
filename is your filename .
then if you got no errors
./a.out

To run shell script
type sh scriptname.sh

also read http://en.wikipedia.org/wiki/Shell_script

shambhavi 09-16-2012 03:20 AM

thanks for your help:)
But iam sure that im running shell script.
Here is the script
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
pid_t pid;
int main()
{
pid=fork();
if(pid==0)
{
printf("child process\n");
printf("the child process is %d\n",getpid());
printf("the parent process is %d\n",getppid());
}
else
{
pid=wait(0);
printf("parent process\n");
printf("child process is %d\n",pid);
printf("the parent process is %d\n",getpid());
}
return 0;
}

414N 09-16-2012 03:31 AM

Don't know from what your assumption of shell script comes, but what you just posted (please use [code][/code] tags when posting code or commands output) is C source code.
To execute it you need to compile it first as rhklinux suggested.
Shell scripts need not to be compiled, as they're interpreted.

shambhavi 09-16-2012 04:19 AM

Whatever you told that's right.
I agree and i used sh filename.sh only to run it.
But it is showing as given below:
line 5:syntax error near unexpected token '('
line 5:`int main()`

Please help me.:newbie:

414N 09-16-2012 04:44 AM

How can you tell that what I told earlier is right if you're still trying to execute that C code as a shell script?
Let's try to make it more clear: the program you wrote is a C program and its suffix should be .c and not .sh.
C programs need to be compiled first, using a C compiler (gcc is the most widespread one on Linux).
If your program is inside file program.c, then
Code:

gcc program.c -Wall -o program
will compile your code to the executable program, if no errors occur (the -Wall flag makes the compiler print all warnings).
Your code is syntactically correct except for the
Code:

#include<sys/type.h>
which should be
Code:

#include<sys/types.h>
After the compilation completes successfully, you'll be able to run the program you just compiled with
Code:

./program
Shell scripts are text files which are directly interpreted by the shell, so no compilation phase occurs.
This is an example shell script:
Code:

#!/bin/sh

print hello

As you can see, the syntax is completely different from what you posted (no main, no includes, no ';' after instructions etc.).

shambhavi 09-16-2012 05:23 AM

Yes.I executed in the same way as u told.
I got correct output.
Thanks a lot:)


All times are GMT -5. The time now is 07:49 PM.