LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 07: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, 08:00 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,797

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
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, 08:11 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,666

Rep: Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543
Use the system() function to set environment variable(s)?
 
Old 02-18-2014, 09:00 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,844
Blog Entries: 12

Rep: Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010
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, 11:48 AM   #5
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

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


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

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

All times are GMT -5. The time now is 10:09 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration