Problem running a C file through the shell

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Problem running a C file through the shell
# 1  
Old 04-11-2018
Problem running a C file through the shell

Hi,

I have tried running c file with:
Code:
gcc -o example1 first.c
./first.c



But got :

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

This is the file:

Code:
#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
# 2  
Old 04-11-2018
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.
# 3  
Old 04-11-2018
Quote:
Originally Posted by Don Cragun
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
Code:
./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:
Code:
./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
# 4  
Old 04-11-2018
Quote:
Originally Posted by uniran
This is what I get when trying to run the object
Code:
./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:
Code:
./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. Smilie 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.
# 5  
Old 04-11-2018
Quote:
Originally Posted by uniran
This is what I get when trying to run the object
Code:
./example1
bash: ./example1: No such file or directory

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.
# 6  
Old 04-12-2018
Quote:
Originally Posted by Corona688
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'
# 7  
Old 04-12-2018
Quote:
Originally Posted by uniran
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. Shell Programming and Scripting

Running .sh file inside a shell script

Hello, You might help a newbie like me, I am trying to run a .sh inside my shell script. After running that I need to execute below commands. Here's how my scripts looks like. Hope you can help: #!/bin/sh cd $ORACLE_HOME/owb/bin/unix ./OMBPlus.sh ---> goes to OMB+> directory cd... (10 Replies)
Discussion started by: aderamos12
10 Replies

3. Shell Programming and Scripting

Problem with running awk script in pbs file

Hi guys I have a problem with one module of my script. It's awk script running in pbs file - It's working when I make a call from pbs to separate awk only file like this but when I try to execute this code in pbs file it fails awk 'BEGIN { max = -1000; min = 1000 } { {$4 == "TCO"} ... (2 Replies)
Discussion started by: grincz
2 Replies

4. Shell Programming and Scripting

Problem in running a shell script

Hi, I'm trying to run a simple shell script whose contents are pasted below. #!/usr/bin/ksh echo $PATH performbuild { echo "Inside performBuild function" } echo "Main Shell started" performbuild echo "Main Shell ended" DV> ls -ltr total 48 -rwxr-xr-x 1 pb5377 it-ibm ... (5 Replies)
Discussion started by: blp_18
5 Replies

5. Shell Programming and Scripting

Shell programming - running the exe file and printing the output.. ?

hai i have a directory lib in that lib directory i have 10 batch files. step i have to do is 1) EXECUTE ALL THE FILES by using the command dwarfdump <filename>| grep DW_AT_SUN_command_line and put the output in one text file.instead of executing the files for all the 10 files... (13 Replies)
Discussion started by: shenthil76
13 Replies

6. Shell Programming and Scripting

error running sqlplus from shell file

I am running a shell file following script on bash shell in solaris 10 ( echo abc@orcl echo abc echo "set feedback off" echo "truncate table SIndexDataTypeHst1_changes;" ) | sqlplus -s but getting the following error ERROR: ORA-01005: null password given; logon denied ... (3 Replies)
Discussion started by: mmunir
3 Replies

7. Shell Programming and Scripting

problem running shell script (for oracle export) in crontab

Hello Gurus, I've been tasked with solving a problem at my new job and I'm stumped. We've got a script that dynamically builds an oracle export parameter files and then runs export from the shell. it runs fine when using the shell, but will NOT run (fails in one spot everytime) when entered... (1 Reply)
Discussion started by: jsheehan223
1 Replies

8. UNIX for Advanced & Expert Users

Problem in running bash shell commands on HP-UX machine

Hello All, After login to the server we are explicitly calling /usr/local/bin/bash to activate bash shell properly. But since commands are not executing properly so I think it is not initialized well. I am facing following problems: 1) If I want to have a look on a particular file using tail... (6 Replies)
Discussion started by: abhishek0071
6 Replies

9. UNIX for Dummies Questions & Answers

Running file sql from shell without capture the password

Dear All I have file a.sql, let's say the content is: _________________________________ select * from dual; exit; _________________________________ and I have shell script a.sh, the content is: _________________________________ ORACLE_SID=testing; export ORACLE_SID... (0 Replies)
Discussion started by: Aditya Purwanto
0 Replies

10. Shell Programming and Scripting

batch file not running after implement dummy shell

Hi, I want to ask if anyone has any ideas, why the batch file i created to transfer files on my desktop doesn't run after i put this line in /etc/passwd and any idea to overcome it ? Thanks user:!:xxx:xxx::/home/user:/usr/local/bin/ssh-dummy-shell (2 Replies)
Discussion started by: DarReNz
2 Replies
Login or Register to Ask a Question