<<

Scripting Basics Overview

Table of Contents

Scripting Basics ...... 2

Scripting Overview -1 ...... 3

Scripting Overview -2 ...... 4

Scripting Overview -3 ...... 6

Scripting Overview -4 ...... 8

The Case for Scripting -1 ...... 9

The Case for Scripting -2 ...... 11

Scripting Options ...... 13

Scripting Options -1 ...... 14

Scripting Options -2 ...... 15

Notices ...... 17

Page 1 of 17 Scripting Basics

Scripting Basics

2

**002 We'll with some Scripting Basics. So this isn't just specifically about BATCH, but in general about scripting files and languages in general.

Page 2 of 17 Scripting Overview -1

Scripting Overview -1

A “script” is a set of commands run in sequence in the order they appear in a file. The script itself is nothing more than a text file the interprets. A script is NOT compiled – it is text.

Windows BATCH Script

3

**003 So what is a script? It is generally a list of commands that are run in sequence, right? They actually run in order, so... Although they can jump around if you have some settings to do so. The script itself is nothing more than a text file, although for BATCH files the extension has to be dot B-A-T, .bat, for the operating system to understand that and for it to run. So scripts are generally not compiled. They are interpreted. They are just text. So if you think of it as if you have one language, say, English, and wherever you're trying to get it to, the computer, needs it in different language, Spanish, a compiled language would be where

Page 3 of 17 the language is changed beforehand versus if it's interpreted language, it would be just like handing that document or whatever to an interpreter and that person would change it to a different language for your audience. And in this case, that would be the computer.

Scripting Overview -2

Scripting Overview -2

Any that can be run at the command line in an operating system can be put in a script. Many commands “could” be put on a single line, but it is ungainly. Scripts can run multiple commands, interact with it, process the output, and feed it back into additional commands.

4

**004 Okay. Any command that you can run on the Windows command line, the CLI, command line interface, can be run on scripts. The way it works from the command line, you can actually put in all the information you need for a script into a single line, but then that becomes

Page 4 of 17 one giant and unsightly kind of unwieldy thing and it's hard to obviously, be harder to read, readability, and it's harder to maintain. So it's good idea put it into a script.

Scripts can run multiple commands. You can interact with it as you need to. Not necessarily a lot of interaction, but it does give you the ability to do that. You can process the output and then you can also, one of the stronger things you can do with it, is you can take an output of the one command and you can feed it back into another.

Page 5 of 17 Scripting Overview -3

Scripting Overview -3

Scripting was made to do “things” quickly and automate repetitive tasks. • Why do the work when you have a computer to do it for you? Scripting languages have evolved to include more advanced technology. • Bash and Windows Batch are ancient relics, but still useful. • was created with string manipulation in mind. • Ruby and Python were created with network access in mind. The things a is really good at is usually why the script was created to begin with! • Exposing functionality, simplifying commands, etc.

5

**005 So scripting in general, some of you already alluded to it. Was pretty much made to do things quickly and repetitive tasks easier and consistent so you don't have to worry about hand jamming it in and perhaps mis-typing something. You can--so why would you want to hand jam it in when you can have the computer do that for you?

Languages have kind of been through an evolution for scripting. Linux Bash with a born-again script has been around for a long, long time and Windows BATCH slides. That's ancient relics, but perhaps about 30 to 30-plus years old, but still quite

Page 6 of 17 useful. Perl is another scripting language, and that was created for report generation and text manipulation in mind, and then Ruby and Python are object-oriented languages and they were really designed to be used for network interaction for connections and such.

So the things that scripting is really good at, you know, the functionality, it lets you get at things rather easily and in a way that you can make it into a readable, a little bit easier to understand, list. And then obviously simplifying the use of these commands.

Page 7 of 17 Scripting Overview -4

Scripting Overview -4

Always an option to mix and match. • Have a Batch file call a Python script… • Have a Ruby script execute a Linux Bash script... Capitalize on the strengths of each scripting language. • Do not restrict yourself to a particular one. Avoid using scripting languages that do not meet your needs quickly and efficiently. • Chances are, someone else has had this problem and solved it with a special utility or another language. – awk, grep, findstr – for when Batch and Bash cannot do text manipulation

6

**006 Okay. So the nice thing about BATCH scripting is that you can mix and match. You can have a BATCH file that calls a Python script or a Ruby script that executes a Linux Bash script. Have a BATCH script that calls Perl. So it's very nice. You could take advantage of the strengths of each of the languages if you are trying to do more report or text manipulation type of thing. Maybe you'll want to call Perl into it from a base BATCH file or a base Linux Bash script.

So we recommend you try to take advantage of the strengths but also understand the weaknesses of the

Page 8 of 17 language and try not to use something and kind of kludge your way through to make something happen. There's usually another language that does something better or somebody came up with a solution that's a better way to deal with this. In particular, like, BATCH doesn't do well with text manipulation or string manipulation, so it's recommended that something like an awk or a grep or a findstr might be a good solution for this.

The Case for Scripting -1

The Case for Scripting -1

Why use a script? • Fast to code and run • Usually very portable – Batch is on every PC. – Bash is on Linux. • Can be run across the network • Other programs and scripts can be called as needed.

7

**007 So The Case for Scripting. So why would you want to use scripting? Some of you alluded to the

Page 9 of 17 automation piece. It's pretty fast to code, because if you already know the command that you want to run, that's exactly what you put in the script. It's portable in the sense that it's available, right? All PCs have BATCH. The OS interpreter is built in. For Linux Bash, the born-again is also available, in most Starnix distributions, as I understand it, and other Linux distributions. They can be run across the network, which is very nice when you're trying to do multiple things, say, across the domain. And also other programs and scripts, like I've mentioned before, can be called as you need to.

Page 10 of 17 The Case for Scripting -2

The Case for Scripting -2

Use BATCH scripts to • Run one command on MULTIPLE computers • Run multiple commands on MULTIPLE computers Avoid BATCH scripts for • Robust user interaction • Processing large amounts of data • String and text operations (unless comfortable with GREP / AWK / CUT…) Point of Diminishing Returns: 5-10 Iterations • Use brute force if less than 5-10 iterations in a task • It will probably take longer to remember how to do something than just doing it!

8

**008 So the advantage of BATCH scripting is if you need to do one task over multiple machines, or multiple tasks over multiple machines, right? And to be honest, it's not a crime to do multiple commands on a single machine. You just have the one machine that you have to do a lot of things on. There's nothing wrong with making a script for that.

So there are times that you should kind of be aware of that perhaps a script is not necessarily the, BATCH scripting, I should say, is not necessarily the best solution for your script. If you do need a lot of user interaction, BATCH doesn't

Page 11 of 17 necessarily handle that the best, so perhaps another solution is out there. If you're processing large amounts of data, this is also not something that it was really designed for. So probably recommended to use a different solution at that point.

And I mentioned the string and text operations. You may want to use grep or awk or cut or something like that within the scripts to take care of that portion of it. So with scripting, there's a point where you should consider whether or not you should be making a script or perhaps it would take a little bit less time to go ahead and hand jam it and do it manually. Probably five medium- sized tasks. Perhaps you don't even need to take the time to research and make your script. You can go ahead and do that and, you know, all the way up to, like, 10 simpler tasks. You can probably, by the time you do all your research and you try to figure out how the syntax works, if you haven't used it in a while, you might as well go ahead and do it to save time.

Page 12 of 17 Scripting Options

Scripting Options

9

**009 So Scripting Options.

Page 13 of 17 Scripting Options -1

Scripting Options -1

Many available • Windows BATCH • Visual Basic Script • Visual Basic for Applications • Javascript • Linux BASH • Python • Perl • Ruby • OpenOffice

10

**010 So there are quite a few out there. Windows BATCH. Visual Basic Script and Visual Basic for Applications, they're a subset of the Visual Basic language. They're similar, but they're not exactly the same. They give you access to the Windows API, which is a nice feature. Javascript is pretty well known in the web development community. It does a lot of stuff in the background. Excuse me. Behind the scenes. Linux Bash, of course, ubiquitous. And Python, Perl and Ruby, and then even OpenOffice has an actual framework for scripting now.

Page 14 of 17 Scripting Options -2

Scripting Options -2

The scripting language used will depend on a few factors • Knowledge / comfort with it • Its ability to “get the job done” • The platform needed to run it on • Portability and accessibility Remember, scripting is for getting something done quickly. • It could be done in 100 lines of Ruby. – But why bother if BASH can do it in a few lines?

11

**011 So some of the factors when you're deciding on what kind of language you might be using. Obviously, you know, the level of knowledge and the comfort you have with it will . And then its ability to get what you're trying to do done the best way and hopefully the most efficiently. And perhaps the platform you need to run on, if it is Windows, you're going to have a certain set of scripts that you'll be using. And if it's in Linux platform you could use others. And then the portability and the accessibility. Sometimes you can do something in Python if you have a hybrid network that has both Linux and Windows and that way you don't

Page 15 of 17 have to write one in BATCH and one in Bash. You can write something in Python, perhaps, that can do both.

And it's definitely, scripting is definitely, about getting things done quick. So again, if you kind of need to kludge something in one language with a bunch of lines of code when you can actually just use a different solution and do it in a few, it's highly recommended. Yes?

Chris Evans: I think the last bullet up here is a key point to make. And I think that it's really important for people who are doing scripting to understand that there are a lot of different options available to you, right? And one solution may not do exactly what you need. So if you're used to working with multiple programming languages, you know it's easier to do something in one language than another. The same holds true here for scripting, right? Where if you're looking at, you know, "I want to write a BATCH file, but there are certain things in it that are really hard to do," like text manipulation is one you said. Look at, "What other things can I use? How can I do the main operations or the iterations in BATCH but then call another script or another type of script to do the really hard text manipulation part?" And so there's no reason why you can't switch back and forth between different scripting languages, but the point is you want to get this done relatively quickly. If you spend a day coming up with a solution and doing sprints, if you will,

Page 16 of 17 on developing your script, you've probably lost whatever gains you would've seen by automating this with a script to begin with. So I think it's just a piggyback on this final bullet here. Understand the realm of options that are available to whatever platform you're using, and try to use the most appropriate language or the most appropriate script for you. You know, I think that tends to work a lot better than trying to, as Wayne said, kludge something together on it."

Instructor: That's good point. Thank you.

Notices

Notices

© 2015 Carnegie Mellon University

This material is distributed by the Software Engineering Institute (SEI) only to course attendees for their own individual study. Except for the U.S. government purposes described below, this material SHALL NOT be reproduced or used in any other manner without requesting formal permission from the Software Engineering Institute at [email protected]. This material was created in the performance of Federal Government Contract Number FA8721-05-C- 0003 with Carnegie Mellon University for the operation of the Software Engineering Institute, a federally funded research and development center. The U.S. government's rights to use, modify, reproduce, release, perform, display, or disclose this material are restricted by the Rights in Technical Data- Noncommercial Items clauses (DFAR 252-227.7013 and DFAR 252-227.7013 Alternate I) contained in the above identified contract. Any reproduction of this material or portions thereof marked with this legend must also reproduce the disclaimers contained on this slide. Although the rights granted by contract do not require course attendance to use this material for U.S. government purposes, the SEI recommends attendance to ensure proper understanding. THE MATERIAL IS PROVIDED ON AN “AS IS” BASIS, AND CARNEGIE MELLON DISCLAIMS ANY AND ALL WARRANTIES, IMPLIED OR OTHERWISE (INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, RESULTS OBTAINED FROM USE OF THE MATERIAL, MERCHANTABILITY, AND/OR NON-INFRINGEMENT). CERT ® is a registered mark owned by Carnegie Mellon University.

6

Page 17 of 17