5

I am a mac user and a web developer and I would like to create automated tools to manage my workflow. Say a single command to start up a type of website.

What parts of bash shell scripting should I learn? There are so many:

  • sed
  • awk
  • grep
  • find

Or should I be learning something else to archive command line automation for my web projects?

2
  • 1
    These are the ones I use most often, plus the fle modification commands (rm, mv, cp, touch, mkdir).
    – Daniel Beck
    Nov 26, 2011 at 16:22
  • 1
    For an extensive list of guides, check out @Lri's excellent answer here.
    – Daniel Beck
    Nov 27, 2011 at 14:43

3 Answers 3

11

You need to be aware that different from other scripting or programming languages, shell scripting heavily relies on the command-line applications provided to you by the OS. This means, since OS X is a BSD, that much of the things that work on Linux, will work slightly different on your computer, rendering a lot of tutorials useless. In general, you can expect your command line tools to have fewer features and flexibility than the Linux counterparts, although there are exceptions.

if you're unfamiliar with the shell and don't feel an overwhelming need to learn it, you might be better off using your favorite web scripting language (PHP, Ruby, Python) from the command line. The advantage there is the probably more consistent API across the entire library of functions.


In bash itself, learn

  • Conditions (including use of test / [...] for defensive programming when handling files)
  • Loops
  • Variable assignment
  • Functions
  • Input and output redirection and piping
  • Calling programs and shell functions (including how to capture their output and read their return values), both regular calls and with command substitution.
  • Options. I love set -u and refuse to work with scripts that cannot handle this.
  • Variable types, arrays, globbing

The problem here is that bash is very tied into the system with things like job control, and, for creation of complex scripts, has a rather steep learning curve.

In my experience *no section of the bash man page is unnecessary. Do it long enough and everything in there is applicable somehow (and if it's just to answer questions on SU).


Some (in my experience) commonly used programs in shell scripts:

  • File handling and navigation: find, ls, cd, rm, mv, cp, chmod, chown, chflags, dd, ln
  • Program execution: xargs, find -exec
  • Data transformation: sort, wc, cat, sed, awk, grep

Specific to OS X are probably the following:

launchctl, open, dscl, PlistBuddy, plutil, osascript, automator, networksetup, systemsetup, growlnotify (if you have Growl)

And of course, depending on the exact nature of what you want to do, there are others...

date (BSD variant — very different from GNU), mount, fsck, kill, sendmail


Some, e.g. netcat require you to install third party packages e.g. using Homebrew, MacPorts, or Fink.

If your scripts heavily use these command line programs, or can be written in such a way, bash scripts are a good way to automate this.


Since you're on OS X, also look into AppleScript (via AppleScript Editor) and Automator. Both of these can be combined with shell scripts to achieve quite nicely integrated script solutions. If you're looking into controlling other OS X applications (e.g. your web browser), these are indispensable. While you can open URLs in different browsers using open on the command line, retrieving data e.g. from loaded tabs requires Automator or AppleScript.

Also, other scripting languages such as ruby, python are also pre-installed on your Mac and can be used to easily create simple helper utilities. These are more of a replacement of bash and regular UNIX utilities. It depends on the nature of what you want to do.

If you want to move a handful of files around your disk, zip them up and transfer them via SSH, bash is probably the best choice. As soon as you have real programming logic (anything involving a lot of objects or numbers), you should probably look into other scripting languages.

2
  • +1: "... you might be better off using your favorite web scripting language (PHP, Ruby, Python) from the command line. . ." IMO, this probably makes more sense and provides a more general skill set in most people's situations.
    – surfasb
    Nov 27, 2011 at 4:34
  • @surfasb it depends on the nature of your scripts. If you copy files around and then transfer them via scp to a different machine, bash is probably easier to do this with. As soon as you read binary files or do any kind of computation, bash stops being fun.
    – Daniel Beck
    Nov 27, 2011 at 7:03
1

I've posted a list of (OS X and bash centric) bookmarks at OS X Terminal Tutorials - Super User.

One of the best ways to learn a scripting language is by modifying existing scripts. Take a look at stuff like:

A few books with freely downloadable code examples:

0

I think for you, just Basic usage of about 20-25 commands is enough, and basic architecture of Linux is must. Depending upon your work type,you can start learning about these bash commands only:

vi,ls,find,sed,awk,grep,cat, service startup and shutdown, netstat,nmap,traceroute,ping,mv,cp,rm etc.

These are most common commands, which are used in bash.

Besides that, you can follow this tutorial:

http://www.faqs.org/docs/bashman/bashref_toc.html

http://blog.emson.co.uk/2009/06/18-useful-bash-scripts-for-web-developers/

Also, you need to learn basic shell scripting to automate some processes.

4
  • There is no service program on OS X.
    – Daniel Beck
    Nov 26, 2011 at 16:23
  • 2
    How does the statement "you need to learn basic shell scripting" to the question "How do I learn basic shell scripting" answer his question in the least? Providing a list of commands with no explanations links to tutorials is not explaining anything.
    – MaQleod
    Nov 26, 2011 at 18:43
  • @MaQleod well, I wouldn't fault him for not giving links to learn the commands he mentioned.. he can search any of them.
    – barlop
    Nov 27, 2011 at 15:04
  • It's fine @MaQleod. The answers are helping though yes some cool practical examples would definitely be great. Nov 27, 2011 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .