J #! script

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help User

# Unix J #! script

A #! J script (hash bang J script) is an executable text file with a first line that gives the full path the jconsole binary.Try the following:
create file sumsquares with 3 lines of text:
#!/usr/local/bin/jconsole
echo +/*:0".>,.2}.ARGV
exit''
make it executable ( chmod +x ) and run it
./sumsquares 1 2 3 4 5
Use NB. to comment out the exit'' to stay in J.
The following loads profile, which loads the #! script, which echos the result on the console and leaves J running.
#!/bin/jconsole
load'strings'
echo 'abcXXXdef' rplc 'XXX';' insert '
The following is the same, except it exits J at the end.
#!/bin/jconsole
load'strings'
echo 'abcXXXdef' rplc 'XXX';' insert '
exit''
The following doesn't load profile and just loads the script.
#!jconsole -jprofile
...
Profile loads jconsole with the following definitions that are useful in #! J scripts:
ARGV - boxed list of jconsole, script name, and arguments
echo - format and display output
getenv - get value of environment variable
stdin - read from standard input
stdout - write to standard output
stderr - write to standard error
exit - exit J (arg is return code)
stdin is defined with stdout as its obverse (see the :. conjunction). When used with &. (under conjunction), as in foo&.stdin '' stdin is first called, reading all of standard input. That input is the argument to foo, and the result is passed to the inverse of stdin, which is stdout. A verb which transforms a character list can be combined with the stdin verb with under to apply the transformation as a Unix filter. As an example we will create a Unix filter which reverses all the characters in a file. Rather than just using |. we'll use (|.@}: , {:) which reverses all but the last character, and appends the last character to it. For files which end in a newline, this reverses the file keeping that newline at the end. Define the #! J script reverse as follows:

    #!/usr/local/bin/jconsole
    rev=. |.@}: , {:
    rev&.stdin ''
    exit''

If you wanted to do a complete reverse of a file which does not end in a newline you could do the following:
rev=. |.`(|.@}: , {:)@.(LF&=@{:)
echo uses 1!:2 to write to J output (file number 2) and formats and writes any J array. stdout and stderr , however, must be given character lists, and writes them unaltered. In particular, echo 'a line' will write a trailing newline character whereas stdout 'a line' does not.

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help User