How to define a script interpreter with shebang

Clear that you can use

#!/usr/bin/perl

shebang in the very first line of the script to define the interpreter. However, this assumes an interpreter that ignores hashmark start lines as comments. How can I use an interpreter that does not have this function?

+5
source share
3 answers

With a wrapper that deletes the first line and calls the real interpreter with the rest of the file. It might look like this:

#!/bin/sh

# set your "real" interpreter here, or use cat for debugging
REALINTERP="cat"

tail -n +2 $1 | $REALINTERP

Also: in some cases, ignoring the error message on this first line may be an option.

Last resort: support for your interpreter's char comment code in the kernel.

+2
source

, . , script script . script 'first.myint' 'myinterpreter', C. .

#!/usr/local/bin/myinterpreter
% 1 #########
2 xxxxxxxxxxx
333
444
% the last comment

:

#include <errno.h>
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#define BUFFERSIZE  256                         /* input buffer size */

  int
main ( int argc, char *argv[] )
{
  char    comment_leader  = '%';                /* define the coment leader */
  char    *line = NULL;
  size_t  len   = 0;
  ssize_t read;
  //  char  buffer[BUFFERSIZE];

  // argv[0] : the name of this executable
  // argv[1] : the name the script calling this executable via shebang

  FILE  *input;                                 /* input-file pointer */
  char  *input_file_name = argv[1];             /* the script name    */

  input = fopen( input_file_name, "r" );
  if ( input == NULL ) {
    fprintf ( stderr, "couldn't open file '%s'; %s\n",
        input_file_name, strerror(errno) );
    exit (EXIT_FAILURE);
  }

  while ((read = getline(&line, &len, input)) != -1) {
    if ( line[0] != comment_leader ) {
      printf( "%s", line );                     /* print line as a test */
    }
    else {
      printf ( "Skipped a comment!\n" );
    }
  }

  free(line);

  if( fclose(input) == EOF ) {                  /* close input file   */
    fprintf ( stderr, "couldn't close file '%s'; %s\n",
        input_file_name, strerror(errno) );
    exit (EXIT_FAILURE);
  }

  return EXIT_SUCCESS;
}   /* ----------  end of function main  ---------- */

script ( ) :

...~> ./first.myint
#!/usr/local/bin/myinterpreter
Skipped a comment!
2 xxxxxxxxxxx
333
444
Skipped a comment!
+1

. holgero .

tail -n +2 $1 | $REALINTERP

, :

linux script * binary * (.. , , chmod 755)?

", , SHC - "

SHC C, . :

http://www.datsi.fi.upm.es/~frosal/

, polyscript.sh:

$ cat polyscript.sh
#!/bin/bash

tail -n +2 $1 | poly

I compiled this with shc and, in turn, with gcc:

$ shc-3.8.9/shc -f polyscript.sh
$ gcc -Wall polyscript.sh.x.c -o polyscript

Now I was able to create the first script written in ML:

$ cat smlscript
#!/home/gergoe/projects/shebang/polyscript $0

print "Hello World!"

and I was able to run it:

$ chmod u+x smlscript
$ ./smlscript
Poly/ML 5.4.1 Release
> > # Hello World!val it = (): unit

Poly has no way to disable compiler output, but that is not a problem. It might be interesting to write a polyscript directly in C, as fgm suggested, but it probably wouldn't speed up.

So that’s how it is. I welcome any comments.

0
source

All Articles