The commands in the gogo
scope provide a few additional tools that are especially useful when scripting a recurrent set of operations. We will cover some of them here.
The echo
command will evaluate its arguments and display the result on the console. For example:
g! var1 = 'this is'
this is
g! var2 = ' a string'
a string
g! echo $var1$var2
this is a string
The grep
command is used to search the input for lines that match a given pattern; it is very similar to the Unix grep
tool. The input is either standard input, the output of a command piped to grep, or the contents of files.
The grep
command will return false
if there were no lines in the input that match the pattern. It will return true
otherwise.
Its usage is as follows:
g! grep -?
grep - search for PATTERN in each FILE or standard input.
Usage: grep [OPTIONS] PATTERN [FILES]
-? --help show help
-i --ignore-case ignore case distinctions
-n --line-number prefix each line with line number within its input file
-q --quiet, --silent suppress all normal output
-v --invert-match select non-matching lines
true
The PATTERN
argument is an encoded regular expression (regex) that defines the sequence of characters that are considered a match. It follows the regex
pattern format, defined for the Java java.util.regex.Pattern
class.
You can visit the following for more information on regex:
http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
For example, to grep the output of the lb
command for lines containing the string Apache:
g! lb | grep Apache
1|Active | 1|Apache Felix Bundle Repository (1.6.2)
2|Active | 1|Apache Felix Gogo Command (0.6.0)
3|Active | 1|Apache Felix Gogo Runtime (0.6.0)
4|Active | 1|Apache Felix Gogo Shell (0.6.0)
true
The -i
(short for --ignore-case)
option makes the pattern not case sensitive, thus matching both the lowercase and uppercase for a letter.
The -n
(or --line-number)
option requests that the command include the line number when printing the results. For example:
g! lb | grep -n Apache
4: 1|Active | 1|Apache Felix Bundle Repository (1.6.2)
5: 2|Active | 1|Apache Felix Gogo Command (0.6.0)
6: 3|Active | 1|Apache Felix Gogo Runtime (0.6.0)
7: 4|Active | 1|Apache Felix Gogo Shell (0.6.0)
true
The -q
option (also --quiet
or --silent)
is used to suppress the grep
command output. The command will only return true
or false
after it is finished with the input. This is especially useful when using the command for its returned value only (as a loop guard, for example).
The -v
(or --invert-match)
is used to show the lines that don't match the pattern (inverse match). For example:
g! lb | grep -v Apache
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (3.0.1)
true
The cat
command is used to concatenate files and display their contents on the console. It takes one or more filenames relative to the current shell session directory (see cd
and ls
in the previous section) and displays them.
For example, to display the run.bat
file we had created at the beginning of this chapter, use the following:
g! cat run.bat
java -jar bin/felix.jar
Separate the arguments with whitespace to display more than one file sequentially.
The tac
command, in a way, is the opposite of the cat
command. Here it takes the text from the standard input and either returns it as a string or as a list for use as input for another command or writes it to a file.
The syntax is as shown here:
g! tac -?
tac - capture stdin as String or List and optionally write to file.
Usage: tac [-al] [FILE]
-a --append append to FILE
-l --list return List<String>
-? --help show help
The following example makes a list of the input and then gets the second item in the resulting list.
g! var1 = tac -l ; $var1 get 1
a1
a2
a3
^Z
a2
Notice the use of Ctrl-Z
(shows as ^Z
on the console display) to terminate user input.
Passing a filename as an argument will create the file and write the input text to it. The -a
or --append
option can be used to append to an existing file. For example, this will create a file from the standard input and display its contents using the cat
command:
g! tac test.out ; cat test.out
this is a test, line 1
last line
^Z
this is a test, line 1
last line
The input of the tac
command can also be the piped output of another. For example, the next command will make a backup copy of our test.out
file created previously:
g! cat test.out | tac test.out.bak
this is a test, line 1 last line
g!
g! cat test.out.bak
this is a test, line 1
last line
The set
command is used to inspect session variable information, as well as turn session tracing on
or off
.
Help on the usage of the set
command can be retrieved as follows:
g! set -?
set - show session variables
Usage: set [OPTIONS] [PREFIX]
-? --help show help
-a --all show all variables, including those starting with .
-x set xtrace option
+x unset xtrace option
If PREFIX given, then only show variable(s) starting with PREFIX
The -x
option is used to turn execution traces on. For example, taking one of the samples used previously, without setting the xtrace
option:
g! var = 'this is a string'
this is a string
g! echo $var
this is a string
When setting the xtrace
option, the shell will output a trace message for each command it will execute and for each result of that command. In this case:
g! set -x
g!
g! var = 'this is a string'
+ var '=' 'this is a string'
this is a string
g! echo $var
+ echo $var
this is a string
Setting shell execution traces on
is especially useful when working on a script (see the previous source).