Shell syntax

Easy to use interactively - no unnecessary syntax

// simple command
karaf@root> echo hello world
hello world

// session variables
karaf@root> msg = "hello world"
hello world
karaf@root> echo $msg
hello world

// execution quotes () - similar to bash backquotes
karaf@root> (bundle 1) location
mvn:org.ops4j.pax.url/pax-url-mvn/1.1.3

List, maps, pipes and closures

// lists - []
karaf@root> list = [1 2 a b]
1
2
a
b

karaf@root> map = [Jan=1 Feb=2 Mar=3]
Jan                 1
Feb                 2
Mar                 3

// pipes
karaf@root> bundles | grep felix
000000 ACT org.apache.felix.framework-3.0.2
000005 ACT org.apache.felix.configadmin-1.2.4
000006 ACT org.apache.felix.fileinstall-3.0.2

// closures - {}
karaf@root> echo2 = { echo xxx $args yyy }
org.apache.felix.gogo.runtime.shell.Closure@2ffb36c2
karaf@root> echo2 hello world
xxx hello world yyy

Leverages existing Java capabilities, via reflection

// exception handling - console shows summary, but full context available
karaf@root> start xxx
Error executing command osgi:start: unable to convert argument ids with value '[xxx]' to type java.util.List<java.lang.Long>
karaf@root> $karaf.lastException printStackTrace
org.apache.felix.gogo.commands.CommandException: Unable to convert argument ids with value '[xxx]' to type java.util.List<java.lang.Long>
	at org.apache.felix.gogo.commands.basic.DefaultActionPreparator.prepare(DefaultActionPreparator.java:347)
	at org.apache.felix.gogo.commands.basic.AbstractCommand.execute(AbstractCommand.java:34)
	at org.apache.felix.gogo.runtime.shell.CommandProxy.execute(CommandProxy.java:50)
	at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:229)
	at org.apache.felix.gogo.runtime.shell.Closure.executeStatement(Closure.java:162)
	at org.apache.felix.gogo.runtime.shell.Pipe.run(Pipe.java:101)
	at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:79)
	at org.apache.felix.gogo.runtime.shell.CommandSessionImpl.execute(CommandSessionImpl.java:71)
	at org.apache.karaf.shell.console.jline.Console.run(Console.java:169)
	at java.lang.Thread.run(Thread.java:637)
Caused by: java.lang.Exception: Unable to convert from [xxx] to java.util.List<java.lang.Long>(error converting collection entry)
	at org.apache.aries.blueprint.container.AggregateConverter.convertToCollection(AggregateConverter.java:318)
	at org.apache.aries.blueprint.container.AggregateConverter.convert(AggregateConverter.java:159)
	at org.apache.karaf.shell.console.commands.BlueprintCommand$BlueprintActionPreparator.convert(BlueprintCommand.java:73)
	at org.apache.felix.gogo.commands.basic.DefaultActionPreparator.prepare(DefaultActionPreparator.java:344)
	... 9 more
Caused by: java.lang.NumberFormatException: For input string: "xxx"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Long.parseLong(Long.java:410)
	at java.lang.Long.valueOf(Long.java:525)
	at org.apache.aries.blueprint.container.AggregateConverter.convertFromString(AggregateConverter.java:261)
	at org.apache.aries.blueprint.container.AggregateConverter.convert(AggregateConverter.java:151)
	at org.apache.aries.blueprint.container.AggregateConverter.convertToCollection(AggregateConverter.java:316)
	... 12 more

// add all public methods on java.lang.System as commands:
karaf@root> addcommand system (loadClass java.lang.System)
karaf@root> system:getproperty karaf.name
root

// create new objects
karaf@root> map = (new java.util.HashMap)
karaf@root> $map put 0 0
karaf@root> $map
0                   0