hello everybody!
i write this following command: system ("time (md5sum ./a.out)>log 2>&1");
as a part of a c program.
but it printf the following error:
sh: Syntax error: word unexpected (expecting ")")
can anybody tell me why?
thanx in advance
hello everybody!
i write this following command: system ("time (md5sum ./a.out)>log 2>&1");
as a part of a c program.
but it printf the following error:
sh: Syntax error: word unexpected (expecting ")")
can anybody tell me why?
thanx in advance
It is because you used a command which includes ().
ok!i understood that! but how can i solve this problem? (beceause the same command work properly if i post it to the terminal)
thanks a lot!
You could put the command string into an executable script and then call that script with the C system() call. Then test the shell script independently of the C program. Note the space characters on the line.
system( "myscript" )
I believe that the C system() call execs to /usr/bin/sh (see "man 3s system").
The command syntax you are using is not valid in /usr/bin/sh .
Not clear what you intended to capture into "log", but in sh the "(" must be the first character on the command line in that context.
Valid in sh:
(time echo "hello world")
Invalid in sh:
time (echo "hello world")
BTW. If you have syntax which requires a different shell, place the shell name at the top of the called script in the usual manner. e.g.:
#!/usr/bin/myshell
I think a backslash can sovle the problem. just like:
system ("time \\(md5sum ./a.out\\)>log 2>&1");
thanx a lot everybody for the help!!!

Often so, but this isn't 100% consistent from system to system since system() a stdio call, not a UNIX one. Probably best not to put complex shell commands of any sort inside a system() call.