Skip to Content.
Sympa Menu

grouper-dev - RE: [grouper-dev] gsh script with arguments

Subject: Grouper Developers Forum

List archive

RE: [grouper-dev] gsh script with arguments


Chronological Thread 
  • From: "Black, Carey M." <>
  • To: "Redman, Chad" <>
  • Cc: "" <>
  • Subject: RE: [grouper-dev] gsh script with arguments
  • Date: Fri, 29 Mar 2019 17:32:17 +0000

Chad,

 

IMHO, No.

                But yes, kind of.

 

 

I have adapted to this general approach.

                Bash script to validate inputs, throws erros if inputs are “bad/incomplete” then “invokes GSH” as needed.

 

                I am not saying this is “ideal”, but it does work. And I do wish there were a “more standard way” to do what I am doing. J

                AKA: I think the bash script should be able to be eliminated if the GSH script would take a known “args” value and “execute them” before calling a script arg value.

                                Which is all that I am really doing.

 

 

Specifically I am using this part of the gsh CLI.

./gsh -help

args: -runarg <command> Run command (use \\n to separate commands)

 

 

So my bash script takes in inputs, validates ( in a normal “bash” way) and stacks up a “runargs” (commands as a single string) for GSH to “execute”;

 

A few quick snips of stuff to help get you going…

 

BASH snips:

#!/bin/bash

# A POSIX variable

OPTIND=1         # Reset in case getopts has been used previously in the shell.

 

verbose=0

optspec=":-:"

# Initialize our own variables:

output_file=""

verbose=0

 

while getopts "$optspec" optchar; do

    case "${optchar}" in

       -)

        case "${OPTARG}" in

                server_env)

                        val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))

                        server_env=$val

                        ;;

                server_env=*)

                        val=${OPTARG#*=}

                        server_env=$val

                        ;;

    h|\?)

        show_help

        exit 0

        ;;

    *)

        echo "Unknown option -${optchar}" >&2

        exit -10

        ;;

    esac

done

 

shift $((OPTIND-1))

 

[ "$1" = "--" ] && shift

 

 

rargs=""; # stack up the args into a string

if [ 'x' != "x$server_env" ]; then

        rargs+='server_env="'${server_env}'"\n';

fi

 

// and now invoke GSH with the rargs that were stacked up by the script…

 

cd /…./grouper.apiBinary-2.3.0/bin;

./gsh -runarg "$rargs:load some_gsh_script_to_run_with_the_params_initilized.gsh\\n";

 

So what GSH gets called with a command line like this:

 

./gsh -runarg "server_env=’foo’\nother_value=’bar’\n[other input values here]:load some_gsh_script_to_run_with_the_params_initilized.gsh\\n";

 

 

In truth I also have some code in the GSH script that validates that it has values for the required inputs too. ( avoids NPE’s and logical errors too. )

GSH script snip

// parse special config var and make them "default" values ( could be null )

try{

        server_env=server_env

}catch (Exception e){

        server_env = null

}

….

// set this before calling the script!

if ((server_env == null)||(!serverList.contains(server_env))){

        System.out.println(String.format("set server_env to a valid value ["+serverList.join(",")+"] before you run this script! Invalid value supplied '%s'",server_env));

        throw new RuntimeErrorException(new Error(String.format("set server_env to a valid value ["+serverList.join(",")+"] before you run this script! Invalid value supplied '%s'",server_env)));

}

NOTE: I do the SYSOUT print because this is a cron job and I get email if something goes wrong. J Log or do whatever you prefer for error handling.

 

 

 

 

I think you could also choose to use this:

args: -main <class> [args...]

   class,               Full class name (must have main method)

   args,                args as required by main method of class

 

However then you need to have a compiled class with args the match the CLI values too. Which seems more tightly coupled than I want.

 

HTH.

--

Carey Matthew

 

From: <> On Behalf Of Redman, Chad
Sent: Friday, March 29, 2019 9:58 AM
To:
Subject: [grouper-dev] gsh script with arguments

 

Is it possible to run groovy gsh with a startup script that has arguments? It may just need to call the Groovy Shell constructor and bind the args as a variable.

 

I wanted to ask before filing a jira, in case there a history of beanshell running a different way. The groovy profile suggests that multiple command line parameters are to load multiple scripts in order. But it's only ever passed the first parameter, so it may not work as intended.

 

Even just a regular :load and passing parameters is challenging. This is the only way I know to get it to work:

 

def binding = new Binding()

binding.args = ["filename-placeholder", "a", "b", "c"]

def ret = new GroovyShell(binding).evaluate(new File("script.gsh"))

 

 

-Chad

 




Archive powered by MHonArc 2.6.19.

Top of Page