Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

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

Terminal - 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; }
2011-06-17 06:39:07
User: argv
Functions: dd printf sed
5
full memcache client in under 255 chars (uses dd, sed and nc)

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)

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

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.

Comment by flatcap 167 weeks and 3 days 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.

Comment by argv 167 weeks and 2 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts