SlideShare a Scribd company logo
1 of 52
Empowering the SQL
Server Professional
with Linux Scripting
Kellyn Gorman,
Azure Data Platform Architect
Microsoft
Please silence
cell phones
everything PASS
has to offer
Free online
webinar events
Free 1-day local
training events
Local user groups
around the world
Online special
interest user groups
Business analytics
training
Get involved
Free Online Resources
Newsletters
PASS.org
Explore
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
What This Session Is….
• Teach you the basics around Linux, (aka bash) scripting.
• Play along if you want, just need a Linux machine to log into or
log onto.
• Scripts and slides will be available post the session on
https://github.com/Dbakevlar/Summit2019
• This includes a VI(M) Cheatsheet!
One Way: Azure Cloud Shell
https://docs.microsoft.com/en-us/azure/cloud-shell/overview
Supports both BASH and PowerShell
Can be used with persistent cloud storage
Choose Wisely How You Author Your
Scripts
Scripts should be easy to:
• Read
• Edit
• Execute
8
Even if You Don’t
Already Know
How to BASH…
1. The following tips are good to
consider in any scripting language
when available
2. Are good practice to be a good
coding team member
3. May save your life some day, (or
keep you from getting killed by your
team members… )
9
Writing a Script Should be…
Like writing a paper. It should include the following:
• An Introduction
• A Body
• A Conclusion
10
The Introduction
11
Set the Shell to Use
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
For many Linux machines, there may be more than one:
What Happens If You Don’t?
./<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.
Normal Execution with the shell set in the script:
Exit When Mistakes are Made
Added at the top of the script under the designation of shell
set –e
set –o errexit
Saves from clean up, easier to recover from.
Also Exit if Undeclared Variables, etc.
Require declarations to be set completely or the script exits:
set -o nounset
OR
set –u
set -euo pipefail
Blank answers for variables, (arguments) can leave a script
to execute incorrectly or worse.
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
Set up Alias’ and Environment Variables
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.
Update the .bashrc with global alias’ and environment
variables that support anything that is used by the login
regularly.
Write a Header for your Script
The # sign can help create a header and signal BASH that it’s for
informational purposes only:
####################################################
# Title: summit_demo.sh #
# Purpose: Summit Demo script for Linux #
# Author: Kellyn Gorman #
# Notes: Script will need three arguments. #
####################################################
Four Choices in Passing Environment
Variables
1. Declaration hard-coded in script
2. Passed as part of execution command for script
3. Interactively read as part of script execution
4. Dynamically generated from other values in script
19
Choose Wisely
20
Variable Type Pro Con
Hard-coded No typos Static, no interaction
Passed during
execution
More interactive and code
is more dynamic, great for
automation
Can suffer typos, no hints
of values required
Interactively read as
part of execution
Very interactive and can be
prompted with
hints/options
Requires interaction and
not made for scheduling
or automation
Dynamically
generated from
other values
Dynamically happens,
requires no/little input
from users. Excellent for
automation
Little/no control over
values, dependent on
values passed or existing
from sources.
Start of our Script
21
#!/bin/bash
#############################################
# Script Name: summit_demo_int.sh #
# Author: Kellyn Gorman #
# Usage: For Linux Scripting Demo #
# Notes: Hard coded values to begin #
#############################################
export dir_name = summitdir
export file_name = summit.lst
export log_name = summit.log
summit_demo_int.sh
How to use Variables Once Declared in
a Script
dir_name becomes $dir_name
file_name becomes $file_name
…and so on…
Any variables in the .bashrc or .profile can be used in scripts
and if reused often, should be considered set in these files.
22
Move from Hard Coding to Passing
Variables at the Execution
Changed at the Introduction of the script
Makes script more robust and flexible
Makes script reusable
Can be done multiple ways
• Set at session/logon
• Set as part of script execution
23
Start of our Script
24
#!/bin/bash
#############################################
#############################################
export dir_name = <generic dir path>
export file_name = <static file name>
export log_name = <static log name>
Export the summitdir variable and run the summit_demo_arg.sh:
export dir_name=summitdir
./summit_demo_arg.sh
summit_demo_arg.sh
Update the Script to Interactive Values
export dir_name = <dir path>
export file_name = <generic_file_name>
export log_name = <generic_log_name>
To execute, we would change to execution path ./<script name> $1
Export the environment variables as part of a profile or part
of your .bashrc OR add to the script/session. This is one
more way to make the script easier to use.
25
Start of our Script
26
#!/bin/bash
export acro=$1
#############################################
#############################################
# Directory is already set, just need to set the file/log name
# Note, the files are unique to the script, but generic to the
environment session:
export dir_name=${dir_name}/${acro}<dir_name>
export log_name = ${dir_name}/${acro}<log_name>
summit_demo_val.sh
Pass Dynamic Values into our Script,
Step 1
usage() { echo "Usage: $0 -f <filename> -l
<logname>" 1>&2; exit 1; }
declare file_name=""
declare log_name=""
27
Pass Dynamic Values into our Script,
Step 2
# Initialize parameters specified from command line
while getopts ":f:l:" arg; do
case "${arg}" in
f)
filename=${OPTARG}
;;
l)
logname=${OPTARG}
;;
esac
Done
shift $((OPTIND-1))
28
Pass Dynamic Values into our Script,
Step 3
if [[ -z "$filename" ]]; then
echo “Type in the name of your file:"
read filename
[[ "${filename:?}" ]]
fi
if [[ -z "$logname" ]]; then
echo “Type in your logname:”
read logname
[[ "${logname:?}" ]]
fi
29
Script has changed on how we setup our
introduction, but body stays the same….
Save your file, (“Esc, :q!” in VIM/VI, which is listed in your VI(M)
cheat sheet)
30 summit_demo_dyn.sh
Test your Environment Variables
Use the scripts you’ve created
Make sure they are executable:
chmod 744 *.sh
Run each of the scripts
Test the variables, are they set?
echo $<variable name>
Do this for each script for each variable…
31
The Body
32
The Body of the Script
This is where the code will perform the work as part of the main purpose of
the script.
As when writing a paper, this will be the largest section of your
script.
Start simple
Add debugging and error exit options as you build out your
body.
*Consider building a script as functions- easier to manage and test.
33
Don’t Leave Others in the Dark
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.
Comments in your scripts
Goal of Script
• Create a directory, (mkdir)
• Create an empty file, (touch)
• Confirm the creation of the directory with a list, (ls) and write
to a log file, ( >)
• Confirm the creation of the file with a list, (ls) and append to a
log file, (>>)
35
Create the Body, (Post the Variables)
36
export dir_name = summitdir
export file_name = ${summitdir}/summit.lst
export log_name = ${dir_name}/summit.log
# Create new directory
mkdir ./${summitdir}
# Create empty file
touch ./${file_name}
# Verify that directory and file exist
ls ./${summitdir} > $log_name
ls ./${file_name} >> $log_name
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:”
37
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.
Build Out Functions
• Functions allow you to group commands and execute
them as part of a function name.
• Write in any order, execute in the order you want
• Place execution at the end of the script to make it easy
for managing, testing and understanding steps in a script
39
Example of Function
# Function Bodies:
function quit {
exit
}
function hello {
echo Hello!
}
# Execute Function:
hello
quit
40
# First function touch files
function touch_func {
touch ${file}
touch ${log_file}
}
# Second function to Verify that directory and file exist
function write_log_func {
pwd ${home}/${dir_name} > $log_file
ls -ltr ${file} >> $log_file
ls -ltr ${log_file} >> $log_file
}
41
Our Functions
Function for Last Step- Commented
Out!
function clean_func {
rm -rf $dir_name/*
rmdir $dir_name
}
Be careful with rm –rf!!
42
The Conclusion
43
If Using Functions
Execute Functions and then-
• Add any last logging steps
• Clean up any files
• Email log files or notifications
44
Complete our Script
# Conclusion
# Execute Functions and clean up
touch_func
write_log_func
#clean_func # function is commented out to
begin!
echo “Script has completed” >> $log_name
45
Always do Clean Up and Notify
Completion
• Remove any files that were created for script.
• Parse log files for success or errors.
• Report on success or errors.
• Notify the script has finished, successfully or even if it
hasn’t.
46
Executing Functions
Functions are executed as part of the conclusion and…
• Makes it easier to test and work with sections of scripts.
• Check out the full script: summit_demo_func.sh
• Run Script with CLEAN function commented out!
47
Test out the Scripts!
• Note the differences
• Note how more commands would be built in.
• Notice logging and how commands can be used as well as
checks.
• More advance utilities/commands: AWK, GREP, SED to do
advanced filtering and searching.
• Use email utilities like sendmail for notifications, alerting
48
Summary
• Learn Vi/Vim, Nano or another editor to make it easy to write
scripts in the Linux terminal.
• Use best practices from the beginning so as scripts mature,
already easy for others to read, manage and use.
• Use dynamic values to make code reusable
• Use functions to make easier to manage and test scripts
• Use exit codes for variables and errors to keep scripts from
running without the right information.
49
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
50
Session
Evaluations
Submit by 5pm Friday,
November 15th to
win prizes.
Download the GuideBook App
and search: PASS Summit 2019
Follow the QR code link on session
signage
Go to PASSsummit.com
3 W A Y S T O A C C E S S
Thank You
Speaker name
@DBAkevlar
dbakevlar@gmail.com

More Related Content

What's hot

Scalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and ApproachesScalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and Approaches
adunne
 
Low Latency SQL on Hadoop - What's best for your cluster
Low Latency SQL on Hadoop - What's best for your clusterLow Latency SQL on Hadoop - What's best for your cluster
Low Latency SQL on Hadoop - What's best for your cluster
DataWorks Summit
 

What's hot (20)

Cloudera Impala: A modern SQL Query Engine for Hadoop
Cloudera Impala: A modern SQL Query Engine for HadoopCloudera Impala: A modern SQL Query Engine for Hadoop
Cloudera Impala: A modern SQL Query Engine for Hadoop
 
2017 OWASP SanFran March Meetup - Hacking SQL Server on Scale with PowerShell
2017 OWASP SanFran March Meetup - Hacking SQL Server on Scale with PowerShell2017 OWASP SanFran March Meetup - Hacking SQL Server on Scale with PowerShell
2017 OWASP SanFran March Meetup - Hacking SQL Server on Scale with PowerShell
 
Melbourne Chef Meetup: Automating Azure Compliance with InSpec
Melbourne Chef Meetup: Automating Azure Compliance with InSpecMelbourne Chef Meetup: Automating Azure Compliance with InSpec
Melbourne Chef Meetup: Automating Azure Compliance with InSpec
 
Bare-metal performance for Big Data workloads on Docker containers
Bare-metal performance for Big Data workloads on Docker containersBare-metal performance for Big Data workloads on Docker containers
Bare-metal performance for Big Data workloads on Docker containers
 
Tuning Apache Ambari performance for Big Data at scale with 3000 agents
Tuning Apache Ambari performance for Big Data at scale with 3000 agentsTuning Apache Ambari performance for Big Data at scale with 3000 agents
Tuning Apache Ambari performance for Big Data at scale with 3000 agents
 
Machine Learning in SQL Server 2019
Machine Learning in SQL Server 2019Machine Learning in SQL Server 2019
Machine Learning in SQL Server 2019
 
End-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL ServerEnd-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL Server
 
YARN and the Docker container runtime
YARN and the Docker container runtimeYARN and the Docker container runtime
YARN and the Docker container runtime
 
Farming hadoop in_the_cloud
Farming hadoop in_the_cloudFarming hadoop in_the_cloud
Farming hadoop in_the_cloud
 
Extreme Availability using Oracle 12c Features: Your very last system shutdown?
Extreme Availability using Oracle 12c Features: Your very last system shutdown?Extreme Availability using Oracle 12c Features: Your very last system shutdown?
Extreme Availability using Oracle 12c Features: Your very last system shutdown?
 
SUSE, Hadoop and Big Data Update. Stephen Mogg, SUSE UK
SUSE, Hadoop and Big Data Update. Stephen Mogg, SUSE UKSUSE, Hadoop and Big Data Update. Stephen Mogg, SUSE UK
SUSE, Hadoop and Big Data Update. Stephen Mogg, SUSE UK
 
Microsoft SQL Server Query Tuning
Microsoft SQL Server Query TuningMicrosoft SQL Server Query Tuning
Microsoft SQL Server Query Tuning
 
Scalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and ApproachesScalable Web Architectures: Common Patterns and Approaches
Scalable Web Architectures: Common Patterns and Approaches
 
Low Latency SQL on Hadoop - What's best for your cluster
Low Latency SQL on Hadoop - What's best for your clusterLow Latency SQL on Hadoop - What's best for your cluster
Low Latency SQL on Hadoop - What's best for your cluster
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architecture
 
Making MySQL highly available using Oracle Grid Infrastructure
Making MySQL highly available using Oracle Grid InfrastructureMaking MySQL highly available using Oracle Grid Infrastructure
Making MySQL highly available using Oracle Grid Infrastructure
 
eProseed Oracle Open World 2016 debrief - Oracle 12.2.0.1 Database
eProseed Oracle Open World 2016 debrief - Oracle 12.2.0.1 DatabaseeProseed Oracle Open World 2016 debrief - Oracle 12.2.0.1 Database
eProseed Oracle Open World 2016 debrief - Oracle 12.2.0.1 Database
 
Upgrade Without the Headache: Best Practices for Upgrading Hadoop in Production
Upgrade Without the Headache: Best Practices for Upgrading Hadoop in ProductionUpgrade Without the Headache: Best Practices for Upgrading Hadoop in Production
Upgrade Without the Headache: Best Practices for Upgrading Hadoop in Production
 
The Challenges of SQL on Hadoop
The Challenges of SQL on HadoopThe Challenges of SQL on Hadoop
The Challenges of SQL on Hadoop
 
Oracle SQL Developer for the DBA
Oracle SQL Developer for the DBAOracle SQL Developer for the DBA
Oracle SQL Developer for the DBA
 

Similar to Pass Summit Linux Scripting for the Microsoft Professional

Shell tutorial
Shell tutorialShell tutorial
Shell tutorial
Vu Duy Tu
 
Lpi Part 2 Basic Administration
Lpi Part 2 Basic AdministrationLpi Part 2 Basic Administration
Lpi Part 2 Basic Administration
YemenLinux
 
L lpic1-v3-103-1-pdf
L lpic1-v3-103-1-pdfL lpic1-v3-103-1-pdf
L lpic1-v3-103-1-pdf
hellojdr
 
Lpi Part 1 Linux Fundamentals
Lpi Part 1 Linux FundamentalsLpi Part 1 Linux Fundamentals
Lpi Part 1 Linux Fundamentals
YemenLinux
 
Red Hat Linux Certified Professional step by step guide Tech Arkit
Red Hat Linux Certified Professional step by step guide Tech ArkitRed Hat Linux Certified Professional step by step guide Tech Arkit
Red Hat Linux Certified Professional step by step guide Tech Arkit
Ravi Kumar
 

Similar to Pass Summit Linux Scripting for the Microsoft Professional (20)

PASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and TricksPASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and Tricks
 
Shell tutorial
Shell tutorialShell tutorial
Shell tutorial
 
Gigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die tryingGigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die trying
 
Software Development Automation With Scripting Languages
Software Development Automation With Scripting LanguagesSoftware Development Automation With Scripting Languages
Software Development Automation With Scripting Languages
 
Autoconf&Automake
Autoconf&AutomakeAutoconf&Automake
Autoconf&Automake
 
Makefile
MakefileMakefile
Makefile
 
Shell scrpting(payal harne)
Shell scrpting(payal harne)Shell scrpting(payal harne)
Shell scrpting(payal harne)
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Programming
ProgrammingProgramming
Programming
 
The "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsThe "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/Ops
 
Kohana 3.2 documentation
Kohana 3.2 documentationKohana 3.2 documentation
Kohana 3.2 documentation
 
Lpi Part 2 Basic Administration
Lpi Part 2 Basic AdministrationLpi Part 2 Basic Administration
Lpi Part 2 Basic Administration
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
 
Feature and platform testing with CMake
Feature and platform testing with CMakeFeature and platform testing with CMake
Feature and platform testing with CMake
 
L lpic1-v3-103-1-pdf
L lpic1-v3-103-1-pdfL lpic1-v3-103-1-pdf
L lpic1-v3-103-1-pdf
 
Lpi Part 1 Linux Fundamentals
Lpi Part 1 Linux FundamentalsLpi Part 1 Linux Fundamentals
Lpi Part 1 Linux Fundamentals
 
One Click Provisioning With Enterprise Manager 12c
One Click Provisioning With Enterprise Manager 12cOne Click Provisioning With Enterprise Manager 12c
One Click Provisioning With Enterprise Manager 12c
 
Red Hat Linux Certified Professional step by step guide Tech Arkit
Red Hat Linux Certified Professional step by step guide Tech ArkitRed Hat Linux Certified Professional step by step guide Tech Arkit
Red Hat Linux Certified Professional step by step guide Tech Arkit
 

More from Kellyn Pot'Vin-Gorman

More from Kellyn Pot'Vin-Gorman (20)

Redgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptxRedgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptx
 
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptxSQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
 
Boston_sql_kegorman_highIO.pptx
Boston_sql_kegorman_highIO.pptxBoston_sql_kegorman_highIO.pptx
Boston_sql_kegorman_highIO.pptx
 
Oracle on Azure IaaS 2023 Update
Oracle on Azure IaaS 2023 UpdateOracle on Azure IaaS 2023 Update
Oracle on Azure IaaS 2023 Update
 
IaaS for DBAs in Azure
IaaS for DBAs in AzureIaaS for DBAs in Azure
IaaS for DBAs in Azure
 
Being Successful with ADHD
Being Successful with ADHDBeing Successful with ADHD
Being Successful with ADHD
 
Azure DBA with IaaS
Azure DBA with IaaSAzure DBA with IaaS
Azure DBA with IaaS
 
Turning ADHD into "Awesome Dynamic Highly Dependable"
Turning ADHD into "Awesome Dynamic Highly Dependable"Turning ADHD into "Awesome Dynamic Highly Dependable"
Turning ADHD into "Awesome Dynamic Highly Dependable"
 
DevOps in Silos
DevOps in SilosDevOps in Silos
DevOps in Silos
 
Securing Power BI Data
Securing Power BI DataSecuring Power BI Data
Securing Power BI Data
 
Cepta The Future of Data with Power BI
Cepta The Future of Data with Power BICepta The Future of Data with Power BI
Cepta The Future of Data with Power BI
 
Taming the shrew Power BI
Taming the shrew Power BITaming the shrew Power BI
Taming the shrew Power BI
 
Power BI with Essbase in the Oracle Cloud
Power BI with Essbase in the Oracle CloudPower BI with Essbase in the Oracle Cloud
Power BI with Essbase in the Oracle Cloud
 
ODTUG Leadership Talk- WIT and Sponsorship
ODTUG Leadership Talk-  WIT and SponsorshipODTUG Leadership Talk-  WIT and Sponsorship
ODTUG Leadership Talk- WIT and Sponsorship
 
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
DevOps and Decoys  How to Build a Successful Microsoft DevOps Including the DataDevOps and Decoys  How to Build a Successful Microsoft DevOps Including the Data
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
 
GDPR- The Buck Stops Here
GDPR-  The Buck Stops HereGDPR-  The Buck Stops Here
GDPR- The Buck Stops Here
 
Taming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI OptionsTaming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI Options
 
DevOps tools for winning agility
DevOps tools for winning agilityDevOps tools for winning agility
DevOps tools for winning agility
 
24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAs24HOP Introduction to Linux for SQL Server DBAs
24HOP Introduction to Linux for SQL Server DBAs
 
Hotsos Advanced Linux Tools
Hotsos Advanced Linux ToolsHotsos Advanced Linux Tools
Hotsos Advanced Linux Tools
 

Recently uploaded

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
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

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 Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Pass Summit Linux Scripting for the Microsoft Professional

  • 1. Empowering the SQL Server Professional with Linux Scripting Kellyn Gorman, Azure Data Platform Architect Microsoft
  • 3. everything PASS has to offer Free online webinar events Free 1-day local training events Local user groups around the world Online special interest user groups Business analytics training Get involved Free Online Resources Newsletters PASS.org Explore
  • 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/ Kellyn.Gorman@Microsoft.com
  • 5.
  • 6. What This Session Is…. • Teach you the basics around Linux, (aka bash) scripting. • Play along if you want, just need a Linux machine to log into or log onto. • Scripts and slides will be available post the session on https://github.com/Dbakevlar/Summit2019 • This includes a VI(M) Cheatsheet!
  • 7. One Way: Azure Cloud Shell https://docs.microsoft.com/en-us/azure/cloud-shell/overview Supports both BASH and PowerShell Can be used with persistent cloud storage
  • 8. Choose Wisely How You Author Your Scripts Scripts should be easy to: • Read • Edit • Execute 8
  • 9. Even if You Don’t Already Know How to BASH… 1. The following tips are good to consider in any scripting language when available 2. Are good practice to be a good coding team member 3. May save your life some day, (or keep you from getting killed by your team members… ) 9
  • 10. Writing a Script Should be… Like writing a paper. It should include the following: • An Introduction • A Body • A Conclusion 10
  • 12. Set the Shell to Use 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 For many Linux machines, there may be more than one:
  • 13. What Happens If You Don’t? ./<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. Normal Execution with the shell set in the script:
  • 14. Exit When Mistakes are Made Added at the top of the script under the designation of shell set –e set –o errexit Saves from clean up, easier to recover from.
  • 15. Also Exit if Undeclared Variables, etc. Require declarations to be set completely or the script exits: set -o nounset OR set –u set -euo pipefail Blank answers for variables, (arguments) can leave a script to execute incorrectly or worse.
  • 16. 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
  • 17. Set up Alias’ and Environment Variables 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. Update the .bashrc with global alias’ and environment variables that support anything that is used by the login regularly.
  • 18. Write a Header for your Script The # sign can help create a header and signal BASH that it’s for informational purposes only: #################################################### # Title: summit_demo.sh # # Purpose: Summit Demo script for Linux # # Author: Kellyn Gorman # # Notes: Script will need three arguments. # ####################################################
  • 19. Four Choices in Passing Environment Variables 1. Declaration hard-coded in script 2. Passed as part of execution command for script 3. Interactively read as part of script execution 4. Dynamically generated from other values in script 19
  • 20. Choose Wisely 20 Variable Type Pro Con Hard-coded No typos Static, no interaction Passed during execution More interactive and code is more dynamic, great for automation Can suffer typos, no hints of values required Interactively read as part of execution Very interactive and can be prompted with hints/options Requires interaction and not made for scheduling or automation Dynamically generated from other values Dynamically happens, requires no/little input from users. Excellent for automation Little/no control over values, dependent on values passed or existing from sources.
  • 21. Start of our Script 21 #!/bin/bash ############################################# # Script Name: summit_demo_int.sh # # Author: Kellyn Gorman # # Usage: For Linux Scripting Demo # # Notes: Hard coded values to begin # ############################################# export dir_name = summitdir export file_name = summit.lst export log_name = summit.log summit_demo_int.sh
  • 22. How to use Variables Once Declared in a Script dir_name becomes $dir_name file_name becomes $file_name …and so on… Any variables in the .bashrc or .profile can be used in scripts and if reused often, should be considered set in these files. 22
  • 23. Move from Hard Coding to Passing Variables at the Execution Changed at the Introduction of the script Makes script more robust and flexible Makes script reusable Can be done multiple ways • Set at session/logon • Set as part of script execution 23
  • 24. Start of our Script 24 #!/bin/bash ############################################# ############################################# export dir_name = <generic dir path> export file_name = <static file name> export log_name = <static log name> Export the summitdir variable and run the summit_demo_arg.sh: export dir_name=summitdir ./summit_demo_arg.sh summit_demo_arg.sh
  • 25. Update the Script to Interactive Values export dir_name = <dir path> export file_name = <generic_file_name> export log_name = <generic_log_name> To execute, we would change to execution path ./<script name> $1 Export the environment variables as part of a profile or part of your .bashrc OR add to the script/session. This is one more way to make the script easier to use. 25
  • 26. Start of our Script 26 #!/bin/bash export acro=$1 ############################################# ############################################# # Directory is already set, just need to set the file/log name # Note, the files are unique to the script, but generic to the environment session: export dir_name=${dir_name}/${acro}<dir_name> export log_name = ${dir_name}/${acro}<log_name> summit_demo_val.sh
  • 27. Pass Dynamic Values into our Script, Step 1 usage() { echo "Usage: $0 -f <filename> -l <logname>" 1>&2; exit 1; } declare file_name="" declare log_name="" 27
  • 28. Pass Dynamic Values into our Script, Step 2 # Initialize parameters specified from command line while getopts ":f:l:" arg; do case "${arg}" in f) filename=${OPTARG} ;; l) logname=${OPTARG} ;; esac Done shift $((OPTIND-1)) 28
  • 29. Pass Dynamic Values into our Script, Step 3 if [[ -z "$filename" ]]; then echo “Type in the name of your file:" read filename [[ "${filename:?}" ]] fi if [[ -z "$logname" ]]; then echo “Type in your logname:” read logname [[ "${logname:?}" ]] fi 29
  • 30. Script has changed on how we setup our introduction, but body stays the same…. Save your file, (“Esc, :q!” in VIM/VI, which is listed in your VI(M) cheat sheet) 30 summit_demo_dyn.sh
  • 31. Test your Environment Variables Use the scripts you’ve created Make sure they are executable: chmod 744 *.sh Run each of the scripts Test the variables, are they set? echo $<variable name> Do this for each script for each variable… 31
  • 33. The Body of the Script This is where the code will perform the work as part of the main purpose of the script. As when writing a paper, this will be the largest section of your script. Start simple Add debugging and error exit options as you build out your body. *Consider building a script as functions- easier to manage and test. 33
  • 34. Don’t Leave Others in the Dark 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. Comments in your scripts
  • 35. Goal of Script • Create a directory, (mkdir) • Create an empty file, (touch) • Confirm the creation of the directory with a list, (ls) and write to a log file, ( >) • Confirm the creation of the file with a list, (ls) and append to a log file, (>>) 35
  • 36. Create the Body, (Post the Variables) 36 export dir_name = summitdir export file_name = ${summitdir}/summit.lst export log_name = ${dir_name}/summit.log # Create new directory mkdir ./${summitdir} # Create empty file touch ./${file_name} # Verify that directory and file exist ls ./${summitdir} > $log_name ls ./${file_name} >> $log_name
  • 37. 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:” 37
  • 38. 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.
  • 39. Build Out Functions • Functions allow you to group commands and execute them as part of a function name. • Write in any order, execute in the order you want • Place execution at the end of the script to make it easy for managing, testing and understanding steps in a script 39
  • 40. Example of Function # Function Bodies: function quit { exit } function hello { echo Hello! } # Execute Function: hello quit 40
  • 41. # First function touch files function touch_func { touch ${file} touch ${log_file} } # Second function to Verify that directory and file exist function write_log_func { pwd ${home}/${dir_name} > $log_file ls -ltr ${file} >> $log_file ls -ltr ${log_file} >> $log_file } 41 Our Functions
  • 42. Function for Last Step- Commented Out! function clean_func { rm -rf $dir_name/* rmdir $dir_name } Be careful with rm –rf!! 42
  • 44. If Using Functions Execute Functions and then- • Add any last logging steps • Clean up any files • Email log files or notifications 44
  • 45. Complete our Script # Conclusion # Execute Functions and clean up touch_func write_log_func #clean_func # function is commented out to begin! echo “Script has completed” >> $log_name 45
  • 46. Always do Clean Up and Notify Completion • Remove any files that were created for script. • Parse log files for success or errors. • Report on success or errors. • Notify the script has finished, successfully or even if it hasn’t. 46
  • 47. Executing Functions Functions are executed as part of the conclusion and… • Makes it easier to test and work with sections of scripts. • Check out the full script: summit_demo_func.sh • Run Script with CLEAN function commented out! 47
  • 48. Test out the Scripts! • Note the differences • Note how more commands would be built in. • Notice logging and how commands can be used as well as checks. • More advance utilities/commands: AWK, GREP, SED to do advanced filtering and searching. • Use email utilities like sendmail for notifications, alerting 48
  • 49. Summary • Learn Vi/Vim, Nano or another editor to make it easy to write scripts in the Linux terminal. • Use best practices from the beginning so as scripts mature, already easy for others to read, manage and use. • Use dynamic values to make code reusable • Use functions to make easier to manage and test scripts • Use exit codes for variables and errors to keep scripts from running without the right information. 49
  • 50. 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 50
  • 51. Session Evaluations Submit by 5pm Friday, November 15th to win prizes. Download the GuideBook App and search: PASS Summit 2019 Follow the QR code link on session signage Go to PASSsummit.com 3 W A Y S T O A C C E S S

Editor's Notes

  1. [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]
  2. Lets the usage be set, there must be two arguments to pass in the declaration or exit.
  3. Let the script know that values will be passed in for the arguments in the following section.
  4. Asking our questions of the person executing the script