Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!visdc!jiii From: jiii@visdc.UUCP (John E Van Deusen III) Newsgroups: comp.unix.wizards Subject: Re: exec() with executable shell scripts Message-ID: <522@visdc.UUCP> Date: 24 Apr 89 23:00:24 GMT References: <647@mqcomp.oz> <520@visdc.UUCP> <7954@june.cs.washington.edu> Reply-To: jiii@visdc.UUCP (John E Van Deusen III) Organization: VI Software Development, Boise, Idaho Lines: 33 In article <7954@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