[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
void err_msg (const char *msg)
{
perror (msg);
exit (EXIT_FAILURE);
}
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
err_msg ("fork error");
} else if (pid == 0) {
if (execl("/home/beyes/shell/temp.sh", "temp.sh", "arg1", "arg2", (char *)0) < 0)
err_msg("execl error");
}
if (waitpid(pid, NULL, 0) < 0)
err_msg("waitpid error");
exit (EXIT_SUCCESS);
}
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
FILE *fp;
char combuf[512];
char *pbuf = combuf;
int c;
for (i = 0; i < argc; i++)
printf ("argv[%d] = %s\n", i, argv);
printf ("------------interpret script begin------------\n");
fp = fopen(argv[2], "r");
if ((unsigned char)fgetc(fp) == '#') {
while ((unsigned char)(c = fgetc(fp)) != '\n')
continue;
}
while (!feof(fp)) {
fseek (fp, -1, SEEK_CUR);
while ((unsigned char)(c = fgetc(fp)) == '\n')
continue;
fseek (fp, -1, SEEK_CUR);
while ((unsigned char)(c = fgetc(fp)) != '\n')
*pbuf++ = (unsigned char)c;
*pbuf = '\0';
system(combuf);
pbuf = combuf;
fgetc(fp);
}
fclose(fp);
return 0;
}