Reputation: 57
1)After solving the question: How to translate 'system()' call to 'fork() + execl()' when dealing with awk command?
2)I encounter another problem according to melpomene's method:
#define LOG_FILE_PATH "/tmp/logfile"
system("awk -v FS=\"[][]\" -v BEGINTIME=\"$BEGINTIME\" -v ENDTIME=\"$ENDTIME\" '$2>=BEGINTIME && $2<=ENDTIME' "LOG_FILE_PATH);
it works fine and I get what I want.
3)Translate to fork+execl version:
sprintf(tmp1, "BEGINTIME=%s", getenv("BEGINTIME"));
sprintf(tmp2, "ENDTIME=%s", getenv("ENDTIME"));
sprintf(tmp3, "$2>=%s && $2<=%s", getenv("BEGINTIME"), getenv("ENDTIME"));
execl("/usr/bin/awk", "awk", "-v", "FS=\"[][]\"", "-v", tmp1, "-v", tmp2, tmp3, LOG_FILE_PATH, (char *)0);
it outputs nothing, why?
Upvotes: 0
Views: 430
Reputation: 212268
Because tmp
3 should include the literal text BEGINTIME
and ENDTIME
. Try:
sprintf( tmp3, "$2>=%s && $2<=%s", "BEGINTIME", "ENDTIME" );
Also, you probably want to keep the double quotes in tmp1
and tmp2
:
sprintf(tmp1, "BEGINTIME=\"%s\"", getenv("BEGINTIME"));
and you really need to check that the environment variables are actually set.
Also, you are setting FS incorrectly. In the system
version, awk
gets the argument FS=[][]
(the shell strips the double quotes before it invokes awk
.) So the argument passed to execl
should be "FS=[][]"
Upvotes: 2
Reputation: 203684
If you're on Solaris then /usr/bin/awk is old, broken awk which among it's many other issues does not support -v
for variable assignment.
Also, awk programs are delimited by a quotation character so I THINK you should be doing something like this:
sprintf(tmp3, "\'$2>=%s && $2<=%s\'", ...);
instead of this:
sprintf(tmp3, "$2>=%s && $2<=%s", ...);
Upvotes: 0