Automating the efficiency of duties through scripting is one thing all of us attempt to do as IT execs. However taking it a step additional through the use of grep provides a layer of granularity and universality to your scripts.
Interacting with knowledge is the core operate of any IT skilled. Every position is exclusive and brings with it particular challenges, however on the coronary heart of every position lies the identical fundamental premise: As IT, we interpret and reply to the information we obtain and if it is not correct, we take measures to right the information movement. Once we automate duties based mostly on scripts, it’s sometimes completed in response to identified parameters and supplies a predetermined set of instructions to execute, thereby automating repetitive duties.
SEE: TechRepublic Premium editorial calendar: IT insurance policies, checklists, toolkits, and analysis for obtain (TechRepublic Premium)
That is nice and, I whole-heartedly advocate that IT execs automate the place potential to maximise efficiency and restrict downtime for all stakeholders. However what in case you may execute instructions or write scripts that would offer info and make adjustments (execute instructions) based mostly on the responses? You could possibly automate duties with a little bit of logic baked in to permit for the script to account for quite a lot of potentialities restricted solely by your creativeness or script writing capabilities.
Scripts like these could also be discovered in lots of fashionable boards or GitHub, and so long as they’re examined and verified to work on your setting, it is best to positively implement them the place possible. However in case you’re extra of the do-it-yourself sort, want to be taught this hands-on, or can not discover a appropriate, pre-authored resolution, could I introduce the grep command to you?
Grep is natively present in Linux and macOS programs. It’s also out there as an installable package deal for Home windows.
What’s grep?
In accordance with the grep man web page in macOS, “The grep utility searches any given enter recordsdata, choosing strains that match a number of patterns.” Because of this grep primarily searches for knowledge that matches a selected set of phrases or patterns that you just inform it to search for. The enter for grep to go looking could come from recordsdata fed to the utility or is extra generally used at the side of the output of instructions which are piped into grep after execution to establish sure bits of information.
Why ought to I take advantage of grep?
In addition to reducing down, maybe drastically, on manually looking recordsdata or command outputs for particular knowledge or responses, as talked about earlier than, grep may be included in a series of instructions with the output of a earlier command piped into the enter for grep to sift via. This permits for the output from grep to be piped right into a subsequent command to execute one other command towards, and so forth. When mixed with different instructions inside a script, grep could also be used to learn info from one file to find out if the command ought to proceed in a single path or one other, reducing down on the variety of scripts to take care of (and replace).
SEE: Navigating knowledge privateness (free PDF) (TechRepublic)
What are some examples of grep in motion?
Beneath I will present some real-world examples of grep in use inside scripts to ascertain an understanding of how grep works and spotlight how effectively it performs with different instructions. It is as much as you to see how it may be built-in so as to add common performance to your scripts.
1. Confirm hash worth for an replace earlier than set up
Within the instance under, we have now a file known as “macOS_Update01.pkg” that we want to set up however wish to confirm the hash worth to verify the file’s integrity is undamaged. We all know the hash worth is “9aac9b799f3bb26da66f886024e1af58a1b4d3a7” and as is widespread observe, we have now a file named “hash.sha” with the SHA1 worth saved in it. By operating the grep command under with the identified hash worth saved within the checksum file, the string will carry out a verify on the values, evaluating them. If it matches, the Terminal will output the filename on-screen to verify integrity.
grep 9aac9b799f3bb26da66f886024e1af58a1b4d3a7 hash.sha
2. Decide the software program model of macOS and export non-matched gadgets
On this instance, we’re operating a command to find out when the present operating model of macOS is put in on a bunch of gadgets. Those that don’t match 11.1 can be exported to an inventory titled NeedsUpdating.csv for assessment. The output of the primary command can be piped into grep to whittle down the information that matches the requirement.
sw_vers -productVersion | grep -v 11.1 > "NeedsUpdating.csv"