|
Home > Archive > Unix administration > July 2005 > Execute a shell script from within a C-Program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Execute a shell script from within a C-Program
|
|
|
| Hello Group,
I want to execute a shell-script from within a c-program.
It should run on a solaris 10 host and work as a cgi-application.
But when I run it I get a core-dump.
Is this a "you can not to it this way" or have I forgotten something?
Thank you for any hints.
Bernhard
I do it the following way (c-wrapper application):
//----------------------
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c;
FILE *in_stream;
system("/export/home/buchreit/snmp_scripts/snmpwalk_GetARPTable
192.168.10.1");
printf("Content-type: text/html\n\n"); //HTML-Header
in_stream =
fopen("/export/home/buchreit/snmp_scripts/ARPTable","r");
while ((c=fgetc(in_stream)) != EOF)
printf("%c",c);
return(0);
}
//----------------------------
The shell-script looks like:
//-----------------------------
snmpwalk -c public -v 1 $1 1.3.6.1.2.1.4.22.1.2 > tmp_ARPtable
sed -f cmd.sed tmp_ARPtable > ARPtable
//------------------------------
And the sed-command-file looks like:
//-------------------------------
s/^IP-MIB\:\:ipNetToMediaPhysAddress\./\<tr\>\<td\>/
s/ = STRING\: /\<\/td\>\<td\>/
s/$/\<\/td\>\<\/tr\>/
//-------------------------------
| |
|
| none wrote:
> Hello Group,
> I want to execute a shell-script from within a c-program.
> It should run on a solaris 10 host and work as a cgi-application.
> But when I run it I get a core-dump.
> Is this a "you can not to it this way" or have I forgotten something?
> Thank you for any hints.
>
> Bernhard
>
>
> I do it the following way (c-wrapper application):
> //----------------------
> #include <stdio.h>
> #include <stdlib.h>
> int main(void)
> {
> int c;
> FILE *in_stream;
> system("/export/home/buchreit/snmp_scripts/snmpwalk_GetARPTable
> 192.168.10.1");
> printf("Content-type: text/html\n\n"); //HTML-Header
> in_stream =
> fopen("/export/home/buchreit/snmp_scripts/ARPTable","r");
> while ((c=fgetc(in_stream)) != EOF)
> printf("%c",c);
>
> return(0);
> }
> //----------------------------
> The shell-script looks like:
> //-----------------------------
> snmpwalk -c public -v 1 $1 1.3.6.1.2.1.4.22.1.2 > tmp_ARPtable
> sed -f cmd.sed tmp_ARPtable > ARPtable
> //------------------------------
> And the sed-command-file looks like:
> //-------------------------------
> s/^IP-MIB\:\:ipNetToMediaPhysAddress\./\<tr\>\<td\>/
> s/ = STRING\: /\<\/td\>\<td\>/
> s/$/\<\/td\>\<\/tr\>/
> //-------------------------------
did you try running the script by itself? not from the C program?
next, try running the script form your C prog, but have the script just echo
the $1 param and exit. Whats the script bash? might put #!/bin/bash at the
top but probably that wont matter, your script is called like this:
/bin/sh -c /export/home/buchreit/snmp_scripts/snmpwalk_GetARPTable
192.168.10.1
according to the man page i have.
Beyond that, i dont see anything in your system command that looks illegal.
Eric
| |
|
|
"Eric" <BorgMotherShip@AliensR_US.org> schrieb im Newsbeitrag
news:BKKdnTys6YusNkHfRVn-jg@comcast.com...
> did you try running the script by itself? not from the C program?
> next, try running the script form your C prog, but have the script just
> echo
> the $1 param and exit. Whats the script bash? might put #!/bin/bash at the
> top but probably that wont matter, your script is called like this:
> /bin/sh -c /export/home/buchreit/snmp_scripts/snmpwalk_GetARPTable
> 192.168.10.1
> according to the man page i have.
> Beyond that, i dont see anything in your system command that looks
> illegal.
> Eric
>
Thank you for your answer.
The script by itself works. Inserting th #!/usr/bin/bash line into the
shell-script does not change the core-dumping of the C-program.
Very strange...
Bernhard
|
|
|
|
|