Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert a shell sciprt in a c program

Status
Not open for further replies.

CaptNeo

Technical User
Jun 4, 2002
49
0
0
US
Hello all,

Is there a method to insert a shell script in a c program or binary source code so that when I compile the source code the shell script gets compiled as well?

Any help will be appreciated.
 
Two obvious ways I guess:
The first is to use the "system" function to call a shell script but obviously then you need to ensure that you keep the C executable and the shell script together.
The second is similar but you store the shell source in the C source and get the C program to create the shell script "on the fly". Then you can guarantee that it will always be available. You will, of course, have to worry about file locations and names and permissions.



Trojan.
 
Trojan, could you give me an example?

PHV, I need something that's free.

I know I've done this in the past but I lost my documentation. I was able to add a shell script within a C source code and compiled it. I don't remember using the "system" function but if anyone could provide me a little more detail, I would appreciate it.
 
I guess you just embed your Shell source in the C as a char array and just before you need to execute it, you fprintf it to a file and "system" it.
I guess I could sit down and write an example but that will take a little time and I'm just a little short of time for a while.
I should be back later though so if you'd had no further help by then...


Trojan.
 
How do I write a system function containing a lengthy shell script?
 
The system function doesn't contain it, you write it to a file. The system function is merely given the name of the file.
The only pain is storing the source for the shell script in the C source.
Another idea would be to encrypt the shell script and leave it encrypted and get the C program to decrypt a copy and send that to a shell.
To do that your C program would open the script, read all the data to a char array and decrypt it and then open a pipeline to a shell and feed it the script as stdin.


Trojan.
 
Trojan, do you think this would work?

#include <stdlib.h>
main()
{
system("decrypt < test.sh | ksh");
}

I haven't had the time to test this but I'll do it next week.

ogniemi, I've tried that but it was giving me an error. I wasn't able to get the exact error but I'll post it next week.

Thanks you guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top