LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 02-18-2014, 06:55 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
How to execute C inside Shell and capture the return value of C in Shell


Hi,

How to execute C inside Shell and capture the return value of C in Shell.



test.c
Code:
#include <stdio.h>
int main(int argc, const char *argv[]){
    int i = 0;
    for (; i < argc; ++i) {
        printf("argv[%d] = '%s'\n", i, argv[i]);
    }
    return argv[i];
}
test.sh
Code:
./test hi how are you
echo $?
 
Old 02-18-2014, 07:00 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
c should be compiled and linked, the shell itself cannot do that.
You need to write that code into a file, compile it and execute from that script
 
1 members found this post helpful.
Old 02-18-2014, 07:11 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Use the system() function to set environment variable(s)?
 
Old 02-18-2014, 08:00 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What's your question? You wrote code which returns a value and you wrote your test.sh script to do exactly what you want. The fact that your code appears incorrect is part of this.

The function returns an integer, however argv[i] is a character array. Therefore you could either change it so that main returns char *, or you return i. By the way, i becomes the value of 5 therefore returning argv[5] is nothing.

Code:
#include <stdio.h>
int main(int argc, const char *argv[]){
    int i = 0;
    for (; i < argc; ++i) {
        printf("argv[%d] = '%s'\n", i, argv[i]);
    }
    return i;
}
Code:
~/testcode$ gcc -o test-return test-return.c
~/testcode$ ./test-return this is my function
argv[0] = './test-return'
argv[1] = 'this'
argv[2] = 'is'
argv[3] = 'my'
argv[4] = 'function'
~/testcode$ echo $?
5
I will grant that I do not know how to return a string from that function and have it be assigned to a bash variable.
 
Old 02-18-2014, 10:48 AM   #5
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The program can only return an integer, which is used to signal if the program failed or not. That you can check with a bash if statement.

If you wanted to make the output of a C program into a bash variable:
Code:
var="$(./test hi how are you)"
echo "$var"
 
  


Reply



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
[SOLVED] shell shell to execute commands for pgrep linux833 Programming 11 09-06-2012 11:01 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
execute shell command inside of c code? khucinx Programming 6 10-26-2010 09:30 PM
Can I execute a shell script from inside a php one. prabhatsoni Linux - Software 2 05-24-2006 05:40 AM
execute shell command inside of tcl file khucinx Programming 1 07-05-2004 01:29 PM

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

All times are GMT -5. The time now is 11:35 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