Native shebang: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 55: | Line 55: | ||
Also note that this ".so" will only be generated if the ".a68" source file |
Also note that this ".so" will only be generated if the ".a68" source file |
||
has been touched. |
has been touched. |
||
'''File: echo.a68'''< |
'''File: echo.a68'''<syntaxhighlight lang="algol68">#!/usr/bin/a68g --script # |
||
# -*- coding: utf-8 -*- # |
# -*- coding: utf-8 -*- # |
||
STRING ofs := ""; |
STRING ofs := ""; |
||
FOR i FROM 4 TO argc DO print((ofs, argv(i))); ofs:=" " OD</ |
FOR i FROM 4 TO argc DO print((ofs, argv(i))); ofs:=" " OD</syntaxhighlight> |
||
'''Test Execution:''' |
'''Test Execution:''' |
||
<pre> |
<pre> |
||
Line 71: | Line 71: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
Arturo is a scripting language and does not compile to a binary. |
Arturo is a scripting language and does not compile to a binary. |
||
< |
<syntaxhighlight lang="rebol">#!/usr/bin/env arturo |
||
print "Hello from Arturo!"</ |
print "Hello from Arturo!"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 86: | Line 86: | ||
'''File: script_gcc.c''' |
'''File: script_gcc.c''' |
||
< |
<syntaxhighlight lang="c">#!/usr/local/bin/script_gcc.sh |
||
/* Optional: this C code initially is-being/can-be boot strapped (compiled) using bash script_gcc.sh */ |
/* Optional: this C code initially is-being/can-be boot strapped (compiled) using bash script_gcc.sh */ |
||
#include <errno.h> |
#include <errno.h> |
||
Line 210: | Line 210: | ||
perror(STRCAT(binpath, ": executable not available", ENDCAT)); |
perror(STRCAT(binpath, ": executable not available", ENDCAT)); |
||
exit(errno); |
exit(errno); |
||
}</ |
}</syntaxhighlight> |
||
'''Test Source File: echo.c''' |
'''Test Source File: echo.c''' |
||
< |
<syntaxhighlight lang="c">#!/usr/local/bin/script_gcc.c |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 226: | Line 226: | ||
putchar('\n'); |
putchar('\n'); |
||
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
||
}</ |
}</syntaxhighlight> |
||
'''Test Execution:''' |
'''Test Execution:''' |
||
Line 239: | Line 239: | ||
===2nd version. Pure C, no extra bash script=== |
===2nd version. Pure C, no extra bash script=== |
||
'''File: script_gcc.c''' |
'''File: script_gcc.c''' |
||
< |
<syntaxhighlight lang="c">/* |
||
* rosettacode.org: Native shebang |
* rosettacode.org: Native shebang |
||
* |
* |
||
Line 410: | Line 410: | ||
return !fprintf(stderr, "%s : executable not available\n", exec_path) | ENOENT; |
return !fprintf(stderr, "%s : executable not available\n", exec_path) | ENOENT; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Test Source File: echo.c''' |
'''Test Source File: echo.c''' |
||
< |
<syntaxhighlight lang="c">#!/usr/local/bin/script_gcc |
||
/* |
/* |
||
* note, any additional libs or include paths would have params added after |
* note, any additional libs or include paths would have params added after |
||
Line 431: | Line 431: | ||
putchar('\n'); |
putchar('\n'); |
||
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
||
}</ |
}</syntaxhighlight> |
||
'''Test Execution:''' |
'''Test Execution:''' |
||
Line 444: | Line 444: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Such functionality can be added easily by the following definition: |
Such functionality can be added easily by the following definition: |
||
<lang>: #! [COMPILE] \ ; immediate</ |
<syntaxhighlight lang="text">: #! [COMPILE] \ ; immediate</syntaxhighlight> |
||
Some Forth compilers - like 4tH or Gforth - support this functionality out of the box. |
Some Forth compilers - like 4tH or Gforth - support this functionality out of the box. |
||
This example program works as advertised: |
This example program works as advertised: |
||
<lang>#! /usr/local/bin/4th cxq |
<syntaxhighlight lang="text">#! /usr/local/bin/4th cxq |
||
argn 1 ?do i args type space loop cr</ |
argn 1 ?do i args type space loop cr</syntaxhighlight> |
||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
Line 464: | Line 464: | ||
The following works fine on Ubuntu 16.04. |
The following works fine on Ubuntu 16.04. |
||
< |
<syntaxhighlight lang="go">///usr/bin/env go run echo.go "$@"; exit |
||
package main |
package main |
||
Line 476: | Line 476: | ||
fmt.Println(os.Args[1]) |
fmt.Println(os.Args[1]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 487: | Line 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: |
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: |
||
< |
<syntaxhighlight lang="j">#!/usr/local/bin/ijconsole |
||
echo each ARGV</ |
echo each ARGV</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 502: | Line 502: | ||
'''Example 1:''' |
'''Example 1:''' |
||
< |
<syntaxhighlight lang="sh">$ cat echo.foo |
||
#!/usr/bin/env/jq -M -n -r -f |
#!/usr/bin/env/jq -M -n -r -f |
||
"Klaatu barada nikto!"</ |
"Klaatu barada nikto!"</syntaxhighlight> |
||
Line 518: | Line 518: | ||
'''Example 2:''' |
'''Example 2:''' |
||
< |
<syntaxhighlight lang="sh">$ cat echo.foo |
||
#!/usr/bin/env/jq -M -n -r -f |
#!/usr/bin/env/jq -M -n -r -f |
||
$x</ |
$x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ ./echo.foo --arg x "Hello, world!" |
||
Hello, world!</ |
Hello, world!</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
usage: ./thisfile.jl "hello" |
usage: ./thisfile.jl "hello" |
||
< |
<syntaxhighlight lang="julia">#!/usr/local/bin/julia |
||
# Put the Julia code below this line. It will be compiled and run. |
# Put the Julia code below this line. It will be compiled and run. |
||
Line 534: | Line 534: | ||
println(ARGS) |
println(ARGS) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
_ |
_ |
||
Line 551: | Line 551: | ||
'''File: echo.langur''' |
'''File: echo.langur''' |
||
< |
<syntaxhighlight lang="langur">#!/usr/bin/langur |
||
writeln join " ", _args</ |
writeln join " ", _args</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
Line 568: | Line 568: | ||
'''File: nativeshebang.nim''' |
'''File: nativeshebang.nim''' |
||
< |
<syntaxhighlight lang="nim">#!/usr/bin/env -S nim c -r --hints:off |
||
import os,strutils |
import os,strutils |
||
echo commandLineParams().join(" ")</ |
echo commandLineParams().join(" ")</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
<pre>./nativeshebang.nim hello, world</pre> |
<pre>./nativeshebang.nim hello, world</pre> |
||
Line 579: | Line 579: | ||
'''File: nim.cfg''' |
'''File: nim.cfg''' |
||
<lang |
<syntaxhighlight lang="nim">--hints:off</syntaxhighlight> |
||
'''File: nativeshebang2.nims''' |
'''File: nativeshebang2.nims''' |
||
< |
<syntaxhighlight lang="nim">#!nim r |
||
import os,strutils |
import os,strutils |
||
echo commandLineParams().join(" ")</ |
echo commandLineParams().join(" ")</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
<pre>./nativeshebang2.nim hello, world</pre> |
<pre>./nativeshebang2.nim hello, world</pre> |
||
Line 593: | Line 593: | ||
'''File: echo.ml''' |
'''File: echo.ml''' |
||
< |
<syntaxhighlight lang="ocaml">#! /usr/bin/env ocaml |
||
let () = |
let () = |
||
let argl = Array.to_list Sys.argv in |
let argl = Array.to_list Sys.argv in |
||
print_endline (String.concat " " (List.tl argl))</ |
print_endline (String.concat " " (List.tl argl))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 615: | Line 615: | ||
'''File: echo.pl''' |
'''File: echo.pl''' |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
print "@ARGV\n"; |
print "@ARGV\n"; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Usage:''' |
'''Usage:''' |
||
Line 633: | Line 633: | ||
and compilation, apart from the executable file, is a -c flag on the command line, which I recommend |
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): |
omitting unless it proves helpful or necessary. Example (quietly ignored by pwa/p2js, and Phix in general): |
||
< |
<syntaxhighlight lang="phix">#!/path/to/phix</syntaxhighlight> |
||
You can also invoke the compiler directly as follows |
You can also invoke the compiler directly as follows |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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: #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> |
<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 641: | Line 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: #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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
See also demo/capture_console.exw which redirects stdin/out/err while interpreting a child process. |
See also demo/capture_console.exw which redirects stdin/out/err while interpreting a child process. |
||
Line 652: | Line 652: | ||
'''File: echo.py''' |
'''File: echo.py''' |
||
< |
<syntaxhighlight lang="python">#!/path/to/python |
||
# Although `#!/usr/bin/env python` may be better if the path to python can change |
# Although `#!/usr/bin/env python` may be better if the path to python can change |
||
import sys |
import sys |
||
print " ".join(sys.argv[1:])</ |
print " ".join(sys.argv[1:])</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
Line 682: | Line 682: | ||
File <tt>native-shebang.rkt</tt> contains the following: |
File <tt>native-shebang.rkt</tt> contains the following: |
||
< |
<syntaxhighlight lang="racket">#! /usr/local/racket-6.1/bin/racket |
||
#lang racket |
#lang racket |
||
(displayln "hello")</ |
(displayln "hello")</syntaxhighlight> |
||
My directory contains only this: |
My directory contains only this: |
||
Line 718: | Line 718: | ||
'''File: echo.p6''' |
'''File: echo.p6''' |
||
<lang |
<syntaxhighlight lang="raku" line>#!/path/to/raku |
||
put @*ARGS;</ |
put @*ARGS;</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
Line 733: | Line 733: | ||
===Unix shebang=== |
===Unix shebang=== |
||
Using e.g. Regina open source REXX interpreter |
Using e.g. Regina open source REXX interpreter |
||
< |
<syntaxhighlight lang="rexx"> |
||
#!/usr/local/bin/regina |
#!/usr/local/bin/regina |
||
/* Echo the command line argument */ |
/* Echo the command line argument */ |
||
say arg(1) |
say arg(1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===ARexx=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="rexx"> |
||
/* Echo the command line argument */ |
/* Echo the command line argument */ |
||
say arg(1) |
say arg(1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 751: | Line 751: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Sidef is a scripting language and does not compile to a binary. |
Sidef is a scripting language and does not compile to a binary. |
||
< |
<syntaxhighlight lang="ruby">#!/usr/bin/sidef |
||
say ARGV.join(" ")</ |
say ARGV.join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 764: | Line 764: | ||
'''File: echo.swift''' |
'''File: echo.swift''' |
||
< |
<syntaxhighlight lang="swift">#!/usr/bin/swift |
||
import Foundation |
import Foundation |
||
print(Process.arguments[1..<Process.arguments.count].joinWithSeparator(" ")) |
print(Process.arguments[1..<Process.arguments.count].joinWithSeparator(" ")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Usage:''' |
'''Usage:''' |
||
< |
<syntaxhighlight lang="bash"> |
||
./echo.swift Hello, world! |
./echo.swift Hello, world! |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 788: | Line 788: | ||
'''File: echo.sh''' |
'''File: echo.sh''' |
||
< |
<syntaxhighlight lang="sh">#!/bin/sh |
||
echo "$@"</ |
echo "$@"</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
Line 806: | Line 806: | ||
'''File: script_gcc.sh''' |
'''File: script_gcc.sh''' |
||
< |
<syntaxhighlight lang="bash">#!/bin/bash |
||
# Actual shebang when using bash: |
# Actual shebang when using bash: |
||
Line 869: | Line 869: | ||
echo "$binpath: executable not available" 1>&2 |
echo "$binpath: executable not available" 1>&2 |
||
exit $ENOENT |
exit $ENOENT |
||
fi</ |
fi</syntaxhighlight> |
||
'''Test Source File: echo.c''' |
'''Test Source File: echo.c''' |
||
< |
<syntaxhighlight lang="c">#!/usr/local/bin/script_gcc.sh |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 884: | Line 884: | ||
putchar('\n'); |
putchar('\n'); |
||
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
||
}</ |
}</syntaxhighlight> |
||
'''Test Execution:''' |
'''Test Execution:''' |
||
Line 897: | Line 897: | ||
=={{header|Wren}}== |
=={{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''. |
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''. |
||
< |
<syntaxhighlight lang="ecmascript">#!/bin/wren native_shebang.wren |
||
import "os" for Process |
import "os" for Process |
||
System.print(Process.arguments[1])</ |
System.print(Process.arguments[1])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 912: | Line 912: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="zkl">#!/home/craigd/Bin/zkl |
||
// This file: nativeShebang.zkl, compiles to nativeShebang.zsc |
// This file: nativeShebang.zkl, compiles to nativeShebang.zsc |
||
// zkl --#! . -c nativeShebang -o. |
// zkl --#! . -c nativeShebang -o. |
||
Line 932: | Line 932: | ||
////////////// the script: |
////////////// the script: |
||
println("Hello world!");</ |
println("Hello world!");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |