exec() with executable shell scripts

John E Van Deusen III jiii at visdc.UUCP
Tue Apr 25 09:00:24 AEST 1989


In article <7954 at june.cs.washington.edu> (Kenneth Almquist) writes:
> (John E Van Deusen III) writes:
>> If you do not want to use [execlp or execvp], you have to ...
>>
>> 	execl("/bin/sh", "sh", "-c", "/bin/true", 0);
>
> More generally, you can write:
>
>	execl(program, (char *)0);
>	if (errno == ENOEXEC) {		/* it's a shell procedure */
>		execl("/bin/sh", "sh", program, (char *)0);
>		perror("/bin/sh");
>	} else {
>		perror(program);
>	}
>	exit(2);

With respect to the construct I used, (#1), Mr. Almquist's code, (#2),
is NOT more "general"; assuming, of course, that program is substituted
for "/bin/true" and (char *)0 for 0.

Construct #2 will fail, that is exit(2), if program is not an absolute
or relative pathname and is not a file in the current directory.
Construct #1 uses the shell to resolve pathname(s) and can handle
situations where program is not even a file name; for instance
"date | cut -d: -f2".  Construct #2 will correctly handle the "special"
case where program is a command name, not a path name; it is intended
for the command to be executed from the current directory; and that is
NOT the way PATH is set up.
--
John E Van Deusen III, PO Box 9283, Boise, ID  83707, (208) 343-1865

uunet!visdc!jiii



More information about the Comp.unix.wizards mailing list