Problem running a C file through the shell

Hi,

I have tried running c file with:

gcc -o example1 first.c
./first.c

But got :

./first.c: line 6: syntax error near unexpected token `!=0'
./first.c: line 6: `if( fork()!=0 )'

This is the file:

#include <sys/types.h>

main() 
{
int s;
if ( fork()!=0 )
{
wait(&s);
execlp("date","date",NULL);
printf("A\n");
}
else{
exexlp("yyy","yyy",NULL);
prinf("B\n");
}
}

I have tried to remove spaces near to the if or to add them, but it seems like I am missing something and it won't run

What error messages did you get when you tried to compile your code? Did it complain about the symbols exexlp and prinf not being found? Did it complain about there not being any function prototypes for fork , wait , execlp , printf , exexlp , and prinf ?

If you actually got an executable file named example1 , why wouldn't you try executing ./example1 ? You showed us that you attempted to run ./first.c and there is no reason to think that a C source code file should be executable; but trying to execute it should have given you one or more diagnostic messages.

If you actually succeeded in running example1 (which, from what you have shown us, I doubt), is there an executable named yyy on your search path? Since you aren't checking the return codes on any of the functions you're calling, why would you believe that any of the functions you're calling are doing what you expected them to do?

I strongly suggest that you first get rid of all of the compilation warnings and errors in the code you're trying to compile before you try running any object file compiling it creates. Then try running the object file created; not the source file.

This is what I get when trying to run the object

./example1
bash: ./example1: No such file or directory

Though I did add the error that showed when I tried to run the first.c file:

./first.c: line 6: syntax error near unexpected token `!=0'
./first.c: line 6: `if( fork()!=0 )'

I didn't get what wrong in this line

Sorry, I missed that. I was thinking that you got it from running example1 . :confused: What it wrong is that you can't execute source code. You have to compile it first and run the executable code that is produced from that source. But, as I implied with all of the questions about what errors you got when you tried to compile first.c (which you still haven't answered), I'm not at all surprised that you didn't get an executable file named example1 . Fix all of the errors in your source code (missing #include lines for the headers that contain the prototypes for the functions you're using, misspelled function names, incorrect types on function arguments, and strongly consider checking return codes on functions like wait , execlp , and fork .

Note that fork returns 0 in the child after a successful fork, -1 after a failed fork, and the PID of the child to the parent after a successful fork. Just checking for non-zero doesn't give you enough information to understand what is going on in your code.

I'm going to make a wild guess and say there's no such file or directory.

If you run 'gcc filename.c' without giving it an output file, it generates the file name a.out for you. Have you tried running ./a.out ?

Failing that, do ls -l and see what files you actually do have.

You cannot run the .c file itself.

I did 'gcc -o example1 first.c'

We know that. But we have asked what output that command produced and you still haven't shown us that output!

It`s in my second post:

./example1
bash: ./example1: No such file or directory

No! That is the output from running the command:

./example1

Show us the output from running the command:

gcc -o example1 first.c

I also asked for ls -l