SlideShare a Scribd company logo
1 of 34
http://www.lihaoyi.com/Ammonite/
Rock-solid shell scripting
with Ammonite
Maxim Novak
@maximnovakmaximn@wix.com https://github.com/maximn
01
List files
List files (recursively)
List all *.scala files
List the 3 largest *.scala files
Challenge… let’s ask Google
List the 3 largest *.scala files
Can’t really understand it
Seems to work…
http://toonclips.com/design/5803
http://www.clipartpanda.com/categories/man-thinking-drawing
(Lack of) Error handling
http://www.clipartpanda.com/categories/man-thinking-drawing
(Lack of) Error handling
Success
http://www.clipartpanda.com/categories/man-thinking-drawing
(Lack of) Error handling
Failed
Bash scripts are hard to understand,
hard to maintain, and very fragile
Bash scripts are hard to understand,
hard to maintain, and very fragile
We need something better
Why not ?
02
List files
import java.io.File
val file = new File(".")
if (file.exists && file.isDirectory) {
file.listFiles.toSeq
} else {
Nil
}
Write text to file
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
val file = Paths.get("hi.txt")
val content =
"hello".getBytes(StandardCharsets.UTF_8)
Files.write(file, content)
http://humandefect.deviantart.com/art/U
h-Oh-542255224
Verbose…
What you really want is
What you really want is
the power of Scala
with the semantics of a shell
Welcome Ammonite!
03
List files
http://toonclips.com/design/9164
Structured data,
yes!
ls! wd is an alias for ls(wd)
List files (recursively)
List all *.scala files
List 3 largest *.scala files
Change directory
Write text to file
Running an executable
It’s easy breezy.
It’s easy breezy. And it’s safe.
Type safety
Main.scala:296: type mismatch;
found : ammonite.ops.Path
required: ammonite.ops.RelPath
relative/absolute
^
Compilation Failed
Type safety
Error handling
java.nio.file.NoSuchFileException: /Users/max/noSuchPath
sun.nio.fs.UnixException.translateToIOException(UnitExce
sun.nio.fs.UnixException.rethrowAsIOException(UnitExcept
sun.nio.fs.UnixException.rethrowAsIOException(UnitExcept
sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(U
It’s easy breezy. And it’s safe.
Also, you can load any JVM library
to the shell.
Loading an external library
JVM ecosystem
at your fingertips
Power and safety
of Scala
Shell semantics
https://pixabay.com/en/shells-massage-therapy-sand-beach-792912/
http://youandworld.am/news/5790.html
Ammonite blurs the line between
“system shell” and a “programming language”
http://www.lihaoyi.com/Ammonite/
Thank You
Maxim Novak
@maximnovakmaximn@wix.com https://github.com/maximn

More Related Content

Similar to Rock-solid shell scripting with Ammonite

Mambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMSMambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMS
John Patten
 
7.Canon & Dt
7.Canon & Dt7.Canon & Dt
7.Canon & Dt
phanleson
 
Duspviz website
Duspviz websiteDuspviz website
Duspviz website
DUSPviz
 

Similar to Rock-solid shell scripting with Ammonite (20)

Regex Considered Harmful: Use Rosie Pattern Language Instead
Regex Considered Harmful: Use Rosie Pattern Language InsteadRegex Considered Harmful: Use Rosie Pattern Language Instead
Regex Considered Harmful: Use Rosie Pattern Language Instead
 
Dutch hug
Dutch hugDutch hug
Dutch hug
 
Scott Meyers — Why C++ Sails When the Vasa Sank
Scott Meyers — Why C++ Sails When the Vasa SankScott Meyers — Why C++ Sails When the Vasa Sank
Scott Meyers — Why C++ Sails When the Vasa Sank
 
Joys & frustrations of putting 34,000 lines of Haskell into production (at Va...
Joys & frustrations of putting 34,000 lines of Haskell into production (at Va...Joys & frustrations of putting 34,000 lines of Haskell into production (at Va...
Joys & frustrations of putting 34,000 lines of Haskell into production (at Va...
 
Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracket
 
Firefox ExtDev Introduction
Firefox ExtDev IntroductionFirefox ExtDev Introduction
Firefox ExtDev Introduction
 
The War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll CachingThe War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll Caching
 
Mambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMSMambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMS
 
Red Dirt JS
Red Dirt JSRed Dirt JS
Red Dirt JS
 
7.Canon & Dt
7.Canon & Dt7.Canon & Dt
7.Canon & Dt
 
Apache Sling - The whys and the hows
Apache Sling - The whys and the howsApache Sling - The whys and the hows
Apache Sling - The whys and the hows
 
"15 Technique to Exploit File Upload Pages", Ebrahim Hegazy
"15 Technique to Exploit File Upload Pages", Ebrahim Hegazy"15 Technique to Exploit File Upload Pages", Ebrahim Hegazy
"15 Technique to Exploit File Upload Pages", Ebrahim Hegazy
 
Strayer cis-273-week-3-lab-assignment-3-storyboarding-new
Strayer cis-273-week-3-lab-assignment-3-storyboarding-newStrayer cis-273-week-3-lab-assignment-3-storyboarding-new
Strayer cis-273-week-3-lab-assignment-3-storyboarding-new
 
Strayer cis-273-week-3-lab-assignment-3-storyboarding-new
Strayer cis-273-week-3-lab-assignment-3-storyboarding-newStrayer cis-273-week-3-lab-assignment-3-storyboarding-new
Strayer cis-273-week-3-lab-assignment-3-storyboarding-new
 
CIS 273 Education Specialist / snaptutorial.com
CIS 273  Education Specialist / snaptutorial.comCIS 273  Education Specialist / snaptutorial.com
CIS 273 Education Specialist / snaptutorial.com
 
HyperKitty, or how to get the best from mailing lists and forums
HyperKitty, or how to get the best from mailing lists and forumsHyperKitty, or how to get the best from mailing lists and forums
HyperKitty, or how to get the best from mailing lists and forums
 
DrupalEasy: Intro to Theme Development
DrupalEasy: Intro to Theme DevelopmentDrupalEasy: Intro to Theme Development
DrupalEasy: Intro to Theme Development
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
 
Rails 101
Rails 101Rails 101
Rails 101
 
Duspviz website
Duspviz websiteDuspviz website
Duspviz website
 

Recently uploaded

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 

Recently uploaded (20)

Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmux
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Abortion Clinic In Stanger ](+27832195400*)[ 🏥 Safe Abortion Pills In Stanger...
Abortion Clinic In Stanger ](+27832195400*)[ 🏥 Safe Abortion Pills In Stanger...Abortion Clinic In Stanger ](+27832195400*)[ 🏥 Safe Abortion Pills In Stanger...
Abortion Clinic In Stanger ](+27832195400*)[ 🏥 Safe Abortion Pills In Stanger...
 

Rock-solid shell scripting with Ammonite

Editor's Notes

  1. Hello everybody, my name is Max. In the next 10 minutes I will introduce you with a new powerful tool for writing rock-solid shell scripts.
  2. To understand why we need it, let’s start with the traditional way - bash. I assume that most of you are familiar with bash. Let’s see what's wrong with it.
  3. List files is easy
  4. Now let’s do it recursively
  5. We still can read and understand it …
  6. A bit more complicated ...Who can do it without Google? You might be familiar of this process … let’s ask google. And you need to google for everything, even if you had to do the same thing few weeks ago. Who even remember how to do simple stuff like for loop or an if statement? So usually you pick the one with most votes on StackOverflow
  7. You google for it, and you find many results. Then you look at it and try to understand it, look at it again …. and still don’t understand it. Probably very few people in this room can understand it. So you run it! Seems to work! -> You happy! That’s the amount of confidence we usually have in many our bash scripts In addition to that amount of confidence, the result is a string, so to use it we need to parse it and extract what we need. -- maybe replace ‘can’t understand’ with - WAT !??!
  8. Now let’s have a look at another example. What will happen? It depends! If the function returned the path to delete, it will delete everything under this path. If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
  9. Now let’s have a look at another example. What will happen? It depends! If the function returned the path to delete, it will delete everything under this path. If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
  10. Now let’s have a look at another example. What will happen? It depends! If the function returned the path to delete, it will delete everything under this path. If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
  11. We’re all Scala developers! Why don’t we use Scala? Let’s try
  12. So a simple bash `ls` command unfolds to more than 5 lines of Scala code. We get where this goes, I’m not going to go into the recursive examples here. Let’s try something else
  13. Write text to a file. What do you think? Again, Scala is much more verbose. So probably Scala isn’t better. Don’t we deserve something better? //It’s very verbose, and I don’t even want to go into all problems scala REPL have (like CTRL+C kicks us out of REPL)
  14. Welcome Ammonite! It’s a combination of a better Scala REPL and a shell like operations library. Let’s try to do the same things with Ammonite.
  15. Very easy, almost like bash. To list files we use `ls!`, the bang means executing a shell command. The cool part here is that the result is a Scala class! //This is another syntax-saving extension, that makes it easy to call functions without having to constantly be opening and closing brackets. It does nothing else.
  16. Ammonite is doing pretty print and fold long results.
  17. To list all Scala files we list the working directory and then just run `filter` on the extension. Because it’s statically typed we even can use code completion to get the .ext BTW, you’re getting some kind of code completion and highlighting
  18. Then to get the to 3 largest files is very simple. I believe that everyone in this room can understand this command. Let’s have a look at something else
  19. Let’s say we want to work with git.
  20. Another big advantage of Scala is type safety. We define these values that represents relative and absolute paths. Now let’s try to combine them, it’s working as expected.
  21. But what will happen when we will try to add an absolute path to a relative path? Boom! Compilation error!
  22. In case of a runtime error - an exception is thrown - I oppose to bash no silent failure so the execution of the script will stop.
  23. Cross platform! we simply load jsoup, then load wix homepage and using selector to get the header of the page - “It all starts with a stunning website. Create yours. It’s easy and free.
  24. Ammonite blurs the line between “system shell” to a “programming language” It gives you the familiarity and ease of use of a bash like shell combines it with the power and safety of Sacla And on top of it you have all the JVM ecosystem at your fingertips This talk gave you a little of taste of what Ammonite is. I invite you all to go to the website for more details and encourage you to try it