Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
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/
Kellyn.Gorman@Microsoft.com
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
35. Thank you for attending
@sqlpass
#sqlpass
@PASScommunity
Learn more from Kellyn Gorman
@DBAKevlar kellyn.gorman@Microsoft.com
Editor's Notes
[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]
[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]
[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]
[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]
And without further ado, here is Kellyn with Scripting for Success on Linux- Top Tips and Tricks.
{speaker begins}
**[Speaker takes over]**
You an kill the job by typing in
Kill and the job number shown at the left of “Running”
[Moderator Part]
The moderator’s script will be added in this section after deck submission.
Stay tuned for our next session, Why You Need Query Store with Erin Stellato.
[move to next slide]
[Moderator Part]
The moderator’s script will be added in this section after deck submission.
[Moderator Part]
The moderator’s script will be added in this section after deck submission.