full memcache client in under 255 chars (uses dd, sed and nc)

mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
usage: mem memcache-command [arguments] where memcache-command might be: set add get[s] append prepend replace delete incr decr cas stats verbosity version notes: exptime argument is set to 0 (no expire) flags argument is set to 1 (arbitrary)

5
By: argv
2011-06-17 06:39:07

What Others Think

Alas, I don't know much about memcache, but there's a couple of things I can improve. If you give printf one format specifier, e.g. %s, but several arguments, it will reuse the format: printf "%s " a b c displays "a b c" Next, there's a shell abbreviation, in bash at least, for all the parameters: $@ Quote it to keep any whitespace: printf "%s " "$@" Later in the command you use a local variable $a, but you don't alter it, so you may as well use the original $3 (I think). This shortens your command from 235 to 193 bytes. mem(){ { case $1 in st*|[gid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; } Now, if you give us some sample data for the dd/sed part of the command, we'll see if we can simplify that.
flatcap · 443 weeks ago
thx flatcap. you're right. i've made your suggested changes, and added another of my own. when i look at this my mind says the original arg3 should be "reset(lost)" when passing through a pipe. habitually, i use local vars to prevent this as i'm typing one-liners that use a pipe. but you're right, it's not necessary here. also the last printf isn't necessary if you're using gnu sed.
argv · 443 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: