One of the reasons we (I use we, I mean after all you are reading a post on shell scripting) love Linux is because of the ease of automation. A big part of that is using BASH (Bourne Again Shell) to script some common tasks.
Start off your script defining the interpreter, what everyone calls the shebang line.
As an example we’ll make a variable and do a for each loop through them.
teams=”Broncos Avalanche Rockies”
for each in $teams
Now how do you run that? You could run bash ./myScript.sh , but that is 5 extra characters you don’t need to type. You will chmod it.
chmod 755 myscript.sh
Then you would run it with ./myScript.sh
joes-MacBook-Pro:~ joe$ ./myscript.shBroncosAvalancheRockiesls | while read linedoecho $linedone
There are also while loops, which work well if you are looping through a list of files that may have spaces in the name.
Bash has if statements, it’s just a little unique. It’s unique in that spacing is very important and [ ] if else are all commands that are invoked. Let’s look at an example
starTrekTheNextGen=2starTrekTheOriginal=1if [ $starTrekTheNextGen -gt starTrekTheOriginal ]thenecho “Piccard Forever”elseecho “KAAAAAAHHHHHHN”fi
-gt: greater than
-lt less than
-ge/le/: greater/lesser than or equals
-n: not null
You see a -gt for greater than. There are a number of test conditions for bash, but here are the ones I use most:
Even though I love Python, it’s lack of a switch statement bothers me. But fear not, Bash has switch statements. The are opened with case and closed with esac. A wildcard can be used to with an asterisk.
#!/bin/bashif [ -z $1 ]then echo "ERROR: Please provide an argument" exitficase $1 in "tos") echo "The Original Series" ;; "tng") echo "The Next Generation" ;; "ds9") echo "Deep Space Nine" ;; *) echo "I haven't watched that yet" ;;esac
That is just a primer on shell scripting. If you want to learn more, track down the book Linux and Unix Shell Programming by David Tansley. Thanks for reading and come back every Friday for a new Linux hack!
Facing growing threats and a rapidly expanding attack surface, understaffed and alert-fatigued organizations need more efficient ways to eliminate their exposure to vulnerabilities. Automox is a modern cyber hygiene platform that closes the aperture of attack by more than 80% with just half the effort of traditional solutions.
Cloud-native and globally available, Automox enforces OS & third-party patch management, security configurations, and custom scripting across Windows, Mac, and Linux from a single intuitive console. IT and SecOps can quickly gain control and share visibility of on-prem, remote and virtual endpoints without the need to deploy costly infrastructure.
Experience modern, cloud-native patch management today with a 15-day free trial of Automox and start recapturing more than half the time you're currently spending on managing your attack surface. Automox dramatically reduces corporate risk while raising operational efficiency to deliver best-in-class security outcomes, faster and with fewer resources.