I had been looking at page 75 of this online book:
I've used the system function in C to call bash commands before, but wanted to learn this way too. The solution in the book worked perfectly. However, I tried changing the simple "ls -l /" they used to this one-liner:
lsof 2>&1 | grep log | awk '/openbox/ { print $10 }' | sed '1d'
If you're using a red hat based system instead of debian, you'll probably have to change the $10 to $9 in the above.
However, I'm getting the following errors:
# ./cbash2
Done with main program.
pyro# lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
lsof: status error on 2>&1: No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on grep: No such file or directory
lsof: status error on log: No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on awk: No such file or directory
lsof: status error on '/openbox: No such file or directory
lsof: status error on { print $10 }': No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on sed: No such file or directory
lsof: status error on '1d': No such file or directory
lsof 4.86
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g ] [-i ] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Here is the code I'm working with now:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
// Spawn a child process for the bash part later:
int spawn(char* program, char** arg_list)
{
pid_t child_pid;
// Duplicate this process:
child_pid = fork();
if (child_pid !=0)
// This is the parent process.
return child_pid;
else {
// Execute "program" which will call bash:
execvp (program, arg_list);
// execvp will only return if an error occurs
fprintf (stderr, "an error occurred in execvp\n");
abort();
}
}
int main()
{
//Argument list to pass bash commands:
char* arg_list[] = {
"lsof", // argv[0], the name of the program.
"2>&1",
"|",
"grep",
"log",
"|",
"awk",
"'/openbox/",
"{ print $10 }'",
"|",
"sed",
"'1d'",
NULL // Arg list must end with a null.
};
// Spawn the child process running bash.
spawn("lsof", arg_list);
printf("Done with main program.\n");
return 0;
}
Any advice much appreciated!