Native shebang: Difference between revisions
m
→{{header|langur}}
(Added program to Forth) |
Langurmonkey (talk | contribs) |
||
(6 intermediate revisions by 6 users not shown) | |||
Line 55:
Also note that this ".so" will only be generated if the ".a68" source file
has been touched.
'''File: echo.a68'''<
# -*- coding: utf-8 -*- #
STRING ofs := "";
FOR i FROM 4 TO argc DO print((ofs, argv(i))); ofs:=" " OD</
'''Test Execution:'''
<pre>
Line 68:
Hello, world!
</pre>
=={{header|Arturo}}==
Arturo is a scripting language and does not compile to a binary.
<syntaxhighlight lang="rebol">#!/usr/bin/env arturo
print "Hello from Arturo!"</syntaxhighlight>
{{out}}
<pre>$> ./native_shebang.art
Hello from Arturo!</pre>
=={{header|C}}==
Line 76 ⟶ 86:
'''File: script_gcc.c'''
<
/* Optional: this C code initially is-being/can-be boot strapped (compiled) using bash script_gcc.sh */
#include <errno.h>
Line 200 ⟶ 210:
perror(STRCAT(binpath, ": executable not available", ENDCAT));
exit(errno);
}</
'''Test Source File: echo.c'''
<
#include <stdio.h>
#include <string.h>
Line 216 ⟶ 226:
putchar('\n');
exit(EXIT_SUCCESS);
}</
'''Test Execution:'''
Line 229 ⟶ 239:
===2nd version. Pure C, no extra bash script===
'''File: script_gcc.c'''
<
* rosettacode.org: Native shebang
*
Line 400 ⟶ 410:
return !fprintf(stderr, "%s : executable not available\n", exec_path) | ENOENT;
}
</syntaxhighlight>
'''Test Source File: echo.c'''
<
/*
* note, any additional libs or include paths would have params added after
Line 421 ⟶ 431:
putchar('\n');
exit(EXIT_SUCCESS);
}</
'''Test Execution:'''
Line 434 ⟶ 444:
=={{header|Forth}}==
Such functionality can be added easily by the following definition:
<syntaxhighlight lang="text">: #! [COMPILE] \ ; immediate</
Some Forth compilers - like 4tH or Gforth - support this functionality out of the box.
This example program works as advertised:
<syntaxhighlight lang="text">#! /usr/local/bin/4th cxq
argn 1 ?do i args type space loop cr</syntaxhighlight>
=={{header|Free Pascal}}==
Since FPC (FreePascal compiler) version 2.6.0 the distribution – e.g. the Debian or FreeBSD packages <tt>fpc-utils</tt> – come with the program <tt>instantfpc(1)</tt>, or <tt>ifpc(1)</tt> for short.
The program fulfills this task’s specifications, plus other goodies.
The sources are available in [https://
See the [https://wiki.freepascal.org/InstantFPC FreePascal wiki for <tt>ifpc</tt> usage].
Line 455 ⟶ 464:
The following works fine on Ubuntu 16.04.
<
package main
Line 467 ⟶ 476:
fmt.Println(os.Args[1])
}
}</
{{out}}
Line 478 ⟶ 487:
As no compiling versions of J are currently available, the binaries are trivially empty and we shall store them in the empty path. We shall use /usr/local/bin/ijconsole (which was compiled using a C compiler) as the J interpreter, and <code>echo each ARGV</code> as our sample code:
<
echo each ARGV</
=={{header|jq}}==
Line 493 ⟶ 502:
'''Example 1:'''
<
#!/usr/bin/env/jq -M -n -r -f
"Klaatu barada nikto!"</
Line 509 ⟶ 518:
'''Example 2:'''
<
#!/usr/bin/env/jq -M -n -r -f
$x</
{{out}}
<
Hello, world!</
=={{header|Julia}}==
usage: ./thisfile.jl "hello"
<
# Put the Julia code below this line. It will be compiled and run.
Line 525 ⟶ 534:
println(ARGS)
</
<pre>
_
Line 542 ⟶ 551:
'''File: echo.langur'''
<
writeln join " ", _args</
'''Usage:'''
<pre>./echo.langur hello,
{{out}}
<pre>hello,
=={{header|Nim}}==
Line 559 ⟶ 568:
'''File: nativeshebang.nim'''
<
import os,strutils
echo commandLineParams().join(" ")</
'''Usage:'''
<pre>./nativeshebang.nim hello, world</pre>
Line 570 ⟶ 579:
'''File: nim.cfg'''
<syntaxhighlight lang
'''File: nativeshebang2.nims'''
<
import os,strutils
echo commandLineParams().join(" ")</
'''Usage:'''
<pre>./nativeshebang2.nim hello, world</pre>
Line 584 ⟶ 593:
'''File: echo.ml'''
<
let () =
let argl = Array.to_list Sys.argv in
print_endline (String.concat " " (List.tl argl))</
{{out}}
<pre>
Line 606 ⟶ 615:
'''File: echo.pl'''
<
print "@ARGV\n";
</syntaxhighlight>
'''Usage:'''
Line 624 ⟶ 633:
and compilation, apart from the executable file, is a -c flag on the command line, which I recommend
omitting unless it proves helpful or necessary. Example (quietly ignored by pwa/p2js, and Phix in general):
<
You can also invoke the compiler directly as follows
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (system_exec)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">sourcefile</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"test.exw"</span><span style="color: #0000FF;">,</span>
Line 632 ⟶ 641:
<span style="color: #000000;">cmd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">interpreter</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sourcefile</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">system_exec</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cmd</span><span style="color: #0000FF;">)</span>
<!--</
See also demo/capture_console.exw which redirects stdin/out/err while interpreting a child process.
Line 643 ⟶ 652:
'''File: echo.py'''
<
# Although `#!/usr/bin/env python` may be better if the path to python can change
import sys
print " ".join(sys.argv[1:])</
'''Usage:'''
Line 673 ⟶ 682:
File <tt>native-shebang.rkt</tt> contains the following:
<
#lang racket
(displayln "hello")</
My directory contains only this:
Line 709 ⟶ 718:
'''File: echo.p6'''
<syntaxhighlight lang="raku"
put @*ARGS;</
'''Usage:'''
Line 724 ⟶ 733:
===Unix shebang===
Using e.g. Regina open source REXX interpreter
<
#!/usr/local/bin/regina
/* Echo the command line argument */
say arg(1)
</syntaxhighlight>
===ARexx===
Under AmigaOS, the obligatory REXX starting comment /* is recognised as a shebang of its own, automatically causing the file to be parsed by ARexx as long as the file's script flag is set.
<
/* Echo the command line argument */
say arg(1)
</syntaxhighlight>
=={{header|Ruby}}==
Line 742 ⟶ 751:
=={{header|Sidef}}==
Sidef is a scripting language and does not compile to a binary.
<
say ARGV.join(" ")</
{{out}}
Line 755 ⟶ 764:
'''File: echo.swift'''
<
import Foundation
print(Process.arguments[1..<Process.arguments.count].joinWithSeparator(" "))
</syntaxhighlight>
'''Usage:'''
<
./echo.swift Hello, world!
</syntaxhighlight>
{{Out}}
Line 779 ⟶ 788:
'''File: echo.sh'''
<
echo "$@"</
'''Usage:'''
Line 797 ⟶ 806:
'''File: script_gcc.sh'''
<
# Actual shebang when using bash:
Line 860 ⟶ 869:
echo "$binpath: executable not available" 1>&2
exit $ENOENT
fi</
'''Test Source File: echo.c'''
<
#include <stdio.h>
#include <string.h>
Line 875 ⟶ 884:
putchar('\n');
exit(EXIT_SUCCESS);
}</
'''Test Execution:'''
Line 885 ⟶ 894:
Hello, world!
</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
$ cat file.v
#!/usr/local/bin/v run
println('hello')
$ chmod 755 file.v
$ ./file.v
============ running ./file ============
hello
</syntaxhighlight>
V also knows to compile & run .vsh files immediately, so you do not need a separate step to compile them.
An example deploy.vsh:
<syntaxhighlight lang="Vlang">
#!/usr/bin/env -S v
// Note: The shebang line above, associates the .vsh file to V on Unix-like systems,
// so it can be run just by specifying the path to the .vsh file...
</syntaxhighlight>
=={{header|Wren}}==
Normally, Process.arguments[0] would return the (first) command line argument but here we need to use Process.arguments[1] because the first argument passed to Wren's command line interpreter is ''./native_shebang.wren''.
<
import "os" for Process
System.print(Process.arguments[1])</
{{out}}
Line 903 ⟶ 933:
Since the #! parsing is done by a compiler front end and was designed to be used from the command line, we'll do that by forking zkl to compile the source if it is newer than the binary.
<
// This file: nativeShebang.zkl, compiles to nativeShebang.zsc
// zkl --#! . -c nativeShebang -o.
Line 923 ⟶ 953:
////////////// the script:
println("Hello world!");</
{{out}}
<pre>
|