SlideShare a Scribd company logo
1 of 27
Download to read offline
The Bash: Defensive Scripting




      The Bash: Defensive Scripting




                        1
Montag, 29. August 11
The Bash: Defensive Scripting




                        2
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix




                        2
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         Interfaces




                        2
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         Interfaces
         Re-use



                        2
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         Interfaces
         Re-use
         Defensive Programming


                        2
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         Interfaces
         Re-use
         Defensive Programming
         Readings

                        2
Montag, 29. August 11
The Bash: Defensive Scripting




                        3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix




                        3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         • Be forgiving in what you accept, but
                   strict in what you emit




                                    3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         • Be forgiving in what you accept, but
           strict in what you emit
         • If you must fail, do so early and
           noisily


                            3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         • Be forgiving in what you accept, but
           strict in what you emit
         • If you must fail, do so early and
           noisily
         • One tool for each task


                            3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         • Be forgiving in what you accept, but
           strict in what you emit
         • If you must fail, do so early and
           noisily
         • One tool for each task
         • Human-readable data and protocols
                            3
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix
         • Be forgiving in what you accept, but
                   strict in what you emit
         •         If you must fail, do so early and
                   noisily
         •         One tool for each task
         •         Human-readable data and protocols
         •         Do the simplest thing that will do
                                    3
Montag, 29. August 11
The Bash: Defensive Scripting




                        4
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix: The Unix Toolbox




                        4
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix: The Unix Toolbox
         Get familiar with cat, cut, tee, uniq,
         sort, head, tail, but also grep, sed,
         diff, find and possibly even awk. They
         will save you a whole lot of work.


                            4
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix: The Unix Toolbox
         Get familiar with cat, cut, tee, uniq,
         sort, head, tail, but also grep, sed,
         diff, find and possibly even awk. They
         will save you a whole lot of work.
         For more inspiration: ls /usr/bin

                            4
Montag, 29. August 11
The Bash: Defensive Scripting




         Unix: The Unix Toolbox
         Get familiar with cat, cut, tee, uniq,
         sort, head, tail, but also grep, sed,
         diff, find and possibly even awk. They
         will save you a whole lot of work.
         For more inspiration: ls /usr/bin
         Extend your Unix Toolbox
                            4
Montag, 29. August 11
The Bash: Defensive Scripting




                        5
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces




                        5
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces
         • stdin, stdout, stderr, exit code




                            5
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces
         • stdin, stdout, stderr, exit code
         • Favour plain text




                            5
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces
         • stdin, stdout, stderr, exit code
         • Favour plain text
         • Treat comments as comments




                            5
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces
         •         stdin, stdout, stderr, exit code
         •         Favour plain text
         •         Treat comments as comments
         •         Filter, Source, Sink, Cantrip,
                   Compiler

                                    5
Montag, 29. August 11
The Bash: Defensive Scripting




                        6
Montag, 29. August 11
The Bash: Defensive Scripting




         Interfaces: Filter




                        6
Montag, 29. August 11

More Related Content

Recently uploaded

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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...Drew Madelung
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

The bash – defensive scripting

  • 1. The Bash: Defensive Scripting The Bash: Defensive Scripting 1 Montag, 29. August 11
  • 2. The Bash: Defensive Scripting 2 Montag, 29. August 11
  • 3. The Bash: Defensive Scripting Unix 2 Montag, 29. August 11
  • 4. The Bash: Defensive Scripting Unix Interfaces 2 Montag, 29. August 11
  • 5. The Bash: Defensive Scripting Unix Interfaces Re-use 2 Montag, 29. August 11
  • 6. The Bash: Defensive Scripting Unix Interfaces Re-use Defensive Programming 2 Montag, 29. August 11
  • 7. The Bash: Defensive Scripting Unix Interfaces Re-use Defensive Programming Readings 2 Montag, 29. August 11
  • 8. The Bash: Defensive Scripting 3 Montag, 29. August 11
  • 9. The Bash: Defensive Scripting Unix 3 Montag, 29. August 11
  • 10. The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit 3 Montag, 29. August 11
  • 11. The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily 3 Montag, 29. August 11
  • 12. The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task 3 Montag, 29. August 11
  • 13. The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task • Human-readable data and protocols 3 Montag, 29. August 11
  • 14. The Bash: Defensive Scripting Unix • Be forgiving in what you accept, but strict in what you emit • If you must fail, do so early and noisily • One tool for each task • Human-readable data and protocols • Do the simplest thing that will do 3 Montag, 29. August 11
  • 15. The Bash: Defensive Scripting 4 Montag, 29. August 11
  • 16. The Bash: Defensive Scripting Unix: The Unix Toolbox 4 Montag, 29. August 11
  • 17. The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. 4 Montag, 29. August 11
  • 18. The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. For more inspiration: ls /usr/bin 4 Montag, 29. August 11
  • 19. The Bash: Defensive Scripting Unix: The Unix Toolbox Get familiar with cat, cut, tee, uniq, sort, head, tail, but also grep, sed, diff, find and possibly even awk. They will save you a whole lot of work. For more inspiration: ls /usr/bin Extend your Unix Toolbox 4 Montag, 29. August 11
  • 20. The Bash: Defensive Scripting 5 Montag, 29. August 11
  • 21. The Bash: Defensive Scripting Interfaces 5 Montag, 29. August 11
  • 22. The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code 5 Montag, 29. August 11
  • 23. The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text 5 Montag, 29. August 11
  • 24. The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text • Treat comments as comments 5 Montag, 29. August 11
  • 25. The Bash: Defensive Scripting Interfaces • stdin, stdout, stderr, exit code • Favour plain text • Treat comments as comments • Filter, Source, Sink, Cantrip, Compiler 5 Montag, 29. August 11
  • 26. The Bash: Defensive Scripting 6 Montag, 29. August 11
  • 27. The Bash: Defensive Scripting Interfaces: Filter 6 Montag, 29. August 11
  • 28. The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout 6 Montag, 29. August 11
  • 29. The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout Perfect for pipes 6 Montag, 29. August 11
  • 30. The Bash: Defensive Scripting Interfaces: Filter Read from stdin, write to stdout Perfect for pipes Example: cut 6 Montag, 29. August 11
  • 31. The Bash: Defensive Scripting 7 Montag, 29. August 11
  • 32. The Bash: Defensive Scripting Interfaces: Source 7 Montag, 29. August 11
  • 33. The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout 7 Montag, 29. August 11
  • 34. The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout Interface to outer world, beginning of a pipe sequence 7 Montag, 29. August 11
  • 35. The Bash: Defensive Scripting Interfaces: Source Read from file, write to stdout Interface to outer world, beginning of a pipe sequence Example: cat 7 Montag, 29. August 11
  • 36. The Bash: Defensive Scripting 8 Montag, 29. August 11
  • 37. The Bash: Defensive Scripting Interfaces: Sink 8 Montag, 29. August 11
  • 38. The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file 8 Montag, 29. August 11
  • 39. The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file Interface to outer world, end of a pipe sequence 8 Montag, 29. August 11
  • 40. The Bash: Defensive Scripting Interfaces: Sink Read from stdin, write to file Interface to outer world, end of a pipe sequence Example: > 8 Montag, 29. August 11
  • 41. The Bash: Defensive Scripting 9 Montag, 29. August 11
  • 42. The Bash: Defensive Scripting Interfaces: Cantrip 9 Montag, 29. August 11
  • 43. The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) 9 Montag, 29. August 11
  • 44. The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) This is super scriptable 9 Montag, 29. August 11
  • 45. The Bash: Defensive Scripting Interfaces: Cantrip Do sth. and return nothing but a status code (or errors) This is super scriptable Example: rm 9 Montag, 29. August 11
  • 46. The Bash: Defensive Scripting 10 Montag, 29. August 11
  • 47. The Bash: Defensive Scripting Interfaces: Compiler 10 Montag, 29. August 11
  • 48. The Bash: Defensive Scripting Interfaces: Compiler Read from file, write to another file 10 Montag, 29. August 11
  • 49. The Bash: Defensive Scripting Interfaces: Compiler Read from file, write to another file Example: tar 10 Montag, 29. August 11
  • 50. The Bash: Defensive Scripting 11 Montag, 29. August 11
  • 51. The Bash: Defensive Scripting Re-use 11 Montag, 29. August 11
  • 52. The Bash: Defensive Scripting Re-use • Functions 11 Montag, 29. August 11
  • 53. The Bash: Defensive Scripting Re-use • Functions • Wrappers 11 Montag, 29. August 11
  • 54. The Bash: Defensive Scripting Re-use • Functions • Wrappers • $() (aka ``) 11 Montag, 29. August 11
  • 55. The Bash: Defensive Scripting Re-use • Functions • Wrappers • $() (aka ``) • source (aka ‘.’) 11 Montag, 29. August 11
  • 56. The Bash: Defensive Scripting 12 Montag, 29. August 11
  • 57. The Bash: Defensive Scripting Re-use: Functions 12 Montag, 29. August 11
  • 58. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own 12 Montag, 29. August 11
  • 59. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { 12 Montag, 29. August 11
  • 60. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 12 Montag, 29. August 11
  • 61. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } 12 Montag, 29. August 11
  • 62. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } log “Cannot connect” 12 Montag, 29. August 11
  • 63. The Bash: Defensive Scripting Re-use: Functions Will behave like a script on its own function log { echo “$(date) ERR $@” >&2 } log “Cannot connect” # Do 4 Aug 2011 10:38:58 CEST ERR Cannot connect 12 Montag, 29. August 11
  • 64. The Bash: Defensive Scripting 13 Montag, 29. August 11
  • 65. The Bash: Defensive Scripting Re-use: Wrappers 13 Montag, 29. August 11
  • 66. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash 13 Montag, 29. August 11
  • 67. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if 13 Montag, 29. August 11
  • 68. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it 13 Montag, 29. August 11
  • 69. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 13 Montag, 29. August 11
  • 70. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” 13 Montag, 29. August 11
  • 71. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” test -d $newdir || mkdir $newdir 13 Montag, 29. August 11
  • 72. The Bash: Defensive Scripting Re-use: Wrappers #!/bin/bash # file mcd -- make a dir if # necessary and cd to it test -n “$1” || exit 1 newdir=”$1” test -d $newdir || mkdir $newdir cd $newdir 13 Montag, 29. August 11
  • 73. The Bash: Defensive Scripting 14 Montag, 29. August 11
  • 74. The Bash: Defensive Scripting Re-use: $() 14 Montag, 29. August 11
  • 75. The Bash: Defensive Scripting Re-use: $() More readable than `backticks` 14 Montag, 29. August 11
  • 76. The Bash: Defensive Scripting Re-use: $() More readable than `backticks` myfiles=$(ls $HOME) 14 Montag, 29. August 11
  • 77. The Bash: Defensive Scripting 15 Montag, 29. August 11
  • 78. The Bash: Defensive Scripting Re-use: source 15 Montag, 29. August 11
  • 79. The Bash: Defensive Scripting Re-use: source More readable than “.” 15 Montag, 29. August 11
  • 80. The Bash: Defensive Scripting Re-use: source More readable than “.” source $HOME/lib/basics.sh 15 Montag, 29. August 11
  • 81. The Bash: Defensive Scripting Re-use: source More readable than “.” source $HOME/lib/basics.sh source /etc/conf/script.cnf 15 Montag, 29. August 11
  • 82. The Bash: Defensive Scripting 16 Montag, 29. August 11
  • 83. The Bash: Defensive Scripting Defensive Programming 16 Montag, 29. August 11
  • 84. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) 16 Montag, 29. August 11
  • 85. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find 16 Montag, 29. August 11
  • 86. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) 16 Montag, 29. August 11
  • 87. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap 16 Montag, 29. August 11
  • 88. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr 16 Montag, 29. August 11
  • 89. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr • --help, --version, usage() 16 Montag, 29. August 11
  • 90. The Bash: Defensive Scripting Defensive Programming • test, test, test (for $?) • find • && and || (AND and OR) • trap • log, stderr • --help, --version, usage() • Use full path names 16 Montag, 29. August 11
  • 91. The Bash: Defensive Scripting 17 Montag, 29. August 11
  • 92. The Bash: Defensive Scripting Defensive Programming: test 17 Montag, 29. August 11
  • 93. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” 17 Montag, 29. August 11
  • 94. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” 17 Montag, 29. August 11
  • 95. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” 17 Montag, 29. August 11
  • 96. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” 17 Montag, 29. August 11
  • 97. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” test -n “$variable” 17 Montag, 29. August 11
  • 98. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” test -d “$destdir” test -x “$script” test -w “$destfile” test -n “$variable” test -z “$nothing” 17 Montag, 29. August 11
  • 99. The Bash: Defensive Scripting 18 Montag, 29. August 11
  • 100. The Bash: Defensive Scripting Defensive Programming: test 18 Montag, 29. August 11
  • 101. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” 18 Montag, 29. August 11
  • 102. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then 18 Montag, 29. August 11
  • 103. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” 18 Montag, 29. August 11
  • 104. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else 18 Montag, 29. August 11
  • 105. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else echo “cannot read $sourcefile” 18 Montag, 29. August 11
  • 106. The Bash: Defensive Scripting Defensive Programming: test test -r “$sourcefile” if [[ $? = 0 ]]; then echo “yes, it’s readable” else echo “cannot read $sourcefile” fi 18 Montag, 29. August 11
  • 107. The Bash: Defensive Scripting 19 Montag, 29. August 11
  • 108. The Bash: Defensive Scripting Defensive Programming: && and || 19 Montag, 29. August 11
  • 109. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || exit 1 19 Montag, 29. August 11
  • 110. The Bash: Defensive Scripting 20 Montag, 29. August 11
  • 111. The Bash: Defensive Scripting Defensive Programming: && and || 20 Montag, 29. August 11
  • 112. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { 20 Montag, 29. August 11
  • 113. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 20 Montag, 29. August 11
  • 114. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 exit 1 20 Montag, 29. August 11
  • 115. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” || { echo “Cannot read source file” >&2 exit 1 } 20 Montag, 29. August 11
  • 116. The Bash: Defensive Scripting 21 Montag, 29. August 11
  • 117. The Bash: Defensive Scripting Defensive Programming: && and || 21 Montag, 29. August 11
  • 118. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { 21 Montag, 29. August 11
  • 119. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” 21 Montag, 29. August 11
  • 120. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { 21 Montag, 29. August 11
  • 121. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 21 Montag, 29. August 11
  • 122. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 exit 1 21 Montag, 29. August 11
  • 123. The Bash: Defensive Scripting Defensive Programming: && and || test -r “$sourcefile” && { cat -n “$sourcefile” } || { echo “Cannot read source file” >&2 exit 1 } 21 Montag, 29. August 11
  • 124. The Bash: Defensive Scripting 22 Montag, 29. August 11
  • 125. The Bash: Defensive Scripting Defensive Programming: find 22 Montag, 29. August 11
  • 126. The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. 22 Montag, 29. August 11
  • 127. The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. find Syntax is a bit peculiar. 22 Montag, 29. August 11
  • 128. The Bash: Defensive Scripting Defensive Programming: find Don’t rely on a file being in some exact place. If it needs to be, tell the user. find Syntax is a bit peculiar. find $install_dir -name “*README*” -type f -exec mv {} $install_dir/docs/ ; 22 Montag, 29. August 11
  • 129. The Bash: Defensive Scripting 23 Montag, 29. August 11
  • 130. The Bash: Defensive Scripting Defensive Programming: trap 23 Montag, 29. August 11
  • 131. The Bash: Defensive Scripting Defensive Programming: trap trap “/bin/rm -f $tmpf; exit” EXIT 23 Montag, 29. August 11
  • 132. The Bash: Defensive Scripting Defensive Programming: trap trap “/bin/rm -f $tmpf; exit” EXIT trap “source $config” SIGHUP 23 Montag, 29. August 11
  • 133. The Bash: Defensive Scripting function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24 Montag, 29. August 11
  • 134. The Bash: Defensive Scripting Defensive Programming: Logs function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24 Montag, 29. August 11
  • 135. The Bash: Defensive Scripting Defensive Programming: Logs Write status to stderr. stderr is not for *errors* only, but for all kind of metainformation function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24 Montag, 29. August 11
  • 136. The Bash: Defensive Scripting Defensive Programming: Logs Write status to stderr. stderr is not for *errors* only, but for all kind of metainformation Use syslog/logger or your own logging function function log { msg=“$(date) ERR $@” test -w $LOGF && echo $msg > $LOGF echo $msg >&2 } 24 Montag, 29. August 11
  • 137. The Bash: Defensive Scripting 25 Montag, 29. August 11
  • 138. The Bash: Defensive Scripting Defensive Programming: 25 Montag, 29. August 11
  • 139. The Bash: Defensive Scripting Defensive Programming: --help, --version 25 Montag, 29. August 11
  • 140. The Bash: Defensive Scripting Defensive Programming: --help, --version • Simplify the use of your program by providing --help and --version 25 Montag, 29. August 11
  • 141. The Bash: Defensive Scripting Defensive Programming: --help, --version • Simplify the use of your program by providing --help and --version • On error print a usage message 25 Montag, 29. August 11
  • 142. The Bash: Defensive Scripting 26 Montag, 29. August 11
  • 143. The Bash: Defensive Scripting Defensive Programming: 26 Montag, 29. August 11
  • 144. The Bash: Defensive Scripting Defensive Programming: use full path names 26 Montag, 29. August 11
  • 145. The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install 26 Montag, 29. August 11
  • 146. The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt 26 Montag, 29. August 11
  • 147. The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt dstf=$base/destination.txt 26 Montag, 29. August 11
  • 148. The Bash: Defensive Scripting Defensive Programming: use full path names base=$HOME/install srcf=$base/source.txt dstf=$base/destination.txt test -r $srcf && cp -i $srcf $dstf 26 Montag, 29. August 11
  • 149. The Bash: Defensive Scripting 27 Montag, 29. August 11
  • 150. The Bash: Defensive Scripting Readings 27 Montag, 29. August 11
  • 151. The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ 27 Montag, 29. August 11
  • 152. The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) 27 Montag, 29. August 11
  • 153. The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) • Wicked Cool Shell Scripts (No Starch Press) 27 Montag, 29. August 11
  • 154. The Bash: Defensive Scripting Readings • Advanced Bash-Scripting Guide http:// tldp.org/LDP/abs/html/ • The Art of Unix Programming (by Eric S. Raymond; http://www.faqs.org/docs/artu/) • Wicked Cool Shell Scripts (No Starch Press) • Patrick Jezek: Bash Techtalk (08/2010) http://slides.liip.ch/shell_scripting/ index.html 27 Montag, 29. August 11