[C Language] Write a C code to produce a Bash script to replace a routine job,
Result :
Use a C code to produce the command like below.
./z_First_Step ./IN/happy1.htm
./z_Second_Step Html2.txt TestScript
./z_Third_Step Html2.txt ./OUT/happy1.sh
./z_First_Step ./IN/happy2.htm
./z_Second_Step Html2.txt TestScript
./z_Third_Step Html2.txt ./OUT/happy2.sh
Write a C code.
Execute bin file and put one parameter into this C code. Then produce the Result.
Create execute bin file to create sh
./RunSh happy1
./RunSh happy2
C Code Display
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WriteFileName "run.sh"
#define BUFF_LENG 4096
char *CreateScript(char *s,char *FileName)
{
static char tmp[BUFF_LENG]={0};
char tmp1[BUFF_LENG]={0};
/*
Most important part.
Produce the script here.
*/
sprintf(tmp,"./z_First_Step ./IN/%s.htm\n",FileName);
strcat(tmp,"./z_Second_Step Html2.txt TestScript\n");
sprintf(tmp1,"./z_Third_Step Html2.txt ./OUT/%s.sh\n",FileName);
strcat(tmp,tmp1);
strcat(tmp,"\n");
return tmp;
}
int main(int argc, char **argv)
{
size_t len;
char buf[BUFF_LENG]={0};
char *s1;
/*
Transmit a parameter to C
*/
char *FileName = (char *)NULL;
int len_argv = 0;
if (argc != 2) return 1;
len_argv = strlen(argv[1]);
if ((FileName = malloc(len_argv + 1)) != NULL) {
bzero(FileName, len_argv + 1);
strncpy(FileName, argv[1], len_argv);
}
/*
Open a file and write.
*/
FILE *fp;
fp = fopen(WriteFileName, "a+");
if (fp == NULL)
exit(1);
// Run Script
s1 = CreateScript(buf,FileName); // Transmit 2 parameter to CreateScript
len = strlen(s1);
fwrite(s1, len, 1, fp);
fclose(fp);
return 0;
}
0 意見:
張貼留言