What if you could automate tasks with a bit of logic to allow for the script to account for a variety of possibilities ?..
Automating the performance of tasks via scripting is something we all strive to do as IT pros. But taking it a step further by using grep adds a layer of granularity and universality to your scripts.
Interacting with data is the core function of any IT professional. Each role is unique and brings with it specific challenges, but at the heart of each role lies the same basic premise: As IT, we interpret and respond to the data we receive and if it isn’t accurate, we take measures to correct the data flow. When we automate tasks based on scripts, it is typically done in response to known parameters and provides a predetermined set of commands to execute, thereby automating repetitive tasks.
This is great and, I whole-heartedly recommend that IT pros automate where possible to maximize performance and limit downtime for all stakeholders. But what if you could execute commands or write scripts that would provide information and make changes (execute commands) based on the responses? You could automate tasks with a bit of logic baked in to allow for the script to account for a variety of possibilities limited only by your imagination or script writing capabilities.
Scripts like these may be found in many popular forums or GitHub, and as long as they are tested and verified to work for your environment, you should definitely implement them where feasible. But if you’re more of the do-it-yourself type, wish to learn this hands-on, or cannot find a suitable, pre-authored solution, may I introduce the grep command to you?
Grep is natively found in Linux and macOS systems. It is also available as an installable package for Windows.
What is grep?
According to the grep man page in macOS, “The grep utility searches any given input files, selecting lines that match one or more patterns.” This means that grep essentially searches for data that matches a specific set of words or patterns that you tell it to look for. The input for grep to search may come from files fed to the utility or is more commonly used in conjunction with the output of commands that are piped into grep after execution to identify certain bits of data.
Why should I use grep?
Besides cutting down, perhaps drastically, on manually searching files or command outputs for specific data or responses, as mentioned before, grep can be included in a chain of commands with the output of a previous command piped into the input for grep to sift through. This allows for the output from grep to be piped into a subsequent command to execute another command against, and so on. When combined with other commands within a script, grep may be used to read information from one file to determine if the command should proceed in one direction or another, cutting down on the number of scripts to maintain (and update).
What are some examples of grep in action?
Below I’ll provide some real-world examples of grep in use within scripts to establish an understanding of how grep works and highlight how well it plays with other commands. It’s up to you to see how it can be integrated to add universal functionality to your scripts.
1. Verify hash value for an update before installation
In the example below, we have a file called “macOS_Update01.pkg” that we wish to install but want to verify the hash value to make sure the file’s integrity is intact. We know the hash value is “9aac9b799f3bb26da66f886024e1af58a1b4d3a7” and as is common practice, we have a file named “hash.sha” with the SHA1 value stored in it. By running the grep command below with the known hash value stored in the checksum file, the string will perform a check on the values, comparing them. If it matches, the Terminal will output the filename on-screen to confirm integrity.
grep 9aac9b799f3bb26da66f886024e1af58a1b4d3a7 hash.sha
2. Determine the software version of macOS and export non-matched devices
In this example, we are running a command to determine when the current running version of macOS is installed on a group of devices. The ones that do not match 11.1 will be exported to a list titled NeedsUpdating.csv for review. The output of the first command will be piped into grep to whittle down the info that matches the requirement.
sw_vers -productVersion | grep -v 11.1 > "NeedsUpdating.csv"