From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA30502 for caml-redistribution; Fri, 9 Jul 1999 03:10:34 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA06835 for ; Fri, 9 Jul 1999 02:18:26 +0200 (MET DST) Received: from beach.frankfurt.netsurf.de (beach.frankfurt.netsurf.de [194.64.181.2]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id CAA02370 for ; Fri, 9 Jul 1999 02:18:24 +0200 (MET DST) Received: from schneemann.darmstadt.netsurf.de (board-84.darmstadt.netsurf.de [194.163.86.212]) by beach.frankfurt.netsurf.de (8.8.5/8.8.5) with ESMTP id CAA27660; Fri, 9 Jul 1999 02:18:23 +0200 (MET DST) Received: from localhost (localhost [[UNIX: localhost]]) by schneemann.darmstadt.netsurf.de (8.8.8/8.8.8) id BAA00080; Fri, 9 Jul 1999 01:44:02 +0200 From: Gerd Stolpmann Reply-To: Gerd.Stolpmann@darmstadt.netsurf.de Organization: privat To: Markus Mottl , luther@dpt-info.u-strasbg.fr Subject: Re: Sys.argv with interpreter and compiler Date: Fri, 9 Jul 1999 01:23:34 +0200 X-Mailer: KMail [version 1.0.17] Content-Type: text/plain Cc: caml-list@inria.fr (OCAML) References: <199907050937.LAA30606@miss.wu-wien.ac.at> MIME-Version: 1.0 Message-Id: <99070901440006.28040@schneemann> Content-Transfer-Encoding: 8bit Sender: weis On Mon, 05 Jul 1999, Markus Mottl wrote: >> I don't get the same, why : >> >> sh-2.02$ cat essay >> #!/usr/local/bin/ocaml >> print_string "Hello world!"; print_newline();; >> exit 0;; >> ^D >> sh-2.02$ ./essai >> ./essai: line 2: syntax error near unexpected token `;' >> ./essai: line 2: `print_string "Hello world!"; print_newline();;' > >Your toplevel "/usr/local/bin/ocaml" is obviously compiled to byte code. >Take a look at it with "less" and you will see that the first line of >this executable is actually "#!/home/mottl/mysys/bin/ocamlrun". There is another way to get around this: #!/bin/sh # (* exec ocaml "$0" "$@" *) directory ".";; print_string "Hello world!"; print_newline();; exit(0);; It does not matter whether "ocaml" is itself a script or not, and you do not need to specify the interpreter with an absolute file name; /bin/sh always exists. The solution is a bit tricky: The first lines are interpreted by both /bin/sh and ocaml. # (* The shell sees a comment line because of #. Ocaml sees the beginning of a directive with a comment between # and the name of the directive which is allowed. exec ocaml "$0" "$@" This line is only seen by the shell, as ocaml thinnks it is a comment. It causes that the process switches over to run ocaml with the given parameters. The shell stops here and does not read further. *) directory ".";; The end of the comment and the directive. #directory "." has no effect, as "." is already in the search path. >As far as I know it is not allowed on any unix system to run scripts >whose interpreter is a script itself. This would make it much easier to >replace this interpreter with something evil - e.g. some kind of wrapper >that executes unfriendly commands under you UID and continues with the >"true" interpreter. That's what they tell you. I believe that kernel programmers do not like recursions at all. (There are actually some serious problems with recursions in kernel mode, for example recursive mutexes.) Gerd -- ---------------------------------------------------------------------------- Gerd Stolpmann Telefon: +49 6151 997705 (privat) Viktoriastr. 100 64293 Darmstadt EMail: Gerd.Stolpmann@darmstadt.netsurf.de (privat) Germany ----------------------------------------------------------------------------