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.
Unix - jconsole - stdin and stdout
The verb defined below calls a program, writes to its standard input, and reads its output.
run=: 4 : 0
'p o i'=. 2!:2 x. NB. Run command, save Process, Output, Input
y. fwrite i NB. Write to its input
fclose i NB. Close its input
2!:3 p NB. Wait for process to terminate
z=.fread o NB. Read its output
fclose o NB. Close its output
z NB. Result
)
Starting J - tech details
In Windows, 1!:45 returns profile.ijs in the path of the J Front End (j.exe or jconsole.exe). For example: c:\j502a\profile.ijs.
In Unix, 1!:45 uses environment variables and the J version to determine the default profile. The version is the text in 9!:14'' up to the first /. If 9!:14'' returned j502a/2003-05-01/16:45, then the version is j502a. If HOME/version/profile.ijs exists, then it is the default profile. For example, if HOME was /home/eric, then /home/eric/j502a/profile.ijs would be the default profile if it existed. If that file doesn't exist, environment variable JPATHversion (for example, JPATHj502a), if it is defined, is the default profile.
Normally J is initialized by the JFE with:
(3 : '0!:0 y.')<1!:45''[ARGV_z_=:...
The JFE command line is given to the JE by setting ARGV_z_. The 1!:45'' returns the full path to the default profile. The profile is loaded by an explicit verb and it must use =: for global assignments.
The default profile defines PROFILE_z_ (if not already defined) as 1!:45''. This makes it easy for a stub profile to redirect to another profile.
-jprofile as the first parameter with additional parameters initializes J with:
(3 : '0!:0 y.')2{ARGV_z_=:...
That is, the parameter after -jnoprofile is loaded instead of the standard profile.
Previous Release Highlights
See