Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Created October 7, 2012 20:22
Show Gist options
  • Select an option

  • Save ubermuda/3849459 to your computer and use it in GitHub Desktop.

Select an option

Save ubermuda/3849459 to your computer and use it in GitHub Desktop.
A quick redis consumer in bash (using a custom silex console in this case, but you get the point)
#!/bin/bash
POP_FROM="jobs"
PUSH_TO="jobs_running"
REDIS_CLI=$(which redis-cli)
CONSOLE_DIR=$(cd $(dirname "$0"); pwd)/..;
while job=$($REDIS_CLI brpoplpush $POP_FROM $PUSH_TO 0); do
eval $(echo $CONSOLE_DIR/console $job);
done;
@lyrixx
Copy link

lyrixx commented Oct 7, 2012

eval $(echo ...) ? why not simply use $CONSOLE_DIR/console $job)

@ubermuda
Copy link
Author

ubermuda commented Oct 8, 2012

Good question, thanks for asking! The reason is because of the way bash handles arguments, quoting and parameter expansion.

Say you have a job that has an argument that can contain space, you'll want to describe the job using a string like the following:

foo:bar arg1 "arg2 with spaces"

So from a purely "stringy" point of view, that's what $job would contain. From a bash point of view, $job contains 5 words:

  1. 'foo:bar'
  2. 'arg1'
  3. '"arg2'
  4. 'with'
  5. 'spaces"'

You can check this in your terminal by running set -v and set -x (if you use bash), they tell bash to show you how it expands parameters before executing a command:

$ set -v
$ set -x
$ job='arg1 "arg2 with spaces"'
$ echo $job

(type set +v and set +x to disable them)

And your console call will (most likely) end up failing with a "too many arguments" error message. That's why we eval $job instead of carelessly using it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment