Bash Scripting – Getting Started

Bash Scripting: The Basics

Over the last two years or so I’ve been lucky enough to be involved in a number of projects that required a knowledge and understanding of how to script on a Mac. There are a number of scripting languages you can use on Mac OS X, the most popular being Applescript and Bash (or ‘Shell’) scripting. Each of these have there own pros and cons for using them, including but not limited to the complexity to write scripts and features available to each language.

I won’t delve into these reasons, to be honest; I’ve only really worked with the one scripting language and if you’re here, you probably searched for ‘Bash scripting’ anyway?

Understanding the concepts

So to start off, I’m going to assume you know the ideas behind scripting and a general idea of the concepts. If not, it’s simply a case of using command line-style tools to extract information (e.g. ‘less’ and ‘grep’), using questions to figure out what to do with the information (e.g. ‘if’, ‘else’, ‘then’), manipulating the information (e.g. ‘sed’, ‘tr’) and using that information (e.g. ‘echo’, ‘mount’). Once you understand the idea of both what you want to do and how bash needs you to ask it, then the rest is playing with different commands!

Confused? Ok, let me give you an example.

Say you want to check how a user logging in is, and if they’re a certain specific user, you want to mount/map a network volume/drive, but only for that user. With scripting you would ‘ask’ if the user that is just logged in matches ‘User A’. If not, you’d tell the script not to continue. If so, you’d tell it to mount the volume and go through the steps this entails.

Getting started

Right, lets run through some essential points and good to know hints (almost all of these can also apply to Unix scripting, however if you’re running Unix you’re probably beyond this):

  • To be auto detected by Mac OS X, the script will need to be saved with the .sh extension. Much like a Word document is .doc (or .docx) and so informs the Finder that this is a Word document, files ending in .sh tell Finder ‘this is a script, run me in Terminal’.
  • To run, the script must be told what ‘shell’ to run in. If you are unsure stick with the default ‘/bin/sh’. This is set by the first line of the script. For example, to set the script to run in the above ‘shell’ have “#!/bin/sh” as your first line.

 

By the way, “do bash stuff here” is not a command, in case you were unsure!

  • To add a comment line, use the hash symbol (‘#’). An important thing about scripting is to add comments! If you or a colleague come back to a script a year later, it can be extremely difficult to decipher, especially as they get more complex. The hash (or ‘pound’ if you’re American) symbol tells the script that the rest of that line is a comment and not to do anything with it. It can be used after a command, or to temporarily ‘remove’ a command when testing.
How to use bash scripting command line
  • Never have the script make an automated decision to delete data. Imagine a typical day in your life. Think of all the tiny little mistakes you could (and occasionally do) make throughout the day. A trip here, a sneeze there, an accidental keyboard press. Now imagine if you made that one tiny mistake in the middle of a script.

Just to be on the safe side, avoid having the script delete data based on a decision made by itself. To quote my boss, “That’s how Skynet started”

  • Lastly, a recommendation. Get yourself a script editor that colour codes things and you feel comfortable using. Using Word is a definite no. Use TextEdit only if you are certain its in plain text format (Lion’s TextEdit has a habit of messing up scripts, even in plain text mode. Personally I recommend Fraise – but it’s no longer developed and can be hard to get. Another recommendation is Text Wrangler. Have a look around.

TextEdit:

Bash Scripting Text Edit

 

Fraise:

 

Bach Scripting Fraise

 

Text Wrangler

Bash Scripting Text Wrangler

 

Summary

That’s it for this week. As you can see, it is a little bit different to my usual, but it may well become a regular thing!
What are your thoughts? Do you agree with me? Do you have some of your own ‘rules for scripting’ to share? Go for it in the comments below and I’ll try to respond to as many as I can.

1 Reply to "Bash Scripting – Getting Started"

Leave a Reply

Your email address will not be published. Required fields are marked *