2

I guess my question is pretty hard to decipher (feel free to perfect it). But it pertains to the use of shebang lines in scripts – specifically, in this case, JavaScripts. Let's say I have this phantomjs script:

#!/usr/bin/env phantomjs

/*
Requires phantomjs to be installed (e.g. via Homebrew on Mac)

Example URL:
http://code.google.com/p/phantomjs/wiki/QuickStart#DOM_Manipulation
*/

var page = require('webpage').create(),
    url = 'http://lite.yelp.com/search?find_desc=pizza&find_loc=94040&find_submit=Search';

page.open(url, function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var results = page.evaluate(function() {
            var list = document.querySelectorAll('span.address'), pizza = [], i;
            for (i = 0; i < list.length; i++) {
                pizza.push(list[i].innerText);
            }
            return pizza;
        });
        console.log(results.join('\n'));
    }
    phantom.exit();
});

I suspect this is a highly ridiculous question, but:

Is there any possible trick that lets you keep a shebang line such as in the code above – so that you can execute the file without the need to prefix the script execution with (in this case) phantomjs – and still have it interpreted without errors in JavaScript?

If not, is there any other language interpreter (e.g. Ruby, Python) that 'accepts' shebang lines, or better yet, has a way to ignore them?

nicael
  • 18,550
  • 13
  • 57
  • 90
Henrik
  • 2,421
  • 4
  • 25
  • 33
  • The "hash bang" convention inherently relies on an interpreter ignoring lines that begin with "#". – Pointy Jun 08 '12 at 00:18
  • possible duplicate of [How to make javascript support shebang(#!)?](http://stackoverflow.com/questions/10696222/how-to-make-javascript-support-shebang) – Oliver Charlesworth Jun 08 '12 at 00:19
  • @OliCharlesworth Agreed! I clicked "close" and voted to merge it with the question you pointed to. Currently says it needs 3 more votes to have it merged. (Sorry for the duplicate!) – Henrik Jun 08 '12 at 00:29

2 Answers2

4

Node.js does ignore the shebang line if it exists, and is a special exception in their interpreter to handle it. PhantomJS would have to add such a support as well, since Javascript doesn't use #'s for comments.

If you're not attached to PhantomJS, though, you could always use the Zombie library for Node.js and accomplish the same thing you're doing in your example code.

  • 1
    Actually I found out that phantomjs 1.5.0 does seem to accept/ignore the shebang line if executing the script as `phantomjs shebang-prepended-javascript.js` (And thus, I feel quite dumb for asking this question, but hey, no harm done I hope :) – Henrik Jun 08 '12 at 00:39
2

I don't know the answer to your first question, but the answer is "yes" to your second question. For example, Racket allows the following script:

#! /usr/bin/env racket
#lang racket
"Hello, world!"

Since the #! is ignored as a line comment.

Asumu Takikawa
  • 8,447
  • 1
  • 28
  • 43