SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
Making shell scripts Groovy
about me
● programmer + DevOps enthusiast
– Java, Groovy, learning Clojure
– Linux, Ansible, Liquibase, Maven
● co-organizing software craftsmanship groups:
– Ruhrgebiet + Düsseldorf (Germany)
– coderetreats, coding dojos
georgberky
georg.berky@valtech-mobility.com
my journey to Groovy
● problem in production
● when contacting a remote system via HTTP
● not enough logging
● but access to the machine
● everything works in curl
my journey to Groovy
● problem has to be inside the JVM
● google: “run Java as a script”
● Groovy!
● some tinkering, some dependencies
● ⇒ missing certificate in Java’s keystore :)
today’s agenda
● shebang + groovyserver
● pipes
● dependency management
● parameter parsing
● external commands
● bonus: XML/JSON
● bonus: FIFOs
● bonus: logging
setup
● install sdkman from sdkman.io
● install groovy using sdkman
– groovy --version should work
● use your favorite editor
● Groovy API
http://docs.groovy-lang.org/latest/html/gapi/
● JDK extensions:
http://groovy-lang.org/gdk.html
getting started
● start with shebang + make executable
● write “hello world” in Java
● use file extension: .groovy
● run: ./HelloWorld.groovy
● leave out as many syntax elements as you can
pipes
● connect two processes
● live outside your root file system
● lifetime: as long as the process creating it
● writing end + reading end
● ends support UNIX file interface
protocol for pipes
● one line is one message
● line buffered
● last producer disconnects: EOF
● preserve order
exercise: using pipes
● write a program that mimics grep
– e.g. grep groovy < input.txt
● read lines from System.in
– hint: use eachLine
● print line if it contains the argument
– hint: use args[0]
testing
● batteries included: JUnit 3 – 5
● power assert
● test doubles: closure + map coercion
● MockFor, StubFor
only in dynamic context
scripts: compiled
exercise: testing
● FizzBuzz Kata
● print all numbers from 1 to 100
● if n divisible by 3, print fizz
● if n divisible by 5, print buzz
● if n divisible by 3 and 5, print fizzbuzz
dependency management
● @Grab(‘org.apache.commons:commons-lang3:3.9’)
● @Grab(group='org.apache.commons',
module='commons-lang3',
version='3.9')
● at runtime: Grape.grab(
group:’org.apache.commons’,
module:’commons-lang3’,
version:’3.9’)
dependency management
● Groovy Grape
● built on top of Apache Ivy
● grabs (transitive) dependencies
– default: from Maven Central
● scripts: replaced with Grape.grab()
– in static initializer
command line options
● Groovy Commons CliBuilder
● @Grab(group='org.codehaus.groovy',
module='groovy-cli-commons',
version='2.5.7')
● import groovy.cli.commons.CliBuilder
● supports short and long options
● now supports typed arguments
exercise: parsing arguments
● use:
@Grab(group='org.codehaus.groovy',
module='groovy-cli-commons',
version='2.5.7')
● extend your grep script:
● ignore case: -i
● invert matching: -v
external commands
● method execute() on String
● e.g. “ls -1”.execute()
● creates and runs instance of Process
● method getText() on Process,
then method eachLine() on String
final assignment
● Producer: prints numbers, one per line
● Consumer: read lines
– increment the number
– print the incremented number
● communicate via pipe
● separate concerns
● test drive as much as you can
suggested design
final assignment - hints
● Isolated tests:
– Map coercion for mocks:
[methodName: { … }] as Interface
– @Grab Mockito for mocks
● integrated tests:
– getInputStream() on Process
– getOutputStream() on Process
●
got interested?
● Previous work: Sascha Klein - “Groovy on the Shell”
– https://tinyurl.com/groovy-on-the-shell
● Georg Berky - “Groovy Shell Scripting”
(Never Code Alone Blog)
– http://tinyurl.com/groovy-shell-1
– http://tinyurl.com/groovy-shell-2
– http://tinyurl.com/groovy-shell-3
Videoplasty.com [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)]
thank you!
georgberky
georg.berky@valtech-mobility.com

Más contenido relacionado

La actualidad más candente

When third parties stop being polite... and start getting real
When third parties stop being polite... and start getting realWhen third parties stop being polite... and start getting real
When third parties stop being polite... and start getting real
Charles Vazac
 
Useful Vim Plugins
Useful Vim PluginsUseful Vim Plugins
Useful Vim Plugins
anveo
 
Docker session I: Continuous integration, delivery and deployment
Docker session I: Continuous integration, delivery and deploymentDocker session I: Continuous integration, delivery and deployment
Docker session I: Continuous integration, delivery and deployment
Degendra Sivakoti
 

La actualidad más candente (20)

Introduction to webGL
Introduction to webGLIntroduction to webGL
Introduction to webGL
 
When third parties stop being polite... and start getting real
When third parties stop being polite... and start getting realWhen third parties stop being polite... and start getting real
When third parties stop being polite... and start getting real
 
Devenez le plus heureux des Front-end avec Gulp.js
Devenez le plus heureux des Front-end avec Gulp.jsDevenez le plus heureux des Front-end avec Gulp.js
Devenez le plus heureux des Front-end avec Gulp.js
 
Full stack development
Full stack developmentFull stack development
Full stack development
 
Extensible web
Extensible webExtensible web
Extensible web
 
Modern Development Tools
Modern Development ToolsModern Development Tools
Modern Development Tools
 
Useful Vim Plugins
Useful Vim PluginsUseful Vim Plugins
Useful Vim Plugins
 
Gestión de infraestructura tomcat tom ee con tfactory
Gestión de infraestructura tomcat tom ee con tfactoryGestión de infraestructura tomcat tom ee con tfactory
Gestión de infraestructura tomcat tom ee con tfactory
 
CRaSH the shell for the JVM
CRaSH the shell for the JVMCRaSH the shell for the JVM
CRaSH the shell for the JVM
 
An introduction to Node.js application development
An introduction to Node.js application developmentAn introduction to Node.js application development
An introduction to Node.js application development
 
CRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual MachineCRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual Machine
 
Extensible web #html5j
Extensible web #html5jExtensible web #html5j
Extensible web #html5j
 
Rapid Application Development with Cocoon
Rapid Application Development with CocoonRapid Application Development with Cocoon
Rapid Application Development with Cocoon
 
An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)An Introduction of Node Package Manager (NPM)
An Introduction of Node Package Manager (NPM)
 
Node.js - Javascript além do navegador, por Rômulo Santos
Node.js - Javascript além do navegador, por Rômulo SantosNode.js - Javascript além do navegador, por Rômulo Santos
Node.js - Javascript além do navegador, por Rômulo Santos
 
Improving Chromium's code health: Onion Soup and beyond (BlinkOn 11)
Improving Chromium's code health: Onion Soup and beyond (BlinkOn 11)Improving Chromium's code health: Onion Soup and beyond (BlinkOn 11)
Improving Chromium's code health: Onion Soup and beyond (BlinkOn 11)
 
Повний тестздець SPA + REST
Повний тестздець SPA + RESTПовний тестздець SPA + REST
Повний тестздець SPA + REST
 
Docker session I: Continuous integration, delivery and deployment
Docker session I: Continuous integration, delivery and deploymentDocker session I: Continuous integration, delivery and deployment
Docker session I: Continuous integration, delivery and deployment
 
find & improve some bottleneck in Debian project (DebConf14 LT)
find & improve some bottleneck in Debian project (DebConf14 LT)find & improve some bottleneck in Debian project (DebConf14 LT)
find & improve some bottleneck in Debian project (DebConf14 LT)
 
Docker. Micro services for lazy developers
Docker. Micro services for lazy developersDocker. Micro services for lazy developers
Docker. Micro services for lazy developers
 

Similar a Groovy shell scripting

Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Docker, Inc.
 
Screaming Fast Wpmu
Screaming Fast WpmuScreaming Fast Wpmu
Screaming Fast Wpmu
djcp
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Yasuharu Nakano
 

Similar a Groovy shell scripting (20)

Grails 101
Grails 101Grails 101
Grails 101
 
NovaProva, a new generation unit test framework for C programs
NovaProva, a new generation unit test framework for C programsNovaProva, a new generation unit test framework for C programs
NovaProva, a new generation unit test framework for C programs
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
 
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails Projects
 
Take Home Your Very Own Free Vagrant CFML Dev Environment
Take Home Your Very Own Free Vagrant CFML Dev Environment Take Home Your Very Own Free Vagrant CFML Dev Environment
Take Home Your Very Own Free Vagrant CFML Dev Environment
 
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
Take home your very own free Vagrant CFML Dev Environment - Presented at dev....
 
Screaming Fast Wpmu
Screaming Fast WpmuScreaming Fast Wpmu
Screaming Fast Wpmu
 
Polyglot Applications with GraalVM
Polyglot Applications with GraalVMPolyglot Applications with GraalVM
Polyglot Applications with GraalVM
 
PHP Development Tools
PHP  Development ToolsPHP  Development Tools
PHP Development Tools
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Groovy and noteworthy
Groovy and noteworthyGroovy and noteworthy
Groovy and noteworthy
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
 
How I hack on puppet modules
How I hack on puppet modulesHow I hack on puppet modules
How I hack on puppet modules
 
Towards Continuous Deployment with Django
Towards Continuous Deployment with DjangoTowards Continuous Deployment with Django
Towards Continuous Deployment with Django
 
"Lightweight Virtualization with Linux Containers and Docker". Jerome Petazzo...
"Lightweight Virtualization with Linux Containers and Docker". Jerome Petazzo..."Lightweight Virtualization with Linux Containers and Docker". Jerome Petazzo...
"Lightweight Virtualization with Linux Containers and Docker". Jerome Petazzo...
 
Unmanned Aerial Vehicles: Exploit Automation with the Metasploit Framework
Unmanned Aerial Vehicles: Exploit Automation with the Metasploit FrameworkUnmanned Aerial Vehicles: Exploit Automation with the Metasploit Framework
Unmanned Aerial Vehicles: Exploit Automation with the Metasploit Framework
 
0507 057 01 98 * Adana Klima Servisleri
0507 057 01 98 * Adana Klima Servisleri0507 057 01 98 * Adana Klima Servisleri
0507 057 01 98 * Adana Klima Servisleri
 
Shall we play a game
Shall we play a gameShall we play a game
Shall we play a game
 
Shall we play a game?
Shall we play a game?Shall we play a game?
Shall we play a game?
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Groovy shell scripting

  • 2. about me ● programmer + DevOps enthusiast – Java, Groovy, learning Clojure – Linux, Ansible, Liquibase, Maven ● co-organizing software craftsmanship groups: – Ruhrgebiet + Düsseldorf (Germany) – coderetreats, coding dojos georgberky georg.berky@valtech-mobility.com
  • 3. my journey to Groovy ● problem in production ● when contacting a remote system via HTTP ● not enough logging ● but access to the machine ● everything works in curl
  • 4. my journey to Groovy ● problem has to be inside the JVM ● google: “run Java as a script” ● Groovy! ● some tinkering, some dependencies ● ⇒ missing certificate in Java’s keystore :)
  • 5. today’s agenda ● shebang + groovyserver ● pipes ● dependency management ● parameter parsing ● external commands ● bonus: XML/JSON ● bonus: FIFOs ● bonus: logging
  • 6. setup ● install sdkman from sdkman.io ● install groovy using sdkman – groovy --version should work ● use your favorite editor ● Groovy API http://docs.groovy-lang.org/latest/html/gapi/ ● JDK extensions: http://groovy-lang.org/gdk.html
  • 7. getting started ● start with shebang + make executable ● write “hello world” in Java ● use file extension: .groovy ● run: ./HelloWorld.groovy ● leave out as many syntax elements as you can
  • 8. pipes ● connect two processes ● live outside your root file system ● lifetime: as long as the process creating it ● writing end + reading end ● ends support UNIX file interface
  • 9. protocol for pipes ● one line is one message ● line buffered ● last producer disconnects: EOF ● preserve order
  • 10. exercise: using pipes ● write a program that mimics grep – e.g. grep groovy < input.txt ● read lines from System.in – hint: use eachLine ● print line if it contains the argument – hint: use args[0]
  • 11. testing ● batteries included: JUnit 3 – 5 ● power assert ● test doubles: closure + map coercion ● MockFor, StubFor only in dynamic context scripts: compiled
  • 12. exercise: testing ● FizzBuzz Kata ● print all numbers from 1 to 100 ● if n divisible by 3, print fizz ● if n divisible by 5, print buzz ● if n divisible by 3 and 5, print fizzbuzz
  • 13. dependency management ● @Grab(‘org.apache.commons:commons-lang3:3.9’) ● @Grab(group='org.apache.commons', module='commons-lang3', version='3.9') ● at runtime: Grape.grab( group:’org.apache.commons’, module:’commons-lang3’, version:’3.9’)
  • 14. dependency management ● Groovy Grape ● built on top of Apache Ivy ● grabs (transitive) dependencies – default: from Maven Central ● scripts: replaced with Grape.grab() – in static initializer
  • 15. command line options ● Groovy Commons CliBuilder ● @Grab(group='org.codehaus.groovy', module='groovy-cli-commons', version='2.5.7') ● import groovy.cli.commons.CliBuilder ● supports short and long options ● now supports typed arguments
  • 16. exercise: parsing arguments ● use: @Grab(group='org.codehaus.groovy', module='groovy-cli-commons', version='2.5.7') ● extend your grep script: ● ignore case: -i ● invert matching: -v
  • 17. external commands ● method execute() on String ● e.g. “ls -1”.execute() ● creates and runs instance of Process ● method getText() on Process, then method eachLine() on String
  • 18. final assignment ● Producer: prints numbers, one per line ● Consumer: read lines – increment the number – print the incremented number ● communicate via pipe ● separate concerns ● test drive as much as you can
  • 20. final assignment - hints ● Isolated tests: – Map coercion for mocks: [methodName: { … }] as Interface – @Grab Mockito for mocks ● integrated tests: – getInputStream() on Process – getOutputStream() on Process ●
  • 21. got interested? ● Previous work: Sascha Klein - “Groovy on the Shell” – https://tinyurl.com/groovy-on-the-shell ● Georg Berky - “Groovy Shell Scripting” (Never Code Alone Blog) – http://tinyurl.com/groovy-shell-1 – http://tinyurl.com/groovy-shell-2 – http://tinyurl.com/groovy-shell-3 Videoplasty.com [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)]