10.3. Passing Command and Script Arguments
rhq-cli.sh, commands or full scripts can be passed simultaneously. This is a non-interactive way to connect to the CLI, since the CLI runs the specified command or script and then exits, rather than staying connected in interactive mode.
Example 10.9. Passing Variables to the Server
-c. In this example, the server searches for and prints all supported resource types for the server and prints the results to resource_types.txt
rhq-cli.sh -u rhqadmin -p rhqadmin -c "pretty.print(ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()))" > resource_types.txt
ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()) class invokes the findResourceTypesByCriteria operation on ResourceTypeManager. A new ResourceTypeCriteria object is passed as the argument.
pretty is an implicit object made available to commands and scripts by the CLI. This is useful for outputting objects in a readable, tabular format which is designed for domain objects.
Example 10.10. Running a Script
my_script.js. The CLI terminates immediately after the script has finished executing.
cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -f /export/myScripts/my_script.js
Example 10.11. Handling Script Arguments
if (args.length < 2) {
throw "Not enough arguments!";
}
for (i in args) {
println('args[' + i + '] = ' + args[i]);
}cliRoot/rhq-remoting-cli-4.9.0.JON320GA/samples/modules directory.
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -s jon-server.example.com -u rhqadmin -p rhqadmin -t 7080 echo_args.js --args-style=named x=1 y=2
echo_args.js, for example, is written to accept the two named option with the script invocation, x and y.
for (i in args) {
println('args[' + i + '] = ' + args[i]);
}
println('named args...');
println('x = ' + x);
println('y = ' + y);args[0] = 1
args[1] = 2
named args...
x = 1
y = 2- Explicitly specify that you are using named arguments with the --args-style option.
- The values of the named arguments are still accessible through the implicit args array.
- The named arguments, such as x and y, are bound into the script context as variables.
Example 10.12. Executing a Single Statement
-c option with rhq-cli.sh.
localhost:7080> var x = 1
Example 10.13. Executing a Multi-Line Statement
localhost:7080(rhqadmin)> for (i = 1; i < 3; ++i) { \
localhost:7080(rhqadmin)> println(i); \
localhost:7080(rhqadmin)> }
1
2
localhost:7080(rhqadmin)>Example 10.14. Executing a Script
-f option with the rhq-cli.sh|bat script.
-f option must give the absolute location of the script, even if it is in the same directory as the rhq-cli.sh script. The CLI will not find a script with only a relative path.
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 -f /absolute/path/to/myscript.js
Example 10.15. Executing a Script with Arguments
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 -f /absolute/path/to/myscript.js 1 2 3
Example 10.16. Executing a Script with Named Arguments
[jsmith@server ~]$ cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -t 7080 --args-style=named -f /absolute/path/to/myscript.js x=1 y=2 y=3

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.