sawyl: (Default)
Happened across a few new super-cool bash tricks today. Here are my favourites:

magic-space
This causes bash to substitute C shell-type history escapes when you next type a space. Thus, something cryptic like "ls -l !!:2*" changes to something readable like "ls -l foo bar" before you hit the return key. Seriously useful for preventing history goofs.
M-.
Cycles through the last arguments on previous command lines. Very useful if you do stuff like "ls -l foo" followed by "rm foo" a lot.
M-o
Defined as "\C-p\C-a\M-f ", this goes back to the start of the previous history line, skips over the first word and inserts a space. Perfect for adding extra command line options to the last command.

I also thought the idea of replacing Up and Down with history searches sounded interesting, but I'm not sure how much of a overhead it imposes &mdash performance is already close to glacial on the vector nodes, so I don't really want to do anything that might make it worse. Maybe I'll try it out on my desktop box and see how I get on.

sawyl: (Default)
Most of the files I copy around tend to be system specific, so it usually makes more sense to copy them to a local directory like /tmp/$LOGNAME rather than a global directory like $HOME. So far, so good. But what happens if /tmp/$LOGNAME doesn't exist and I attempt to scp to it? A big ugly mess with a potentially sensitive file sitting around on /tmp for anyone to find, not to mention problems with whatever I've got set up in my profile to automatically create my jtmpdir.

My solution to problem? A bit of bash magic. It turns out that bash sources the contents of .bashrc for both interactive non-login shells and non-interactive remote shells, e.g. ssh and scp. This make it possible to customise the remote environment thus:

  stty > /dev/null 2>&1
  if [ $? != 0 ]
  then
    # Remote shell customisation
    export ENVIRONMENT=REMOTE
    export TMPDIR=/tmp/$LOGNAME
    [ ! -d $TMPDIR ] && mkdir -m 0700 $TMPDIR
  else
    # Interactive shell customisation
    [ -f ~/.bash_profile ] && source ~/.bash_profile
  fi

The nice thing about this is that the mkdir is pretty much guaranteed to run before the start of the copy part of an scp session making the whole remote directory existence thing a don't care situation.

sawyl: (Default)
In order to correct a somewhat obscure pet peeve — I hate the way that some mail clients add extra blank citation lines between blocks of text — I've come up with the following particularly unpleasant piece of sed:

s/\(^>\+\) \+$/\1/ /^>\+$/ { h d } /^>\+.\+$/ { H x s/^\n\+//g p s/^.*// h d }

While it appears to work on the relatively few test cases I've tried, I'm pretty sure that there's some unpleasantly wrong with it, something that's only going to become obvious in a couple of months time when I've forgotten how the stupid thing is supposed to do it's magic. Now that I think about it, maybe I don't find those duplicate blank lines all that annoying after all...

sawyl: (Default)
Every few months, usually during our monthly discussions of my work, the HC and I get into a wrangle over programming languages. He usually gets upset whenever I do anything in python, on the grounds that no one else in the group knows python and that it'll be impossible to support if something goes wrong with it. He usually tries to insist that everything should be written in Perl or, God help us, Korn shell for maximum maintainability.

I mean, Korn Shell. I know Korn shell. Korn shell is one of my languages. I've walked its dusty corridors; coaxed it into extreme contortions; whispered sweet nothings in its ear; taken it out to dinner on Valentines Day and sent it flowers on our anniversary, but that doesn't stop me from recognising it for what it is: an appalling, inconsistent, inelegant, flaky, proprietary mess that no-one in their right mind should think of using for a script longer than 10 lines.

Besides, I've seen some of our in house Korn shell and perl scripts and they generally look like they've been gzipped. There's no way I could even begin to support them; not with out spending two weeks picking them apart, a week bug fixing and three weeks in rehab afterwards. My attitude is that it's not the language that matters, it's the code: I suspect that even without a working knowledge of python, my colleagues would be able to fix one of my programmes, whereas I'm not sure they'd be able to say the same about some of the heavily optimised ksh code that we've cranked out over the years...
sawyl: (Default)
We've been playing a geeky game this week: come up with a way of listing the contents of /etc/hosts when the obvious file manipulation tools on your system are missing or broken. The solutions included:

grep ^ /etc/hosts
sed "" /etc/hosts
gzip -c /etc/hosts | gzcat
diff /dev/null /etc/hosts

My own, favoured, solution involves doing away with external tools and using the existing login shell to do the heavy lifting:

while read X; do echo $X; done < /etc/hosts

It's a neat hack because it's entirely self contained and because you're almost guaranteed to have some form of Bourne shell available to you, no matter how shredded the rest of the system is. I mean, if you don't have sh you're not even going to be able to start single user and the whole exercise becomes even more academic than it already is...

sawyl: (Default)
Happened across an interesting skit which described some clever tricks, magic aliases, that make it possible to short circuit shell expansions. The basic idea is as follows:
  • You have an alias which saves parts of the shell context, changes that context, and then passes the alias's arguments to a helper shell function.
  • The helper shell function restores the original shell context which was saved by the alias (either before or after doing something with the arguments it was given).
  • Thus, the arguments passed to the alias undergo variable and wildcard expansion in the altered shell context, and yet the running of the alias as a whole leaves the shell context as it was.

I'm not convinced that magic aliases are useful for anything, but that doesn't undermine the coolness/perversity of the idea...

Profile

sawyl: (Default)
sawyl

August 2018

S M T W T F S
   123 4
5 6 7 8910 11
12131415161718
192021222324 25
262728293031 

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 5th, 2026 12:29 am
Powered by Dreamwidth Studios