搜索

16.9. 使用控制台

download PDF

16.9.1. 可用命令

要在控制台中查看可用命令列表,您可以使用 帮助

karaf@root()> help
bundle                            Enter the subshell
bundle:capabilities               Displays OSGi capabilities of a given bundles.
bundle:classes                    Displays a list of classes/resources contained in the bundle
bundle:diag                       Displays diagnostic information why a bundle is not Active
bundle:dynamic-import             Enables/disables dynamic-import for a given bundle.
bundle:find-class                 Locates a specified class in any deployed bundle
bundle:headers                    Displays OSGi headers of a given bundles.
bundle:id                         Gets the bundle ID.
...

您有带有简短描述的所有命令列表。

您可以使用 tab 键获取所有命令的快速列表:

karaf@root()> Display all 294 possibilities? (y or n)
...

16.9.2. subshell 和 completion 模式

命令具有范围和名称。例如,命令 feature:list 具有 scope,并列出 为 name。

2008 年按范围"组"命令。每个范围组成一个子 shell。

您可以使用全限定名称(scope:name)直接执行命令:

karaf@root()> feature:list
...

或者,在子 shell 中输入命令上下文到子 shell:

karaf@root()> feature
karaf@root(feature)> list

请注意,您可以通过输入子 shell 名称(此处为 功能)直接输入子 shell。您可以直接从子 shell "switch" 改为另一个:

karaf@root()> feature
karaf@root(feature)> bundle
karaf@root(bundle)>

提示符显示()之间的当前子 shell。

exit 命令进入父子 shell:

karaf@root()> feature
karaf@root(feature)> exit
karaf@root()>

完成模式定义了 tab 键和 help 命令的行为。

您有三种不同的模式可用:

  • 全局
  • 第一个
  • SUBSHELL

您可以使用 etc/org.apache.karaf.shell.cfg 文件中的 completionMode 属性来定义默认的完成模式。默认情况下,您有:

completionMode = GLOBAL

您还可以使用 shell:completion 命令更改完成模式"实时" (使用 Karaf shell 控制台):

karaf@root()> shell:completion
GLOBAL
karaf@root()> shell:completion FIRST
karaf@root()> shell:completion
FIRST

shell: 补全 可以告知您当前使用的完成模式。您还可以提供您想要的新完成模式。

GLOBAL 完成模式是 Karaf 4.0.0 中的默认模式(主要用于转换目的)。

GLOBAL 模式并不实际使用 subshell:它与之前 Karaf 版本的行为相同。

当您输入 tab 键时,无论您在哪个子 shell 中,都会显示所有命令和所有别名:

karaf@root()> <TAB>
karaf@root()> Display all 273 possibilities? (y or n)
...
karaf@root()> feature
karaf@root(feature)> <TAB>
karaf@root(feature)> Display all 273 possibilities? (y or n)

FIRST 完成模式是 GLOBAL 完成模式的替代选择。

如果您在 root 级别 subshell 上输入 tab 键,则完成将显示所有子 shell 中的命令和别名(如 GLOBAL 模式)。但是,如果您在子 shell 中键入了 tab 键,则完成将仅显示当前子 shell 的命令:

karaf@root()> shell:completion FIRST
karaf@root()> <TAB>
karaf@root()> Display all 273 possibilities? (y or n)
...
karaf@root()> feature
karaf@root(feature)> <TAB>
karaf@root(feature)>
info install list repo-add repo-list repo-remove uninstall version-list
karaf@root(feature)> exit
karaf@root()> log
karaf@root(log)> <TAB>
karaf@root(log)>
clear display exception-display get log set tail

SUBSHELL completion 模式是实际的子 shell 模式。

如果您在根级别上输入 tab 键,则完成会显示子 shell 命令(转至子 shell)和全局别名。在子 shell 中后,如果您键入 TAB 密钥,则完成会显示当前子 shell 的命令:

karaf@root()> shell:completion SUBSHELL
karaf@root()> <TAB>
karaf@root()>
* bundle cl config dev feature help instance jaas kar la ld lde log log:list man package region service shell ssh system
karaf@root()> bundle
karaf@root(bundle)> <TAB>
karaf@root(bundle)>
capabilities classes diag dynamic-import find-class headers info install list refresh requirements resolve restart services start start-level stop
uninstall update watch
karaf@root(bundle)> exit
karaf@root()> camel
karaf@root(camel)> <TAB>
karaf@root(camel)>
backlog-tracer-dump backlog-tracer-info backlog-tracer-start backlog-tracer-stop context-info context-list context-start context-stop endpoint-list route-info route-list route-profile route-reset-stats
route-resume route-show route-start route-stop route-suspend

16.9.3. Unix 比如环境

Karaf 控制台提供完整的 Unix 环境,如环境。

16.9.3.1. help 或 man

我们已经看到了使用 help 命令来显示所有可用的命令。

但是,您也可以使用 help 命令获取有关命令或 help 命令的 man 命令的详细信息,该命令是 help 命令的别名。您还可以通过在命令中使用 --help 选项使用另一个表单来获取命令帮助。

因此这些命令

karaf@root()> help feature:list
karaf@root()> man feature:list
karaf@root()> feature:list --help

所有都生成相同的帮助输出:

DESCRIPTION
        feature:list

        Lists all existing features available from the defined repositories.

SYNTAX
        feature:list [options]

OPTIONS
        --help
                Display this help message
        -o, --ordered
                Display a list using alphabetical order
        -i, --installed
                Display a list of all installed features only
        --no-format
                Disable table rendered output

16.9.3.2. completion

当您输入 tab 键时,wam 会尝试完成:

  • subshell
  • commands
  • 别名
  • 命令参数
  • 命令选项

16.9.3.3. Alias

别名是与给定命令关联的另一个名称。

shell:alias 命令创建一个新的别名。例如,要将 list-installed-features 别名创建到实际 feature:list -i 命令,您可以:

karaf@root()> alias "list-features-installed = { feature:list -i }"
karaf@root()> list-features-installed
Name       | Version | Required | State   | Repository     | Description
------------------------------------------------------------------------------------------------------------------------------
feature    | 4.0.0   | x        | Started | standard-4.0.0 | Features Support
shell      | 4.0.0   | x        | Started | standard-4.0.0 | Karaf Shell
deployer   | 4.0.0   | x        | Started | standard-4.0.0 | Karaf Deployer
bundle     | 4.0.0   | x        | Started | standard-4.0.0 | Provide Bundle support
config     | 4.0.0   | x        | Started | standard-4.0.0 | Provide OSGi ConfigAdmin support
diagnostic | 4.0.0   | x        | Started | standard-4.0.0 | Provide Diagnostic support
instance   | 4.0.0   | x        | Started | standard-4.0.0 | Provide Instance support
jaas       | 4.0.0   | x        | Started | standard-4.0.0 | Provide JAAS support
log        | 4.0.0   | x        | Started | standard-4.0.0 | Provide Log support
package    | 4.0.0   | x        | Started | standard-4.0.0 | Package commands and mbeans
service    | 4.0.0   | x        | Started | standard-4.0.0 | Provide Service support
system     | 4.0.0   | x        | Started | standard-4.0.0 | Provide System support
kar        | 4.0.0   | x        | Started | standard-4.0.0 | Provide KAR (KARaf archive) support
ssh        | 4.0.0   | x        | Started | standard-4.0.0 | Provide a SSHd server on Karaf
management | 4.0.0   | x        | Started | standard-4.0.0 | Provide a JMX MBeanServer and a set of MBeans in

登录时,Apache Karaf 控制台会读取 etc/shell.init.script 文件,您可以在其中创建别名。它与 Unix 中的 bashrc 或 profile 文件类似。

ld = { log:display $args } ;
lde = { log:exception-display $args } ;
la = { bundle:list -t 0 $args } ;
ls = { service:list $args } ;
cl = { config:list "(service.pid=$args)" } ;
halt = { system:shutdown -h -f $args } ;
help = { *:help $args | more } ;
man = { help $args } ;
log:list = { log:get ALL } ;

您可以在默认情况下看到可用的别名:

  • ld 是显示日志的简短表单(用于 log:display 命令别名)
  • lde 是显示例外的简短表单(别名为 log:exception-display 命令)
  • la 是列出所有捆绑包的简短形式(别名到 bundle:list -t 0 命令)
  • ls 是列出所有服务的简短形式(别名到 service:list 命令)
  • CL 是一个简短的表单,用于列出所有配置(别名到 config:list 命令)
  • halt 是关闭 Apache Karaf 的简短形式(针对 system:shutdown -h -f 命令)
  • help 是显示帮助的简短形式(别名为 *:help 命令)
  • man 与 help ( help 命令别名)相同。
  • log:list 显示所有日志记录器和级别(用于 log:get ALL 命令)

您可以在 etc/shell.init.script 文件中创建自己的别名。

16.9.3.4. 密钥绑定

与大多数 Unix 环境一样,Ramon 控制台支持一些关键绑定:

  • 在命令历史记录中导航的方向键
  • CTRL-D 以注销/关闭 Karaf
  • CTRL-R 搜索之前执行的命令
  • CTRL-U 删除当前行

16.9.3.5. 管道

您可以将一个命令的输出作为输入传送到另一个命令的输出。它是使用 | 字符的管道:

karaf@root()> feature:list |grep -i war
pax-war                       | 4.1.4                            |          | Uninstalled | org.ops4j.pax.web-4.1.4  | Provide support of a full WebContainer
pax-war-tomcat                | 4.1.4                            |          | Uninstalled | org.ops4j.pax.web-4.1.4  |
war                           | 4.0.0                            |          | Uninstalled | standard-4.0.0           | Turn Karaf as a full WebContainer
blueprint-web                 | 4.0.0                            |          | Uninstalled | standard-4.0.0           | Provides an OSGI-aware Servlet ContextListener fo

16.9.3.6. grep, more, find, …​

Karaf 控制台提供一些类似于 Unix 环境的核心命令:

  • shell:alias 为现有命令创建一个别名
  • shell:cat 显示文件或 URL 的内容
  • shell:clear 清除当前控制台显示
  • shell:completion 显示或更改当前完成模式
  • shell:date 显示当前日期(可选使用格式)
  • shell:each 对参数列表执行冲突
  • shell:echo echoes 并将参数输出到 stdout
  • shell:edit 在当前文件或 URL 中调用文本编辑器
  • shell:env 显示或设置 shell 会话变量的值
  • shell:exec 执行系统命令
  • shell:grep 打印与给定模式匹配的行
  • shell:head 显示输入的第一行
  • shell:history 打印命令历史记录
  • shell:如果 允许您在脚本中使用条件(如果,则是其他块)
  • shell:info 打印有关当前 Karaf 实例的各种信息
  • shell:java 执行 Java 应用程序
  • shell:less file pager
  • shell:logout 断开 shell 与当前会话的连接
  • shell:more 是一个文件页器
  • shell:new 创建一个新的 Java 对象
  • shell:printf 格式和打印参数
  • shell:sleep sleep 用于一个位,然后唤醒
  • shell:sort 将所有文件的排序串联写入 stdout
  • shell:source 执行命令
  • 当执行命令抛出异常时,shell:stack-traces-print 会在控制台中打印完整的堆栈追踪
  • shell:tac 捕获 STDIN 并将其返回为字符串
  • shell:tail 显示输入的最后几行
  • shell:threads 打印当前线程
  • shell:watch 定期执行命令并刷新输出
  • shell:wc 会为每个文件打印换行符、字数和字节数
  • shell: while loop while the condition 为 true

您不必使用 命令的完全限定名称,只要它是唯一的,就可以直接使用该命令名称。因此您可以使用 'head' 而不是 'shell:head'

同样,您可以使用 help 命令或 --help 选项查找这些命令的详细信息和所有选项。

16.9.3.7. 脚本脚本

Apache Karaf 控制台支持完整的脚本语言,类似于 Unix 上的 bash 或 csh。

每个 (shell:each)命令可以迭代列表:

karaf@root()> list = [1 2 3]; each ($list) { echo $it }
1
2
3
注意

同一循环可以使用 shell 编写:同时 命令:

karaf@root()> a = 0 ; while { %((a+=1) <= 3) } { echo $a }
1
2
3

您可以自己创建列表(如上例中所示),或者某些命令也可以返回列表。

我们可以注意到,控制台会创建一个"会话"变量,其中包含您可以使用 $ list 访问的名称列表

$it 变量是与当前对象对应的隐式值(列表中当前迭代的值)。

使用 [] 创建列表时,Apache Karaf 控制台会创建一个 Java ArrayList。这意味着您可以使用 ArrayList 对象(如实例 get 或 size)中提供的方法:

karaf@root()> list = ["Hello" world]; echo ($list get 0) ($list get 1)
Hello world

我们可以在此处注意,在对象上调用方法直接使用 (对象方法参数)。这里 ($list get 0) 表示 $list.get (0),其中 $list 是 ArrayList。

表示法将显示有关对象的详情:

karaf@root()> $list class
...
ProtectionDomain     ProtectionDomain  null
 null
 <no principals>
 java.security.Permissions@6521c24e (
 ("java.security.AllPermission" "<all permissions>" "<all actions>")
)


Signers              null
SimpleName           ArrayList
TypeParameters       [E]

您可以将变量"广播"到指定类型。

karaf@root()> ("hello world" toCharArray)
[h, e, l, l, o,  , w, o, r, l, d]

如果失败,您将看到 casting 异常:

karaf@root()> ("hello world" toCharArray)[0]
Error executing command: [C cannot be cast to [Ljava.lang.Object;

您可以使用 shell:source 命令"调用"脚本:

karaf@root> shell:source script.txt
True!

其中 script.txt 包含:

foo = "foo"
if { $foo equals "foo" } {
  echo "True!"
}
注意

在编写脚本时,空格非常重要。例如,以下脚本不正确:

if{ $foo equals "foo" } ...

并使用以下内容失败:

karaf@root> shell:source script.txt
Error executing command: Cannot coerce echo "true!"() to any of []

由于 if 语句后缺少空格。

对于别名,您可以在 etc/shell.init.script 文件中创建初始化脚本。您还可以使用别名命名脚本。实际上,别名只是脚本。

详情请参阅开发人员指南中的脚本部分。

16.9.4. 安全性

Apache Karaf 控制台支持基于角色的访问控制(RBAC)安全机制。这意味着,根据连接到控制台的用户,您可以定义取决于用户的组和角色、执行某些命令的权限或限制参数允许的值。

此用户指南的 Security 部分中详述了 控制台安全性。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.