Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

PASS 24HOP Linux Scripting Tips and Tricks

177 Aufrufe

Veröffentlicht am

Session from 24HOP on Linux Scripting Tips and Tricks

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

PASS 24HOP Linux Scripting Tips and Tricks

  1. 1. Tips and Tricks Kellyn Gorman, Azure Data Platform Architect Microsoft Scripting for Success on Linux
  2. 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. 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. 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. 5. Kellyn Pot'Vin-Gorman Moderated By: Carlos Lopez Scripting for Success on Linux- Top Tips and Tricks
  6. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  32. 32. Section break
  33. 33. Questions?
  34. 34. Coming up next… Why You Need Query Store Erin Stellato
  35. 35. Thank you for attending @sqlpass #sqlpass @PASScommunity Learn more from Kellyn Gorman @DBAKevlar kellyn.gorman@Microsoft.com