ginger technological blog header information panel


Bash scripts and Command Hooks

Published: May 9th 2018

Wow, okay what happened to the last few months? It has been a crazy start to the year and I don't feel like I've had any time to try and investigate new and cool things or smash around with systems and get blogging with what I've been doing.
It's been a busy time with work trying to recruit new people, push forward with updating systems and rolling out services. But one thing I have been doing lately is a lot of smashing around with Bash scripts and finding cool little flags or options in system commands.

Bash Scripts

If you're using Linux or Mac or you work on Linux-based systems, it's inevitable that you will at some stage require some scripts to undertake the work you need. If you've never before created some scripts to do your bidding, you're missing out on some very cool and useful functionality.

I run a helper application on my mac that can run a variety of tasks. From the command line I can run a script with some options and do things like: fire up Chrome windows with specific URL's; launch SSH sessions to specific systems; purge local DNS cache; Run a backup to external drives...

"Wow, that's so lazy!" I hear you moan. Trust me, when you're at 3am and you need to open six browser windows to specific servers on your fleet when you're on call and your eyes aren't quite open - you'll thank me later!

In bash scripts, just as with other languages, you can create functions that can be called - e.g.

#!/bin/bash
#####################
# HELP FUNCTION #
####################

display_help() {
echo "This is a helper script, there are lots of options you can call."
}

Once you build up your functions, you can look for them in passed parameters:
#####################################
# Check for Parameters and Options #
####################################
while :
do
 case "$1" in
  -h | --help)
  display_help
  exit 0
  ;;
 esac
done

Now when you call your script, you can pass in the -h or --help parameter to call your helper function.

Handy Functions

Handy functions that I throw in my local mac script:

####################
# Flush DNS Cache #
###################
flushdns() {
  echo "Flushing DNS..."
  sudo killall -HUP mDNSResponder
  echo " --- / Complete / ---"
  echo
}

#####################
# Start Ops Hangout #
#####################
opsHangout() {
  echo "Starting Hangout..."
  open -a 'Google Chrome' "https://hangouts.google.com/hangouts/"
  echo " --- / Complete / ---"
  echo
}

Just a couple of functions that save me a bit of time given that I live on the command line for most of my day!

Funky Command Options

I recently had a client throw a gargantuan quantity of data into a single directory. This caused a problem for deriving the contents of the folder ( lovely overflow and refusal to output directory listing ).
Now, one of the things that ls does is try to enumerate all the data and then alphabetically sort the output. This can cause it to have major issues with large quantities of files.
To get around this issue, my colleague discovered a forgotten switch that stops ls from doing the ordering task:

ls -f

Super handy - this starts making ls return information straight away instead of trying to run a sort out the output.

I had to move a whole bunch of files from one location to another where there were large quantities of files - so the head command helped out:

mv `ls -f | head -2000` /folder/to/move/to/;

This grabs the first 2000 items and moves them from the directory you're currently in to your destination folder.

Find files in directories recursively and move them to a single directory:

find . -type f -print -exec cp {} /folder/to/move/files/to/singleLevel \;

Useful for when someone has dumped loads of folders with files scattered in them and you want to have them all in one directory for manipulation.

There are probably a whole bunch of other items I've used lately and forgotten how useful they are but figured I'd document some for when I next go "I'm sure there's an easier way to do this" in a month or two's time! ;-)

Happy hacking!
gingerCoder()

Previous Item.. KS2 Computer Club