Scripting karaf gogo shell with mhu-osgi

Scripting karaf shell could be very constricted. Therefor an extension could help to work more with the possibilities of the karaf engine.

The main reason to enable more scripting are the possibility to find easy maintenance solutions without starting a software project for every small task. (The same reason bash become popular.)

The main extensions are ‘shell:bash’ and ‘shell:run’.

shell:bash

This extension only works for unix systems. It clones the ‘shell:exec’ command and makes it easier to execute bash scripts and bring it together with gogo shell.

To use it you also need the commands ‘shell:read’ and ‘shell:write’ to work with files. The commands will be discussed later.

The following example writes the output of bundle:list to a file and executes bash script.

bundle:list|write test.txt
bash "cat test.txt|cut -d \\| -f 5"

shell:run

More helpful will be the ‘run’ command. You can run scripts that can be extended with block instructions.

Use the -c option to use the inline version. Use -d to debug the execution. Example:

bundle:list|run -c “for i in read *;echo Start \$i End;done”

An instruction must be in a separate line to be interpreted. The following instructions are possible:

  • if <condition> : start of a conditional block
  • elseif <condition> : another condition block if the condition before was not true
  • else : otherwise block
  • endif : end of the condition block
  • while <condition> : start of a while loop
  • for <var> in <list> : start of a for loop. The list could be a iterable object or a string. The string will be split by New Line.
  • done : end of a loop block
  • label <label> : label declaration
  • goto <label> : jump to a label
  • exit : exit execution

The following special instructions are:

# commend

#–
commend block
–#

bash:read

Reading from a file or stdin. Use ‘read <filename>’ to read the content of a file. The content will send to the pipe. Use the ‘-o’ option to set it as variable content.

If you want to read the hole content from the pipe and send it to the next pipe or set it as variable content use a star ‘*’ instead of the file name.

If you want to read only one line from the user input do not set a file name. The content of the line will be send to the pipe or set the variable with ‘-o’.

Examples:

karaf@root()> read -p prompt> 
prompt>Hello World!
Hello World!
karaf@root()> read -p secure> -s
secure>************
Hello World!
karaf@root()> read -p secure> -s -o secure
secure>***********
karaf@root()> echo ${secure}
Secure HeHe
karaf@root()>

bash:write

Write the pipe into a file. Use a star ‘*’ instead of the file name to send it to the next pipe consumer.

Example:

package:exports | write exports.txt

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s