Tag Archives: scripting

Use bound script param in Groovy gstring

In Groovy you can pass in variables using a Binding when evaluating an expression using GroovyShell.

The code below illustrates this. Except the last use case. An attempt is made to access the bound variable in a gstring directly. Apparently it is only accessible by the actual binding.

b1 = new Binding([name:'world']);

def result = new GroovyShell(b1).evaluate("'Hello ' + name")
assert 'Hello world' == result

result = new GroovyShell(b1).evaluate("'Hello ${b1.name}'")
assert 'Hello world' == result

result = new GroovyShell(b1).evaluate("\"Hello $b1.name\"")
assert 'Hello world' == result

result = new GroovyShell(b1).evaluate("println name; 'Hello world'")
assert 'Hello world' == result

result = new GroovyShell(b1).evaluate("\"Hello $name\"")
assert 'Hello world' == result

println "after ${count++}"

Executing the above results in:

world
Caught: groovy.lang.MissingPropertyException: No such property: name for class: BindTest
groovy.lang.MissingPropertyException: No such property: name for class: BindTest
	at BindTest.run(BindTest.groovy:15)

My puzzler is puzzled. Probably one of those intricacies of scoping?

Further reading
Passing parameters into Groovy script using Binding class

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Using Powershell instead of cygwin for scripting

In this blog entry, Windows Tricks For Whacking Lingering Java Processes …, the author resorts to the cygwin linux shell to automate an admin need.  Now cygwin is awesome, bringing some of the best tools from *nix.  However, with Powershell, Microsoft finally transformed the Windows command line into a powerful and usable administration resource.

Below a line from the bash script the author used to find each process given a process name and then loop thru each process ID and terminate it.   Note how the script requires a good understanding of bash scripting and in addition multiple Linux utilities  ps, grep, and sed.   The most important line in the script is:

found=` ps -aW | grep $procText |  sed -e's/^s*([0-9][0-9]*).*/1/' `
Here is how this works.
ps -aW:   show all processes and windows too.
grep $procText:   only use lines that contain the command line string
sed -e:   run stream editor inline
s///:   substitute
^s*:  all beginning white space
([0-9][0-9]*):  find two or more digits, remember these.
.*:  any characters.
/1/:  replace with the found digits

Very complex!!!! But, to *nix users this is nothing. Its even fun to do. With a Linux command line you can rule the world. Note that in the above script line, it’s just text processing.

Powershell has a different approach. In Powershell one works with objects (in the OOP sense). Thus, instead of transforming everything to text, one manipulates properties of objects. One pipes object instead of text.

To do:
Show how to do this in Powershell. Unfortunately, wrote the above a long time ago and lost what little Powershell mojo I was developing. Any help?

Update
3/18/12: The powershell approach will probably be expanded from something like this, which just lists the processes by name and process ID:

$strComputer = "."

$colItems = get-wmiobject -class "Win32_Process" -namespace "rootCIMV2" `
-computername $strComputer

foreach ($objItem in $colItems) {
      write-host "Name: " $objItem.Name
      write-host "Process ID: " $objItem.ProcessId
}

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.