SlideShare a Scribd company logo
Tips and Tricks
Kellyn Gorman, Azure Data Platform Architect
Microsoft
Scripting for Success on
Linux
If you require assistance
during the session, type
your inquiry into the
question pane on the right
side.
Maximize your screen with
the zoom button on the
top of the presentation
window.
Please fill in the short
evaluation following the
session. It will appear in
your web browser.
Technical Assistance
PASS’ flagship event takes place in Seattle, Washington
November 5-8, 2019
PASSsummit.com
PASS Marathon: Career Development
October 8, 2019
Upcoming
Events
10
Kellyn Gorman
Azure Data Platform
Architect, Microsoft
Azure Data Platform Architect
Kellyn has been with Microsoft for over a year
now working in the Analytics and AI team in
Higher Education, but spends a percentage of
her time migrating large Oracle environments
over to Azure bare metal.
Blogger, Author, Speaker
Kellyn writes on two of the top 50 database
blogs in the world, known for Oracle and
Microsoft technical content, has written five
books, including one on Diversity and
Inclusion. She mentors, sponsors and speaks
in both the Oracle and Microsoft communities
as part of giving back to the community.
President, Denver SQL Server User
Group
Kellyn has been the president for over two
years now, continuing to support this
incredible user group while on the road in her
RV, traveling the US.
@DBAKevlar
https://www.linkedin.com/in/kellyngorman/
Kellyn.Gorman@Microsoft.com
Kellyn Pot'Vin-Gorman
Moderated By: Carlos Lopez
Scripting for Success on Linux-
Top Tips and Tricks
What This Session Is….
• Additional session to go with the Linux Scripting session at
PASS Summit
• Tips and Tricks to be used with Shell Scripting
• Best Practices for those already familiar with BASH Scripting
• This is NOT a session to teach you Linux Scripting, but the
valuable tips will help you once you learn BASH to be a better
script author.
Scripts are Representations of the Author:
They have patterns and styles that can be recognized as the
authors:
“To be or not to be…”
7
Choose Wisely How You Author Your
Scripts
• The difficult to navigate script
• The unsure of what is expected script
• The “did it actually run?” script
• The difficult to work with script
• The “everything and the kitchen sink” script
• The “I would rather pull my own teeth than have to update,
enhance, execute” script
8
Even if You Don’t Already Know How to
BASH…
• The following tips are good to consider in any scripting
language when available
• Are good practice to be a good coding team member
• May save your life some day, (or keep you from getting killed
by your team members…)
9
Set your choice in shell
• For many Linux machines, there may be more than one:
• Find out which shell is in use:
which bash
Setting it in your script is done at the very first line of your script:
#!/bin/bash
OR
#!/bin/sh -C Shell
#!/bin/ksh -Korn Shell
What Happens If You Don’t?
Normal Execution with the shell set in the script:
./<script name>/sh <arg1> <arg2>
Without shell set in script:
/bin/bash ./<script name>/sh <arg1> <arg2>
The script must state what shell is to be used with the script EVERY
TIME.
Set up Alias’ and Environment Variables
• Update the .bashrc with global alias’ and environment
variables that support anything that is used by the login
regularly.
• Create .profile with a unique extension, (.profile_sql19,
.profile_net) to support unique applications.
This cuts down on significant variable setting and coding,
requiring only one location to update/manage.
Write a Header for your Script
• The # sign can help create a header and signal BASH that it’s for
informational purposes only:
####################################################
# Title: mk_sqlsrvrvm.sh #
# Purpose: Creates a VM from image supporting #
# SQL Server DB #
# Author: Kellyn Gorman #
# Notes: Requires 4 args as part of execution. #
####################################################
Don’t Leave Others in the Dark
• Comment in your scripts
• Write in ideas for enhancements
• Help explain the logic
• Use the # sign to signal your script that it’s a comment.
# This step builds out the database logical objects.
# If the variables aren’t entered, the script will exit.
Don’t Make Users Guess
If there is a step that requires interaction between your script
and the user, make it clear what is required to promote success:
This can be done using the ECHO command and placing the
statement inside quotes.
echo “Please enter the name of the user:”
15
Exit When Mistakes are Made
• Saves from clean up, easier to recover from.
• Added at the top of the script under the designation of shell
set –e
set –o errexit
Also Exit if Undeclared Variables, etc.
Blank answers for variables, (arguments) can leave a script to
execute incorrectly or worse. Require declarations to be set
completely or the script exits:
set -o nounset
OR
set –u
set -euo pipefail
Don’t throw away your other scripts
• Just as with .Net, Java, Perl, etc., you can run PowerShell
scripts from BASH:
pwsh <script name>
You worked hard on scripts or an existing example
already exists. Don’t recreate the wheel and consider
reusing them, calling them from your BASH script.
Azure Cloud Shell
• Supports both BASH and PowerShell
• Can be used with persistent cloud storage
https://docs.microsoft.com/en-us/azure/cloud-shell/overview
Add Debugging to Your Script
• Want to know what went wrong?
#!/bin/bash
set -vnx
Arguments, (any or all) to be used:
Argument What it Does
-v Verbose mode- shows all lines as they are parsed by the
execution.
-n For syntax checking. The script doesn’t actually execute.
-x Shell tracing mode- will step through each step and report
any errors
Enable BASH History on the Host
Allows you to monitor history of BASH commands on the host,
even when there are multiple sessions open:
shopt -s histappend
This is just a good idea to have enabled so that you can use
the arrow keys for history of commands.
Typing Less
• Hit the TAB key once to auto-complete commands
• Hit the TAB key twice to show all options for a command
Implement Command Correct
• Nothing worse than having to correct typos, so make the
host correct them for you by enabling cdspell:
$ /bin/bsah
/bin/bash
$
shopt -s cdspell
Setting Colors in Text in .bashrc
• # enable colors
eval "`dircolors -b`"
# make the dir command work similar to windows
alias dir='ls --color=auto --format=long'
# make grep highlight results using color
export GREP_OPTIONS='--color=auto’
*This is already turned on in the Azure Cloud Shell
24
Schedulers
• CRON is KING
• CRON is a table, containing a schedule of tasks to run on
intervals or a one-time execution
• -e argument is most common or –u, (user) to edit
• -l to list
crontab -e
25
https://www.computerhope.com/unix/ucrontab.htm
Scheduling in the Cron File
• Syntax is important
• Minute, Hour, Day, Month
• Intervals, multiple hours and other advance options can be
used.
15 18 * * * /home/mssql/msbackup.sh
• Script msbackup will run everyday at 6:15pm.
26
Guaranteeing Scripts Continue to Run
• NoHUP
Execute in one line, the script name, any arguments, send the
output to dev/null and the ampersand asks the script to be run
in the background.
<scriptname>.sh <arg1> <arg2> &>/dev/null &
Disown the Script
• Allows the system to own and run the process, run it in the
background, even if you are disconnected:
disown <scriptname>.sh <arg1> <arg2> &
• You can then check the script is running from the JOBS
command:
Jobs
[1]+ Running <scriptname>.sh
28
Utilities to Support Script Execution
• Screen
• screen -A -m -d -S <screenname> ./<scriptname>.sh &
• Rsync with SSH
• Rsync ssh user@hostb 'bash -s’ < <scriptname>.sh <arg1> <arg2>
• Nohup
• nohup ./<scriptname>.sh > log.out 2> log.err < /dev/null &
Always note the TIME ZONE of the host with the “date”
command!
29
Summary
• Comment and comment often.
• Make your scripts interactive when a user is the one
executing them.
• Set up environments with variables and alias’ to save on
typing.
• Use utilities to ensure that if you are disconnected, your script
won’t be!
• Don’t reinvent the wheel- if the script already exists, execute
it from inside your script, don’t rewrite it.
30
If You Want to Learn More:
Blog Posts, Writing Shell Scripts, Parts 1-4
PASS Summit Session, Empowering the SQL Server Professional
with Linux Scripting- Thursday, Nov. 7th, 1:30pm, RM 611-614
Web Tutorials, Linux Shell Scripting
Edx Class, Linux Command Line Basics
Linux Scripting Class, Linux Tutorials
31
Section break
Questions?
Coming up next…
Why You Need Query Store
Erin Stellato
Thank you for attending
@sqlpass
#sqlpass
@PASScommunity
Learn more from Kellyn Gorman
@DBAKevlar kellyn.gorman@Microsoft.com
PASS 24HOP Linux Scripting Tips and Tricks

More Related Content

What's hot (20)

PPTX
Docker for the enterprise
Bert Poller
 
PPTX
How bigtop leveraged docker for build automation and one click hadoop provis...
Evans Ye
 
PPTX
Automated Deployment Pipeline using Jenkins, Puppet, Mcollective and AWS
Bamdad Dashtban
 
PDF
Coscup
Giivee The
 
KEY
DrupalCon 2011 Highlight
Supakit Kiatrungrit
 
PPTX
Perforce Helix Never Dies: DevOps at Bandai Namco Studios
Perforce
 
PPTX
Cloudy with a Chance of Databases
Kellyn Pot'Vin-Gorman
 
PPTX
Opinionated containers and the future of game servers by Brendan Fosberry
Docker, Inc.
 
PPTX
Enterprise Docker Requires a Private Registry
Chris Riley ☁
 
PPTX
Docker in a big company
Docker, Inc.
 
PPTX
Devoxx 2016 - Docker Nuts and Bolts
Patrick Chanezon
 
PPTX
Alfresco spk-alfresco-day
Maurizio Pillitu
 
PPTX
Gabriele Columbro - Maurizio Pillitu - Get your Alfresco project from Zero to...
Symphony Software Foundation
 
PPTX
Chef vs Puppet vs Ansible vs Saltstack | Configuration Management Tools | Dev...
Simplilearn
 
PDF
WSO2Con US 2015 Kubernetes: a platform for automating deployment, scaling, an...
Brian Grant
 
KEY
What Big Data Folks Need to Know About DevOps
Matt Ray
 
PPTX
Jenkins and Chef: Infrastructure CI and Automated Deployment
Dan Stine
 
PPTX
Nanog75, Network Device Property as Code
Damien Garros
 
PDF
Transforming Application Delivery with PaaS and Linux Containers
Giovanni Galloro
 
PDF
Finding and Organizing a Great Cloud Foundry User Group
Daniel Krook
 
Docker for the enterprise
Bert Poller
 
How bigtop leveraged docker for build automation and one click hadoop provis...
Evans Ye
 
Automated Deployment Pipeline using Jenkins, Puppet, Mcollective and AWS
Bamdad Dashtban
 
Coscup
Giivee The
 
DrupalCon 2011 Highlight
Supakit Kiatrungrit
 
Perforce Helix Never Dies: DevOps at Bandai Namco Studios
Perforce
 
Cloudy with a Chance of Databases
Kellyn Pot'Vin-Gorman
 
Opinionated containers and the future of game servers by Brendan Fosberry
Docker, Inc.
 
Enterprise Docker Requires a Private Registry
Chris Riley ☁
 
Docker in a big company
Docker, Inc.
 
Devoxx 2016 - Docker Nuts and Bolts
Patrick Chanezon
 
Alfresco spk-alfresco-day
Maurizio Pillitu
 
Gabriele Columbro - Maurizio Pillitu - Get your Alfresco project from Zero to...
Symphony Software Foundation
 
Chef vs Puppet vs Ansible vs Saltstack | Configuration Management Tools | Dev...
Simplilearn
 
WSO2Con US 2015 Kubernetes: a platform for automating deployment, scaling, an...
Brian Grant
 
What Big Data Folks Need to Know About DevOps
Matt Ray
 
Jenkins and Chef: Infrastructure CI and Automated Deployment
Dan Stine
 
Nanog75, Network Device Property as Code
Damien Garros
 
Transforming Application Delivery with PaaS and Linux Containers
Giovanni Galloro
 
Finding and Organizing a Great Cloud Foundry User Group
Daniel Krook
 

Similar to PASS 24HOP Linux Scripting Tips and Tricks (20)

PPTX
Pass Summit Linux Scripting for the Microsoft Professional
Kellyn Pot'Vin-Gorman
 
PPTX
Shell scrpting(payal harne)
PayalHarne
 
PDF
Smooth CoffeeScript
Michael Scovetta
 
PDF
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Michael Lihs
 
PDF
Crafting interactive troubleshooting guides and team documentation for your K...
Manning Publications
 
PPTX
Compliance Automation with InSpec - Chef NYC Meetup - April 2017
adamleff
 
PPTX
Geecon 2019 - Taming Code Quality in the Worst Language I Know: Bash
Michał Kordas
 
KEY
Standardizing and Managing Your Infrastructure - MOSC 2011
Brian Ritchie
 
PPTX
PowerShellForDBDevelopers
Bryan Cafferky
 
PPTX
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
PPTX
Quality code in wordpress
Ran Bar-Zik
 
PPTX
Licão 05 scripts exemple
Acácio Oliveira
 
PDF
Modern Front-End Development
mwrather
 
PPTX
DCRUG: Achieving Development-Production Parity
Geoff Harcourt
 
PDF
DevOps demystified
Xebia IT Architects
 
PDF
Tested and Correct, How to Make Sure Your Documentation Keeps Working
Adam Dangoor
 
PDF
Front End Development Automation with Grunt
Ladies Who Code
 
PPTX
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
PPTX
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
PDF
Introduction to Chef
kevsmith
 
Pass Summit Linux Scripting for the Microsoft Professional
Kellyn Pot'Vin-Gorman
 
Shell scrpting(payal harne)
PayalHarne
 
Smooth CoffeeScript
Michael Scovetta
 
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Michael Lihs
 
Crafting interactive troubleshooting guides and team documentation for your K...
Manning Publications
 
Compliance Automation with InSpec - Chef NYC Meetup - April 2017
adamleff
 
Geecon 2019 - Taming Code Quality in the Worst Language I Know: Bash
Michał Kordas
 
Standardizing and Managing Your Infrastructure - MOSC 2011
Brian Ritchie
 
PowerShellForDBDevelopers
Bryan Cafferky
 
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
Quality code in wordpress
Ran Bar-Zik
 
Licão 05 scripts exemple
Acácio Oliveira
 
Modern Front-End Development
mwrather
 
DCRUG: Achieving Development-Production Parity
Geoff Harcourt
 
DevOps demystified
Xebia IT Architects
 
Tested and Correct, How to Make Sure Your Documentation Keeps Working
Adam Dangoor
 
Front End Development Automation with Grunt
Ladies Who Code
 
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
Introduction to Chef
kevsmith
 
Ad

More from Kellyn Pot'Vin-Gorman (20)

PPTX
2024_sqlsat_Oregon_kgorman_aicantdothedishespptx
Kellyn Pot'Vin-Gorman
 
PPTX
ThePowerofWordsMisguidedDescriptionsUndermineWomen.pptx
Kellyn Pot'Vin-Gorman
 
PDF
Leveraging Instant Extracts with Azure Fabric
Kellyn Pot'Vin-Gorman
 
PDF
Making the Second D in ADHD Stand for Dynamic in Tech
Kellyn Pot'Vin-Gorman
 
PPTX
Silk_SQLSaturdayBatonRouge_kgorman_2024.pptx
Kellyn Pot'Vin-Gorman
 
PPTX
Redgate_summit_atl_kgorman_intersection.pptx
Kellyn Pot'Vin-Gorman
 
PPTX
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
Kellyn Pot'Vin-Gorman
 
PPTX
Boston_sql_kegorman_highIO.pptx
Kellyn Pot'Vin-Gorman
 
PDF
Oracle on Azure IaaS 2023 Update
Kellyn Pot'Vin-Gorman
 
PPTX
IaaS for DBAs in Azure
Kellyn Pot'Vin-Gorman
 
PPTX
Being Successful with ADHD
Kellyn Pot'Vin-Gorman
 
PPTX
Azure DBA with IaaS
Kellyn Pot'Vin-Gorman
 
PPTX
Turning ADHD into "Awesome Dynamic Highly Dependable"
Kellyn Pot'Vin-Gorman
 
PPTX
PASS Summit 2020
Kellyn Pot'Vin-Gorman
 
PPTX
DevOps in Silos
Kellyn Pot'Vin-Gorman
 
PPTX
Azure Databases with IaaS
Kellyn Pot'Vin-Gorman
 
PDF
How to Win When Migrating to Azure
Kellyn Pot'Vin-Gorman
 
PDF
Securing Power BI Data
Kellyn Pot'Vin-Gorman
 
PPTX
Cepta The Future of Data with Power BI
Kellyn Pot'Vin-Gorman
 
PDF
Taming the shrew Power BI
Kellyn Pot'Vin-Gorman
 
2024_sqlsat_Oregon_kgorman_aicantdothedishespptx
Kellyn Pot'Vin-Gorman
 
ThePowerofWordsMisguidedDescriptionsUndermineWomen.pptx
Kellyn Pot'Vin-Gorman
 
Leveraging Instant Extracts with Azure Fabric
Kellyn Pot'Vin-Gorman
 
Making the Second D in ADHD Stand for Dynamic in Tech
Kellyn Pot'Vin-Gorman
 
Silk_SQLSaturdayBatonRouge_kgorman_2024.pptx
Kellyn Pot'Vin-Gorman
 
Redgate_summit_atl_kgorman_intersection.pptx
Kellyn Pot'Vin-Gorman
 
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
Kellyn Pot'Vin-Gorman
 
Boston_sql_kegorman_highIO.pptx
Kellyn Pot'Vin-Gorman
 
Oracle on Azure IaaS 2023 Update
Kellyn Pot'Vin-Gorman
 
IaaS for DBAs in Azure
Kellyn Pot'Vin-Gorman
 
Being Successful with ADHD
Kellyn Pot'Vin-Gorman
 
Azure DBA with IaaS
Kellyn Pot'Vin-Gorman
 
Turning ADHD into "Awesome Dynamic Highly Dependable"
Kellyn Pot'Vin-Gorman
 
PASS Summit 2020
Kellyn Pot'Vin-Gorman
 
DevOps in Silos
Kellyn Pot'Vin-Gorman
 
Azure Databases with IaaS
Kellyn Pot'Vin-Gorman
 
How to Win When Migrating to Azure
Kellyn Pot'Vin-Gorman
 
Securing Power BI Data
Kellyn Pot'Vin-Gorman
 
Cepta The Future of Data with Power BI
Kellyn Pot'Vin-Gorman
 
Taming the shrew Power BI
Kellyn Pot'Vin-Gorman
 
Ad

Recently uploaded (20)

PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
July Patch Tuesday
Ivanti
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 

PASS 24HOP Linux Scripting Tips and Tricks

  • 1. Tips and Tricks Kellyn Gorman, Azure Data Platform Architect Microsoft Scripting for Success on Linux
  • 2. If you require assistance during the session, type your inquiry into the question pane on the right side. Maximize your screen with the zoom button on the top of the presentation window. Please fill in the short evaluation following the session. It will appear in your web browser. Technical Assistance
  • 3. PASS’ flagship event takes place in Seattle, Washington November 5-8, 2019 PASSsummit.com PASS Marathon: Career Development October 8, 2019 Upcoming Events 10
  • 4. Kellyn Gorman Azure Data Platform Architect, Microsoft Azure Data Platform Architect Kellyn has been with Microsoft for over a year now working in the Analytics and AI team in Higher Education, but spends a percentage of her time migrating large Oracle environments over to Azure bare metal. Blogger, Author, Speaker Kellyn writes on two of the top 50 database blogs in the world, known for Oracle and Microsoft technical content, has written five books, including one on Diversity and Inclusion. She mentors, sponsors and speaks in both the Oracle and Microsoft communities as part of giving back to the community. President, Denver SQL Server User Group Kellyn has been the president for over two years now, continuing to support this incredible user group while on the road in her RV, traveling the US. @DBAKevlar https://www.linkedin.com/in/kellyngorman/ [email protected]
  • 5. Kellyn Pot'Vin-Gorman Moderated By: Carlos Lopez Scripting for Success on Linux- Top Tips and Tricks
  • 6. What This Session Is…. • Additional session to go with the Linux Scripting session at PASS Summit • Tips and Tricks to be used with Shell Scripting • Best Practices for those already familiar with BASH Scripting • This is NOT a session to teach you Linux Scripting, but the valuable tips will help you once you learn BASH to be a better script author.
  • 7. Scripts are Representations of the Author: They have patterns and styles that can be recognized as the authors: “To be or not to be…” 7
  • 8. Choose Wisely How You Author Your Scripts • The difficult to navigate script • The unsure of what is expected script • The “did it actually run?” script • The difficult to work with script • The “everything and the kitchen sink” script • The “I would rather pull my own teeth than have to update, enhance, execute” script 8
  • 9. Even if You Don’t Already Know How to BASH… • The following tips are good to consider in any scripting language when available • Are good practice to be a good coding team member • May save your life some day, (or keep you from getting killed by your team members…) 9
  • 10. Set your choice in shell • For many Linux machines, there may be more than one: • Find out which shell is in use: which bash Setting it in your script is done at the very first line of your script: #!/bin/bash OR #!/bin/sh -C Shell #!/bin/ksh -Korn Shell
  • 11. What Happens If You Don’t? Normal Execution with the shell set in the script: ./<script name>/sh <arg1> <arg2> Without shell set in script: /bin/bash ./<script name>/sh <arg1> <arg2> The script must state what shell is to be used with the script EVERY TIME.
  • 12. Set up Alias’ and Environment Variables • Update the .bashrc with global alias’ and environment variables that support anything that is used by the login regularly. • Create .profile with a unique extension, (.profile_sql19, .profile_net) to support unique applications. This cuts down on significant variable setting and coding, requiring only one location to update/manage.
  • 13. Write a Header for your Script • The # sign can help create a header and signal BASH that it’s for informational purposes only: #################################################### # Title: mk_sqlsrvrvm.sh # # Purpose: Creates a VM from image supporting # # SQL Server DB # # Author: Kellyn Gorman # # Notes: Requires 4 args as part of execution. # ####################################################
  • 14. Don’t Leave Others in the Dark • Comment in your scripts • Write in ideas for enhancements • Help explain the logic • Use the # sign to signal your script that it’s a comment. # This step builds out the database logical objects. # If the variables aren’t entered, the script will exit.
  • 15. Don’t Make Users Guess If there is a step that requires interaction between your script and the user, make it clear what is required to promote success: This can be done using the ECHO command and placing the statement inside quotes. echo “Please enter the name of the user:” 15
  • 16. Exit When Mistakes are Made • Saves from clean up, easier to recover from. • Added at the top of the script under the designation of shell set –e set –o errexit
  • 17. Also Exit if Undeclared Variables, etc. Blank answers for variables, (arguments) can leave a script to execute incorrectly or worse. Require declarations to be set completely or the script exits: set -o nounset OR set –u set -euo pipefail
  • 18. Don’t throw away your other scripts • Just as with .Net, Java, Perl, etc., you can run PowerShell scripts from BASH: pwsh <script name> You worked hard on scripts or an existing example already exists. Don’t recreate the wheel and consider reusing them, calling them from your BASH script.
  • 19. Azure Cloud Shell • Supports both BASH and PowerShell • Can be used with persistent cloud storage https://docs.microsoft.com/en-us/azure/cloud-shell/overview
  • 20. Add Debugging to Your Script • Want to know what went wrong? #!/bin/bash set -vnx Arguments, (any or all) to be used: Argument What it Does -v Verbose mode- shows all lines as they are parsed by the execution. -n For syntax checking. The script doesn’t actually execute. -x Shell tracing mode- will step through each step and report any errors
  • 21. Enable BASH History on the Host Allows you to monitor history of BASH commands on the host, even when there are multiple sessions open: shopt -s histappend This is just a good idea to have enabled so that you can use the arrow keys for history of commands.
  • 22. Typing Less • Hit the TAB key once to auto-complete commands • Hit the TAB key twice to show all options for a command
  • 23. Implement Command Correct • Nothing worse than having to correct typos, so make the host correct them for you by enabling cdspell: $ /bin/bsah /bin/bash $ shopt -s cdspell
  • 24. Setting Colors in Text in .bashrc • # enable colors eval "`dircolors -b`" # make the dir command work similar to windows alias dir='ls --color=auto --format=long' # make grep highlight results using color export GREP_OPTIONS='--color=auto’ *This is already turned on in the Azure Cloud Shell 24
  • 25. Schedulers • CRON is KING • CRON is a table, containing a schedule of tasks to run on intervals or a one-time execution • -e argument is most common or –u, (user) to edit • -l to list crontab -e 25 https://www.computerhope.com/unix/ucrontab.htm
  • 26. Scheduling in the Cron File • Syntax is important • Minute, Hour, Day, Month • Intervals, multiple hours and other advance options can be used. 15 18 * * * /home/mssql/msbackup.sh • Script msbackup will run everyday at 6:15pm. 26
  • 27. Guaranteeing Scripts Continue to Run • NoHUP Execute in one line, the script name, any arguments, send the output to dev/null and the ampersand asks the script to be run in the background. <scriptname>.sh <arg1> <arg2> &>/dev/null &
  • 28. Disown the Script • Allows the system to own and run the process, run it in the background, even if you are disconnected: disown <scriptname>.sh <arg1> <arg2> & • You can then check the script is running from the JOBS command: Jobs [1]+ Running <scriptname>.sh 28
  • 29. Utilities to Support Script Execution • Screen • screen -A -m -d -S <screenname> ./<scriptname>.sh & • Rsync with SSH • Rsync ssh user@hostb 'bash -s’ < <scriptname>.sh <arg1> <arg2> • Nohup • nohup ./<scriptname>.sh > log.out 2> log.err < /dev/null & Always note the TIME ZONE of the host with the “date” command! 29
  • 30. Summary • Comment and comment often. • Make your scripts interactive when a user is the one executing them. • Set up environments with variables and alias’ to save on typing. • Use utilities to ensure that if you are disconnected, your script won’t be! • Don’t reinvent the wheel- if the script already exists, execute it from inside your script, don’t rewrite it. 30
  • 31. If You Want to Learn More: Blog Posts, Writing Shell Scripts, Parts 1-4 PASS Summit Session, Empowering the SQL Server Professional with Linux Scripting- Thursday, Nov. 7th, 1:30pm, RM 611-614 Web Tutorials, Linux Shell Scripting Edx Class, Linux Command Line Basics Linux Scripting Class, Linux Tutorials 31
  • 34. Coming up next… Why You Need Query Store Erin Stellato
  • 35. Thank you for attending @sqlpass #sqlpass @PASScommunity Learn more from Kellyn Gorman @DBAKevlar [email protected]

Editor's Notes

  • #2: [Moderator Part] Hello and welcome everyone to this 24 Hours of PASS: Summit Preview 2019! We’re excited you could join us today for Kellyn Pot'Vin-Gorman’s session, Scripting for Success on Linux- Top Tips and Tricks. This 24 Hours of PASSconsists of 24 consecutive live webinars, delivered by expert speakers from the PASS community. The sessions will be recorded and posted online after the event. You will receive an email letting you know when these become available. My name is Carlos Lopez [you can say a bit about yourself here if you’d like] I have a few introductory slides before I hand over the reins to Kellyn. [move to next slide]
  • #3: [Moderator Part] If you require technical assistance please type your request into the question pane located on the right side of your screen and someone will assist you. This question pane is also where you may ask any questions throughout the presentation. Feel free to enter your questions at any time and once we get to the Q&A portion of the session, I’ll read your questions aloud to Kellyn. You are able to zoom in on the presentation content by using the zoom button located on the top of the presentation window. Please note that there will be a short evaluation at the end of the session, which will pop-up after the webinar ends in your web browser. Your feedback is really important to us for future events, so please take a moment to complete this. [Note to Carlos Lopez: You need to determine which questions are the most relevant and ask them out loud to the presenter]. [move to next slide]
  • #4: [Moderator Part] Our next scheduled PASS Marathon event is focused on Career Development and will take place on October 8th. Watch your inbox for an email when registration opens. Also, PASS Summit 2019 will be happening on November 5th in Seattle Washington. Head over to PASSsummit.com and register today!   [move to next slide]
  • #5: [Moderator Part] This 24 Hours of PASS session is presented by Kellyn Pot'Vin-Gorman. Kellyn is a member of the Oak Table Network and an Idera ACE and Oracle ACE Director alumnus. She is a Data Platform Architect in Power BI with AI in the EdTech group at Microsoft. Kellyn is known for her extensive work with multi-database platforms, DevOps, cloud migrations, virtualization, visualizations, scripting, environment optimization tuning, automation, and architecture design. Kellyn has spoken at numerous technical conferences for Oracle, Big Data, DevOps, testing, and SQL Server. Her blog (http://dbakevlar.com) and social media activity under her handle, DBAKevlar, is well respected for her insight and content. [move to next slide]
  • #6: And without further ado, here is Kellyn with Scripting for Success on Linux- Top Tips and Tricks. {speaker begins} **[Speaker takes over]**
  • #29: You an kill the job by typing in Kill and the job number shown at the left of “Running”
  • #34: [Moderator Part] The moderator’s script will be added in this section after deck submission.
  • #35: Stay tuned for our next session, Why You Need Query Store with Erin Stellato. [move to next slide]
  • #36: [Moderator Part] The moderator’s script will be added in this section after deck submission.
  • #37: [Moderator Part] The moderator’s script will be added in this section after deck submission.