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: |
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 |
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; } |
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. |
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: |
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 Your code is syntactically correct except for the Code:
#include<sys/type.h> Code:
#include<sys/types.h> Code:
./program This is an example shell script: Code:
#!/bin/sh |
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. |