Shell

JavaScript的外壳提供了一个简单的方法来在批处理模式下或编程的一个探索性的互动环境运行脚本.

调用Shell

<big>java org.mozilla.javascript.tools.shell.Main [options] script-filename-or-url [script-arguments] </big>

这里是一些选项 options

-e script-source

script-source  作为 JavaScript 执行.

-f script-filename-or-url

读取script-filename-or-url 的内容并作为JavaScript执行.

-opt optLevel / -O optLevel

优化等级 optLevel, 必须是 -1 或 [0-9]之间的整数. 参考Rhino Optimization 或许详情.

-version versionNumber

制定语言编译版本. 字符串 versionNumber 必须是 100, 110, 120, 130, 140, 150, 160170. 参考JavaScript Language Versions获取更多的语言版本信息.

-strict

开启strict 模式.

-continuations

启用continuation的实验支持,建立优化级别为-1强制解释模式. 从Rhino 1.7 开始这个选项不再可用.

注意

如果shell被使用设置为true的系统属性rhino.use_java_policy_security调用,一个安全管理将被安装, shell限制脚本通过基于根据URL访问Java安全选项的权限.如果JVM实现Java2的安全模型才可以用.

预定义的属性

在shell中呗执行的脚本访问顶层定义的一些属性.

arguments

当shell被调用时,arguments 对象是一个包含所有通过命令行输入的字符串的数组

environment

返回当前环境对象.

history

显示命令行中执行命令的历史.

help()

获取帮助信息.

defineClass(className)

定义一个使用Java类命名的扩展,参数 className是个类名的字符串. 使用 ScriptableObject.defineClass() 定义扩展.

deserialize(filename)

从指定的文件恢复以前通过调用一个被serialize的对象.

gc()

运行垃圾回收器.

load([filename, ...])

载入字符串命名的参数JavaScript源码文件. 如果有多个参数被传递,那么每一个参数都会被读取并执行.

loadClass(className)

载入并执行字符串className.表示的类.这个类必须是已实现的脚本接口, 就像任何被编译的脚本 Rhino JavaScript Compiler.

print([expr ...])

评估并打印表达式. 评估每一个表达式, 把结果转化为字符串并打印.

readFile(path [, characterCoding])

读取给定的文件并使用指定的编码把字节转换为字符串,如果没有制定编码就用默认编码.

readUrl(url [, characterCoding])

打开指定的url,读取所有的字节并用指定的编码转化为数组,如果没有指定编码就是用默认的.

runCommand(commandName, [arg, ...] [options])

执行参数中给定的命令,作为一个独立的进程选项并返回该进程的退出状态.

Usage:

runCommand(command)
runCommand(command, arg1, ..., argN)
runCommand(command, arg1, ..., argN, options)

All except the last arguments to runCommand are converted to strings and denote command name and its arguments. If the last argument is a JavaScript object, it is an option object. Otherwise it is converted to string denoting the last argument and options objects assumed to be empty.

The following properties of the option object are processed:

  • args - provides an array of additional command arguments
  • env - explicit environment object. All its enumeratable properties define the corresponding environment variable names.
  • input - the process input. If it is not java.io.InputStream, it is converted to string and sent to the process as its input. If not specified, no input is provided to the process.
  • output - the process output instead of java.lang.System.out. If it is not instance of java.io.OutputStream, the process output is read, converted to a string, appended to the output property value converted to string and put as the new value of the output property.
  • err - the process error output instead of java.lang.System.err. If it is not instance of java.io.OutputStream, the process error output is read, converted to a string, appended to the err property value converted to string and put as the new value of the err property.

seal(object)

Seal the specified object so any attempt to add, delete or modify its properties would throw an exception.

serialize(object, filename)

Serialize the given object to the specified file.

spawn(functionOrScript)

Run the given function or script in a different thread.

sync(function)

creates a synchronized function (in the sense of a Java synchronized method) from an existing function. The new function synchronizes on the this object of its invocation.

quit()

退出shell. 如果文件字符是在提示符下键入,shell将也退出在交互模式下.

version([number])

设置或获取JavaScript的版本号. 如果没有参数,就返回当前的版本. 如果有参数,参数要在 100, 110, 120, 130, 140, 150, 160 或 170范围内,它们分别对应 JavaScript1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6 or 1.7.

例子

调用

Here the shell is invoked three times from the command line. (The system command prompt is shown as $.) The first invocation executes a script specified on the command line itself. The next invocation has no arguments, so the shell goes into interactive mode, reading and evaluating each line as it is typed in. Finally, the last invocation executes a script from a file and accesses arguments to the script itself.

$ java org.mozilla.javascript.tools.shell.Main -e print('hi')
hi
$ java org.mozilla.javascript.tools.shell.Main
js> print('hi')
hi
js> 6*7
42
js> function f() {
  return a;
}
js> var a = 34;
js> f()
34
js> quit()
$ cat echo.js
for (i in arguments) {
  print(arguments[i])
}
$ java org.mozilla.javascript.tools.shell.Main echo.js foo bar
foo
bar
$

spawn 和 sync 

下面的例子创建了2个线程 via spawn 和 uses sync 去创建同步的函数test test版本.

js> function test(x) {
  print("entry");
  java.lang.Thread.sleep(x*1000);
  print("exit");
}
js> var o = { f : sync(test) };
js> spawn(function() {o.f(5);});
Thread[Thread-0,5,main]
entry
js> spawn(function() {o.f(5);});
Thread[Thread-1,5,main]
js>
exit
entry
exit

运行命令

这是在Linux下调用 runCommand 的例子.

js> runCommand('date')
Thu Jan 23 16:49:36 CET 2003
0
// Using input option to provide process input
js> runCommand("sort", {input: "c\na\nb"})
a
b
c
0
js> // Demo of output and err options
js> var opt={input: "c\na\nb", output: 'Sort Output:\n'}
js> runCommand("sort", opt)
0
js> print(opt.output)
Sort Output:
a
b
c
js> var opt={input: "c\na\nb", output: 'Sort Output:\n', err: ''}
js> runCommand("sort", "--bad-arg", opt)
2
js> print(opt.err)
/bin/sort: unrecognized option `--bad-arg'
Try `/bin/sort --help' for more information.

js> runCommand("bad_command", "--bad-arg", opt)
js: "<stdin>", line 18: uncaught JavaScript exception: java.io.IOException: bad_command: not found
js> // Passing explicit environment to the system shell
js> runCommand("sh", "-c", "echo $env1 $env2", { env: {env1: 100, env2: 200}})
100 200
0
js> // Use args option to provide additional command arguments
js> var arg_array = [1, 2, 3, 4];
js> runCommand("echo", { args: arg_array})
1 2 3 4
0

Windows下的例子也差不多:

js> // Invoke shell command
js> runCommand("cmd", "/C", "date /T")
27.08.2005
0
js> // Run sort collectiong the output 
js> var opt={input: "c\na\nb", output: 'Sort Output:\n'}
js> runCommand("sort", opt)
0
js> print(opt.output)
Sort Output:
a
b
c
js> // Invoke notepad and wait until it exits
js> runCommand("notepad")
0

文档标签和贡献者

 此页面的贡献者: ziyunfei, OoOoOoOo, bility
 最后编辑者: ziyunfei,