Execute C program in Current shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute C program in Current shell
# 1  
Old 04-09-2013
Hammer & Screwdriver Execute C program in Current shell

Hello,
I have a c program executable which I need to run inside a shell script.
But the c program runs in a subshell because of which all the actions done by the c program is not available to the current shell.

Is there any way to execute a C program binary executable in the current shell?
# 2  
Old 04-09-2013
Code:
. ./a.out

# 3  
Old 04-09-2013
You are joking, balajesuri, aren't you? Smilie

There is no way to execute a compiled executable within a shell. Shells execute shell scripts, not binaries. If you need a side effect of a child process on the parent shell, your compiled program could output shell commands, which are intercepted and executed by the parent shell (sort of eval :-) )

For example, if the compiled program prints the following output to stdout:

Code:
ABC=123;
DEF=456;
cd /tmp;

and the shell starts the program like

Code:
eval $(./a.out)

the shell will define the two variables ABC and DEF and change the current working directory to /tmp.

Last edited by hergp; 04-09-2013 at 04:55 AM.. Reason: semicolons added
# 4  
Old 04-09-2013
Quote:
Originally Posted by sachinverma
Hello,
I have a c program executable which I need to run inside a shell script.
But the c program runs in a subshell because of which all the actions done by the c program is not available to the current shell.

Is there any way to execute a C program binary executable in the current shell?
If a C program needs to export some variables, things usually work the opposite way -- run your C program first, export a bunch of variables, then run your program. Your program will inherit the new values from it that way.

Code:
int main()
{
        setenv("FOO","BAR",1);
        execlp("/path/to/script.sh", "/path/to/script.sh", "arg1", "arg2", NULL);
        perror("Could not exec");
        exit(1);
}

# 5  
Old 04-10-2013
Quote:
Originally Posted by sachinverma
Is there any way to execute a C program binary executable in the current shell?
You should explain why you want it to be run by the current shell.

In any case, if you have the source code of your program and if your shell is ksh93, there is a way to modify your program to make it a custom builtin command and then have it effectively running by the current shell, see www/ksh/builtins.mm mm document

If your only goal is to set variables, that would be overkill compared to the previous suggestions though.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to execute a unix shell script from a java program

Hi All, well , i am facing this problem.. i have tried a few sample codes but there isn't any solution . could anyone please give a sample code as of how to do this... Please see the below details...and read the details carefully. I have written some code, logic is 1)from... (4 Replies)
Discussion started by: aish11
4 Replies

2. UNIX for Dummies Questions & Answers

Execute crontab for every 4 hours and begin from current time

I want to add a crontab entry which should execute for every 4 hours and that 4 hours calculation should begin from the current time. Normally if I set the crontab entry like this, 00 */4 30 05 * root date >>/tmp/cronout The above will execute the date command for every 4 hours like... (7 Replies)
Discussion started by: Ganeshwari
7 Replies

3. Shell Programming and Scripting

Execute a C program from Shell

Hi I want to create a shell script tha executes a C program and then retrieves information about it. I managed to run the program with: #!/bin/bash gcc -o program program.c ./program Now i want to get the id of the process (pid) Any help would be appreciated, Thank you (18 Replies)
Discussion started by: nteath
18 Replies

4. Shell Programming and Scripting

Execute commands from script in current bash session

I have a file as follows: cat /etc/mxg/ssh-hostsmx.example1.com.au:2225 mx2.example2.com.au:2225 mx.example3.com.au:2225 mail.example4.com.au:2225 mail.example5.org.au:2225 mail.example6.com.au:2225I want to dynamically create aliases for quick access to these servers from bash. I wrote... (4 Replies)
Discussion started by: jelloir
4 Replies

5. Programming

C program to execute shell script

Hi, Can anyone give me a sample code to execute shell script from C program. Thanks (6 Replies)
Discussion started by: baigmd
6 Replies

6. Shell Programming and Scripting

C program to execute shell script

Hi, Can anyone pls give a sample to execute a shell script from C program Thanks (2 Replies)
Discussion started by: baigmd
2 Replies

7. Programming

get the path of current running program

How does the program know the full path of itself when the program is running in certain diretory? BTW, I have no "argv" information of main() functino. (The program is written in C++ on linux platform) (1 Reply)
Discussion started by: princelinux
1 Replies

8. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

9. Shell Programming and Scripting

How do i execute script in the current shell

How do i run a shell script or perl script with in the context of a current shell. I know that i can use command source. but we can't pass any arguments to our script if we use source command as it takes only one argement i.e filename Is there any way to run a script in the current shell... (5 Replies)
Discussion started by: Naresh Kumar
5 Replies

10. UNIX for Dummies Questions & Answers

Urgent!! How to write a shell program to execute command to access internet?

hi, I am new ot unix. So, can i write a shell(c shell or korn shell) program to access internet? I mean if I run the program, it can access specified url and then copy the html to a file? Can anyone help me? And how can make the program runs every 1 hr? new comer (2 Replies)
Discussion started by: firebirdonfire
2 Replies
Login or Register to Ask a Question