* script in c
@ 2004-11-08 5:39 kaushal
0 siblings, 0 replies; 6+ messages in thread
From: kaushal @ 2004-11-08 5:39 UTC (permalink / raw)
To: linux prg
Hello all,
Is there a way by which I can exec a shell script without
using the "system()" call?
Thanks in advance.
cheers-
kaushal.
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: script in c
@ 2004-11-08 6:15 Honnavalli_Sreevathsa
2004-11-08 6:32 ` Ron Michael Khu
0 siblings, 1 reply; 6+ messages in thread
From: Honnavalli_Sreevathsa @ 2004-11-08 6:15 UTC (permalink / raw)
To: kaushal, linux-c-programming
You can use popen() and pclose() calls
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org
> [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf
> Of kaushal
> Sent: Monday, November 08, 2004 11:09 AM
> To: linux prg
> Subject: script in c
>
>
> Hello all,
> Is there a way by which I can exec a shell script
> without using the "system()" call? Thanks in advance.
>
> cheers-
> kaushal.
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to
> majordomo@vger.kernel.org More majordomo info at
http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: script in c
2004-11-08 6:15 script in c Honnavalli_Sreevathsa
@ 2004-11-08 6:32 ` Ron Michael Khu
2004-11-08 6:48 ` Jan-Benedict Glaw
0 siblings, 1 reply; 6+ messages in thread
From: Ron Michael Khu @ 2004-11-08 6:32 UTC (permalink / raw)
Cc: linux-c-programming
or the exec functions =)
Honnavalli_Sreevathsa@emc.com wrote:
>You can use popen() and pclose() calls
>
>
>
>
>>-----Original Message-----
>>From: linux-c-programming-owner@vger.kernel.org
>>[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf
>>Of kaushal
>>Sent: Monday, November 08, 2004 11:09 AM
>>To: linux prg
>>Subject: script in c
>>
>>
>>Hello all,
>> Is there a way by which I can exec a shell script
>>without using the "system()" call? Thanks in advance.
>>
>>cheers-
>>kaushal.
>>
>>-
>>To unsubscribe from this list: send the line "unsubscribe
>>linux-c-programming" in the body of a message to
>>majordomo@vger.kernel.org More majordomo info at
>>
>>
>http://vger.kernel.org/majordomo-info.html
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: script in c
2004-11-08 6:32 ` Ron Michael Khu
@ 2004-11-08 6:48 ` Jan-Benedict Glaw
0 siblings, 0 replies; 6+ messages in thread
From: Jan-Benedict Glaw @ 2004-11-08 6:48 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 795 bytes --]
On Mon, 2004-11-08 14:32:25 +0800, Ron Michael Khu <ronkhu@ntsp.nec.co.jp>
wrote in message <418F12F9.6070701@hq.ntsp.nec.co.jp>:
[Calling a script from within a C program]
> or the exec functions =)
Which is generally preferred over system(). system() is a horrible
mess: you face a hard time to properly supply command line arguments
(think about arguments with tabs, spaces, CR or LF in them...).
Just always use the exec functions.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: script in c
@ 2004-11-08 6:50 linux-c-programming-owner
2004-11-08 7:27 ` Glynn Clements
0 siblings, 1 reply; 6+ messages in thread
From: linux-c-programming-owner @ 2004-11-08 6:50 UTC (permalink / raw)
To: linux-c-programming
--- kaushal <kaushal@rocsys.com> wrote:
> Hello all,
Hello,
> Is there a way by which I can exec a
> shell script without
> using the "system()" call?
> Thanks in advance.
You can use exec*() family for this purpose.
man exec
Here is a little sample:
[mbaris@zion:/tmp/code_temp]$ cat exec.c
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void)
{
if (execl("/bin/sh", "-c", "./script.sh", NULL)==-1)
{
printf("ERROR: %s\n", strerror(errno));
}
return 0;
}
[mbaris@zion:/tmp/code_temp]$ cat script.sh
#!/bin/bash
echo "Fired!";
exit;
[mbaris@zion:/tmp/code_temp]$ gcc -o exec exec.c
[mbaris@zion:/tmp/code_temp]$ ./exec
Fired!
[mbaris@zion:/tmp/code_temp]$
>
> cheers-
> kaushal.
>
=====
M.Barýþ Demiray
DOS: n., A small annoying boot virus that causes random
spontaneous system crashes, usually just before saving a massive project. Easily cured by UNIX. See also MS-DOS, IBM-DOS, DR-DOS.
__________________________________
Do you Yahoo!?
Check out the new Yahoo! Front Page.
www.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: script in c
2004-11-08 6:50 linux-c-programming-owner
@ 2004-11-08 7:27 ` Glynn Clements
0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-11-08 7:27 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: message body and .signature --]
[-- Type: text/plain, Size: 990 bytes --]
> > Is there a way by which I can exec a
> > shell script without
> > using the "system()" call?
> > Thanks in advance.
>
> You can use exec*() family for this purpose.
>
> man exec
>
> Here is a little sample:
>
> [mbaris@zion:/tmp/code_temp]$ cat exec.c
> #include <unistd.h>
> #include <string.h>
> #include <errno.h>
>
> int main(void)
> {
> if (execl("/bin/sh", "-c", "./script.sh", NULL)==-1)
> {
> printf("ERROR: %s\n", strerror(errno));
> }
>
> return 0;
> }
A system() replacement is somewhat more complex than that.
If the process wants to continue executing after the script has
finished, you need to fork() a child process, have the child perform
the exec(), then wait() for the child to finish. You may also need to
change the signal handling while the child is executing (system()
ignores SIGINT and SIGQUIT and blocks SIGCHLD while the child process
runs).
A more complete example is attached.
--
Glynn Clements <glynn@gclements.plus.com>
[-- Attachment #2: process spawning example --]
[-- Type: text/plain, Size: 1363 bytes --]
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int spawn(char *command, char **args)
{
/* sample usage:
*
* char *args[3];
*
* args[0] = "sh";
* args[1] = "/path/to/script";
* args[2] = NULL;
* spawn("/bin/sh", args);
*/
struct sigaction act, intr, quit;
sigset_t block, oldmask;
int status = -1;
pid_t pid;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_IGN;
if (sigaction(SIGINT, &act, &intr) < 0)
goto error_1;
if (sigaction(SIGQUIT, &act, &quit) < 0)
goto error_2;
sigemptyset(&block);
sigaddset(&block, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &block, &oldmask) < 0)
goto error_3;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "unable to create a new process");
goto error_4;
}
if (pid == 0)
{
sigaction(SIGINT, &intr, NULL);
sigaction(SIGQUIT, &quit, NULL);
execvp(command, args);
/* if we reach this point, execvp() failed */
fprintf(stderr, "unable to execute command");
_exit(127);
}
else
{
pid_t n;
do n = waitpid(pid, &status, 0);
while (n == (pid_t) -1 && errno == EINTR);
if (n != pid)
status = -1;
}
error_4:
sigprocmask(SIG_SETMASK, &oldmask, NULL);
error_3:
sigaction(SIGQUIT, &quit, NULL);
error_2:
sigaction(SIGINT, &intr, NULL);
error_1:
return status;
}
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-11-08 7:27 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-08 6:15 script in c Honnavalli_Sreevathsa
2004-11-08 6:32 ` Ron Michael Khu
2004-11-08 6:48 ` Jan-Benedict Glaw
-- strict thread matches above, loose matches on Subject: below --
2004-11-08 6:50 linux-c-programming-owner
2004-11-08 7:27 ` Glynn Clements
2004-11-08 5:39 kaushal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).