[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Shell commands in linux

Cheedu forced the electrons to say:
> 	Thanx for your reply for my earlier question. But the man pages
> didn't help. Can you tell me how to use the "system " and "execve" . An
> example would be nice.

Hmm... For system, something like:

char cmdbuf[100];
sprintf (cmdbuf, "ls -al");
system (cmdbuf);

is what you should do. Use that sprintf to your advantage.

You should be using one of the other exec* functions instead of execve. I
prefer execv, but you might like execl or one of the others better.
To execute a program, I do something like:

char *cmd[3];
/* malloc here for cmd[0-2] */
strcpy (cmd[0], "/bin/ls");
strcpy (cmd[1], "/bin/ls");
strcpy (cmd[2], "-al");
cmd[3] = NULL;
switch (pid = fork()) {
   case -1: exit (-1);
   case 0: execv (cmd[0], cmd);
	   exit (-1);
   default: waitpid (pid, &status, 0);
	    /* Process status here */


The prompt for all occasions:
export PS1="F:\$(pwd | tr '/[a-z]' '\134\134[A-Z]')> "
--------------- Binand Raj S. (binand@xxxxxxxxxxxxxxxxxxxxx)