There are many ways to automate a computer process. In this video, you’ll learn about the basics of scripting and some of the most popular scripting languages used today.
If you’ve ever worked at the command line of your operating system, you know there is a lot of typing, and very often, there’s a lot of duplication. You’re simply typing the same thing over and over again or performing similar functions over and over again. There are ways to automate this process using scripting. This means that you don’t have to be there typing into your keyboard interactively. Very often, these things can run while you’re sleeping or away from your computer.
And you can monitor your environment. And if problems start to occur, you can have automated scripts go out to resolve those particular issues all without any human intervention. This means you don’t have to be there. You’re not typing the wrong thing into the command line to be able to resolve these issues. And the speed is as fast as the computer can go. These scripts are very good for automating these mundane tasks so that you can be doing something much more productive with your time.
There are a number of characteristics and scripts that are the same no matter what type of scripting language you’re using. One of these basics is a variable. A variable is when you’re assigning a name to a particular value that you’re storing in memory. For example, if you specify that x is equal to 1, and y is equal to x plus 7, then y would be equal to 8. We’ve used the variable x to perform a calculation and assign the results of that calculation to the variable y.
Variables can also hold particular values. For example, we can assign the variable pi with the number 3.14. And the variable could be text. The variable greeting, for example, could be the text “hello and welcome”. Variables are associated with a data type depending on the information you’re storing in the variable. For example, a string data type would be the one like the variable name, which is storing a string or text information, in this case the words “Professor Messer”.
An integer data type is a data type containing a whole number. This would be a number that is not a fraction or has a decimal inside of it. For example, the number 42 can be stored as an integer data type with the variable x. And as you’ve seen previously, we can use these integer data types to perform mathematical calculations that we can use in our script. Another data type dealing with numbers is a floating point data type. These are the numbers that would have decimal points inside of them. For example, the variable pi is a floating point data type that contains the number 3.14.
One common characteristic of a script is we’ll often perform the same function over and over and over again. Inside of the script, we do this by creating a loop. A script may be looping through a particular section of code a certain number of times that we would specify. Or we may tell the script to continue to loop until something else occurs within that code.
And perhaps the thing that is the most challenging part of scripting is documenting what you’re putting in the script. Every scripting language has a method to put comments so that you can annotate the code as you’re creating the script. This allows yourself or someone else to view that script and get an understanding of exactly what’s happening with that script because you’ve put in some plain English annotation along with the scripting code.
You may not realize it, but in your operating system are a number of variables that are already there and waiting for some script to take advantage of them. These types of variables are called environment variables. And they’re used to describe the environment that your operating system is working under.
For example, there are environment variables that would tell you the location of a Windows installation. On this list of environment variables that I got by going to the command line and typing set, you can see at the very bottom is the winder equals C colon backslash Windows. That is the Windows directory that contains the Windows operating system files. Other environment variables might be the name of the computer or the user’s home directory or many, many other details that you can then use within your script.
A very common scripting type in the Windows operating system is a batch file. Batch files commonly have a .bat file extension. And you’re usually running these from the Windows command line. This is a type of scripting that goes back to the DOS and the OS/2 days, so it’s a scripting type that’s very common to see at the Windows command line. If you look at a Windows batch file, you’ll notice there are commands inside of there that are very similar to what you would type at the command line.
For example, the cd command or performing an XCOPY is all part of the script as if you were sitting at the command line and typing this. In this script, for example, the batch file changes directory to a source directory on the C drive. It then checks to see if there are any doc files or text files. And if there is, it will XCOPY those files from the working directory to a production directory. The same thing occurs if it finds JPEG, PNGs, or BMP graphic’s files, and it copies those as well from the working directory to the images directory.
Another type of scripting file you would find in Windows is the PowerShell script. This usually has a ps1 file extension. And you can download PowerShell for many different versions of Windows, but you’ll find PowerShell is included with Windows 8, 8.1, and Windows 10. PowerShell takes the idea of running a Windows batch file and extends it to include nearly every aspect of the Windows operating system.
These PowerShell scripts are referred to as command-lets. And you can run them as scripts inside of the PowerShell window, or they can run as standalone executables. If you’re a Windows system administrator and you need to integrate the operating systems and workstations within Active Directory infrastructure, then you’re probably going to use the features of Windows PowerShell to accomplish that.
Microsoft includes another type of scripting for Windows called the Microsoft Visual Basic Scripting Addition. These scripting files will usually have a vbs file extension, which stands for vb script or Visual Basic script. You can use vb scripts to manage the backend process of a web server or to run scripts on the Windows desktop. But one of the most common types of vb scripts are found in Microsoft Office and automating the functions within these office applications.
In Windows, it’s common to script using batch files or PowerShell scripts. In Linux or Unix, you would use a shell script. This allows you to automate functions within the Linux or the Unix operating system. The first two characters of a shell script are a hash sign and an exclamation mark. These are referred to as a shebang or a hash-bang. And it signifies that everything after those two characters is a Linux or Unix shell script. Although it’s not necessary to do this in Linux, you can often find that these shell scripts have a .sh extension, so you can easily signify that that is a shell script when looking at a list of file names.
Another popular type of scripting language, especially for IT professionals, is Python. These generally will have a .py file extension. And if you’re looking at a Python script, you’ll see the shebang and then a user bin python3 or similar Python reference in that first line. Python works on many different operating systems. It’s very easy to understand, and it provides a lot of control for the system administrators, so you’ll see Python used for many different tasks across many different operating systems.
As you become more and more familiar with operating systems and administering those operating systems, you’ll become much more familiar with scripting. And depending on the operating system you’re using, you’ll be using different types of scripts. And you should at this point be able to identify a script, understand a little bit of the syntax of the script, and get an understanding of how that script could be used in a particular environment.