Running C program in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running C program in UNIX
# 1  
Old 04-24-2013
Running C program in UNIX

I want to run a C program from my BASH script. Here's some very basic simplified code of my bash script:

Code:
#!/bin/bash
 
echo "Run C program"
./main.c
echo "Ran C program"

Here's my main.c:

Code:
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
  printf("/nRunning C program/n");
}

When I run the script I get the following errors:

Code:
# ./cprog.sh
./cprog.sh: #!/bin/bash: not found
Run C program:
./main.c: 4: Syntax error: "(" unexpected
Ran C program
#

Please excuse my ignorance as it has been some time since I have used C. Do I need more than just main.c?
Do I need to do more than just call it using ./main.c from my script?

Thanks
# 2  
Old 04-24-2013
You cannot give the source file directly. Compile your C program and get an exe, and then execute the exe from the shell script.

Like:
Compile your C program:
Code:
 
$ cc -o myprog main.c
$ ls myprog
myprog

Code:
 
#!/bin/bash

echo "Run C program"
./myprog
echo "Ran C program"

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-24-2013
Quote:
Originally Posted by guruprasadpr
You cannot give the source file directly. Compile your C program and get an exe, and then execute the exe from the shell script.

Like:
Compile your C program:
Code:
 
$ cc -o myprog main.c
$ ls myprog
myprog

Code:
 
#!/bin/bash
 
echo "Run C program"
./myprog
echo "Ran C program"

Guru.
Thanks Guru. That works. Just wondering why using the exe that was generated using the C compiler doesn't work (as opposed to using the Unix compiled one).
# 4  
Old 04-24-2013
Quote:
Originally Posted by millsy5
Just wondering why using the exe that was generated using the C compiler doesn't work (as opposed to using the Unix compiled one).
I think you're confusing the concepts. In your original script you were trying to run a flat text file which happened to have a .c extension. Its contents cannot be interpreted by a shell nor by "Unix" (assuming you're referring to the operating system).

So you basically forgot the step where you compile the code before running it.
This User Gave Thanks to verdepollo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to keep program running after logout

Hello everyone. I am logged into a computer through ssh. I would like to run a program and have it keep running after I log out without screen (i forgot to run it). For example: ssh server user/comp~$ top & 12021 exit after that: ssh server user/comp~$ (something, maybe shell... (5 Replies)
Discussion started by: lepetal
5 Replies

2. Shell Programming and Scripting

Running C# program problem

Hi All, How to run c# program using shell script ? (1 Reply)
Discussion started by: srikar.ch
1 Replies

3. Shell Programming and Scripting

Help with Running More than One Program

Folks, I'm really new to scripting and was wondering if you could help me out. I have the following script that I inherited: #!/bin/bash # # Usage # From the agent directory: # ./run-any-agent AgentName # TAC_AGENT_HOME=`pwd` LIB=${TAC_AGENT_HOME}/lib CLASSPATH=.... (17 Replies)
Discussion started by: DTriniWay
17 Replies

4. UNIX for Dummies Questions & Answers

Running a program (Dynflow)

Lets get some stuff out of the way before the question. I am currently running FreeBSD 7.0 on a VirtualBox virtual machine. I do not know much about Unix or FreeBSD, though I do run linux at home. My boss gave me some files that he says are a unix version of the program Dynflow. The Programs... (0 Replies)
Discussion started by: poet_will
0 Replies

5. Programming

running a parallel program

hi , i need to run a parallel program . for example; program1 { array=" the second program should called here : program 2" the execution should continue } the 2nd program should recieve an array of information as argument and it should... (4 Replies)
Discussion started by: bankpro
4 Replies

6. UNIX for Dummies Questions & Answers

running dos program from unix server

Hello, My apologies if this sounds like a stupid question...... but is it possible to call a script that is located on a w2k machine from a csh script bing run on a unix server. Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies

7. UNIX for Dummies Questions & Answers

Running a program

Hi.Iam new to Linux.i got linux 7.0 pro and dont know how to run programs. I want a perl interputer and i know i installed one but how do i run it ??? Also how do i run a C or C++ editor ?and how do i run cron ? (3 Replies)
Discussion started by: perleo
3 Replies

8. Programming

running a c/c++ program in unix

This is not a question, but rather a simple how-to for programmers who are new to the UNIX environment. I too,am new to UNIX. First I developed a few programs on my box and perfected them until they were satisfactory for execution. Problem was however, that once i compiled and all that,... (2 Replies)
Discussion started by: kray
2 Replies

9. Shell Programming and Scripting

Running a program automatically

How can I make a program run automatically at a certain time of day? My problem is I need to make a small backup program that will back up a few files every day? (3 Replies)
Discussion started by: jvadn0
3 Replies

10. Programming

Running a compiled Program

Just getting into the Unix command line programming and am unable to run any program I write. I am using a Makefile and the source is compiling but when I enter the name of the output file I get back: bash: lab01exe.out: command not found I'm sure I am just dooing something simple... (2 Replies)
Discussion started by: Krebsbac
2 Replies
Login or Register to Ask a Question