Multiline shebang: Difference between revisions

m
→‎{{header|J}}: inline code formatting
(Added Sidef)
m (→‎{{header|J}}: inline code formatting)
Line 252:
=={{header|J}}==
 
Assuming this task is asking for a mix of unix shell commands and J, and also that the J binary directory is listed in <code>$PATH</code>
 
<lang J>#!/bin/sh
Line 282:
</lang>
 
'''Notes:'''
 
The <code>#!/bin/sh</code> line is interpreted by J as a verb train with no arguments - in other words, it is ignored.
 
The <code># 0 :0</code> line is interpreted by shell as a comment and by J as the beginning of a multiline "hereis" script which basically ignores everything up to the lone right parenthesis.
 
So then it's just regular shell script up until the line where we turn control over to J. On that line, we use <code>exec</code> (so that the shell process does not hang around, waiting for J to finish - J takes over the current process). And we pass any shell script command line arguments on to J.
 
On the J side of the fence, we presumably want this code to behave like a normal unix module, so we need to override J's default behavior (which is to provide the J command line). <code>9!:29]1[9!:27'2!:55]1</code> is a bit of magic that accomplishes that: it stacks a command to exit with exit code 1 to be executed when we reach the command line. So any errors will terminate the program.
 
Next, we run the system J profile so that we have all of the standard stuff that that provides. (Or leave this out if that's what you want.)
Line 325:
</lang>
 
The <code>exit $?</code> line tells the shell interpreter to ignore the J part of the file, and the <code>$?</code> reuses J's exit code as the exit code from the shell instance.
 
Note that we've left off the onfail handler within J, and just used a minimal definition to give us a non-zero exit code for the error case. Mostly, the assumption here would be that the error message would not be interesting, and that any failure should be handled by a retry. But you could replace the exit on error line here with the full definition and <code>9!:</code> preparatory bit from the previous example and you could also of course change the <code>1!:2&2</code> lines (<code>1!:2&2</code> is the "low-level" write to stdout mechanism for J - and, yes, those numbers are part of the definition of the language - or at least the "Foreigns" part of the language - note that ultimately all computer languages resolve to things which can be thought of as numbers or sequences of numbers, though some people will vigorously assert other things).
 
=={{header|MATLAB}}==
892

edits