SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Similar a Rock-solid shell scripting with Ammonite

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 InsteadAll Things Open
 
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 SankYandex
 
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...Saurabh Nanda
 
Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracketjnewmanux
 
Firefox ExtDev Introduction
Firefox ExtDev IntroductionFirefox ExtDev Introduction
Firefox ExtDev Introductiondynamis
 
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 CachingPeter Giacomo Lombardo
 
Mambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMSMambo (2006) Presentation on CMS
Mambo (2006) Presentation on CMSJohn Patten
 
7.Canon & Dt
7.Canon & Dt7.Canon & Dt
7.Canon & Dtphanleson
 
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 howsRobert Munteanu
 
"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 HegazyHackIT Ukraine
 
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-newcskvsmi44
 
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-newkrystalhero123
 
CIS 273 Education Specialist / snaptutorial.com
CIS 273  Education Specialist / snaptutorial.comCIS 273  Education Specialist / snaptutorial.com
CIS 273 Education Specialist / snaptutorial.comMcdonaldRyan138
 
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 forumsAnne Nicolas
 
DrupalEasy: Intro to Theme Development
DrupalEasy: Intro to Theme DevelopmentDrupalEasy: Intro to Theme Development
DrupalEasy: Intro to Theme Developmentultimike
 
.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...NETFest
 
Duspviz website
Duspviz websiteDuspviz website
Duspviz websiteDUSPviz
 

Similar a 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
 

Último

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 

Último (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Rock-solid shell scripting with Ammonite

Notas del editor

  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