LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2012, 01:59 AM   #1
shambhavi
LQ Newbie
 
Registered: Sep 2012
Location: india
Posts: 11

Rep: Reputation: Disabled
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?
 
Old 09-16-2012, 02:13 AM   #2
rhklinux
Member
 
Registered: Jan 2010
Location: india/pune
Distribution: Arch Fedora20
Posts: 126

Rep: Reputation: 18
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

Last edited by rhklinux; 09-16-2012 at 02:18 AM.
 
Old 09-16-2012, 03:20 AM   #3
shambhavi
LQ Newbie
 
Registered: Sep 2012
Location: india
Posts: 11

Original Poster
Rep: Reputation: Disabled
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;
}

Last edited by shambhavi; 09-16-2012 at 05:35 AM.
 
Old 09-16-2012, 03:31 AM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
 
Old 09-16-2012, 04:19 AM   #5
shambhavi
LQ Newbie
 
Registered: Sep 2012
Location: india
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-16-2012, 04:44 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.).
 
Old 09-16-2012, 05:23 AM   #7
shambhavi
LQ Newbie
 
Registered: Sep 2012
Location: india
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thumbs up

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling C Programs moserbear Linux - Newbie 7 09-20-2007 07:39 PM
compiling programs tgm4883 Fedora 6 03-17-2006 12:53 PM
I need help with compiling my C++ programs. bitessy Programming 3 02-23-2004 06:07 PM
compiling c,c++ programs ksd Linux - Newbie 9 10-13-2003 08:23 AM
Compiling Programs Jabber63 Linux - Software 3 08-06-2003 02:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration