Created
October 7, 2012 20:22
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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; |
Author
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:
'foo:bar''arg1''"arg2''with''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
eval $(echo ...)? why not simply use$CONSOLE_DIR/console $job)