Saturday, July 25, 2009

File Access Times as Debugging Tools

"Is that configuration file actually being read? I'm sure I changed it. Maybe I'm just not reading the data file I think I am."

"Is my command actually being executed? Is my initialization script getting executed first?"

I use the Linux "file access time" as a debugging tool.

If you want to know the last time a file was written, ls -l tells you the time. You knew that.

If you want to know the last time a file was read (or was run, if it's an executable), ls -ul tells you the time. Maybe you didn't know that.

Or the last time a particular app is getting invoked. The access time tells you when an executable was run because Linux has to read it to execute it.

Or, at least, it used to work this way. Newer Linux systems no longer do, but you can still get the information you need. I'll show you how.

On newer systems, the access time (the time ls -ul reports) is the last time a file was read after the last time it changed. You can get the same information, it just takes a little more setup work. Here's a script that shows how:

#!/bin/bash -x

PS4='$(date +%H:%M) $ '
: == Create a file at a well-defined time.
rm -f /tmp/foo
touch /tmp/foo
: == File write and access times are the same.
ls -l /tmp/foo; ls -ul /tmp/foo
: == Sleep, so the time changes, and read the file
sleep 60; cat /tmp/foo
: == The access time is now later than the write time.
ls -l /tmp/foo; ls -ul /tmp/foo
: == Do it again.
sleep 60; cat /tmp/foo
: == The access time DOES NOT change, because There was no intervening write.
ls -l /tmp/foo; ls -ul /tmp/foo
: == Now "write" the file.
touch /tmp/foo
: == Sleep so the time changes, and read it.
sleep 60; cat /tmp/foo
: == This time, the time does change.
ls -l /tmp/foo; ls -ul /tmp/foo
The meaning of access time was changed as an efficiency hack, so that inodes aren't re-written as often.

In the script, I'm using ':' as a comment character. I should explain why, but I'll leave that as a separate post. Or, you could run the script to see. :-)

Thursday, July 2, 2009

Using Preferred Applications from the CLI

I can use a command-line interface to invoke (not just "set," but "invoke") my GUI preferences.

System-> Preferences -> Preferred Applications lets me set things like my preferred email application and preferred browser. Works fine in the GUI, but how about from the command line?

The solution is XDG tools, from the Portland Project. Browse a url or edit a file?
xdg-open https://google.com
xdg-open /tmp/foo.txt
Email?
xdg-email

Friday, June 26, 2009

To Be Continued

I hate backslashes. They make code harder to read.

In these four code snippets, I like the first two, but not the last two:
sleep 10 & date

sleep 10 &
date

sleep 10 & \
date

sleep 10 \
& date
So? So, for the same reason, I would re-write:
grep :0: /etc/passwd | \
wc -l

as
grep :0: /etc/passwd |
wc -l

and
grep -c jsh /etc/passwd \
|| echo jsh not found

as
grep jsh /etc/passwd ||
echo jsh not found
The symbols ', ", `, &, |, &&, and ||, at the ends of lines, say "I'm not done," and automatically continue to the next line (or, in the case of the quote, until the closing quote).

For shell symbols with parens, like $( and <(, the continuation is until the matching, closing paren.

Thursday, June 25, 2009

Printing Strings in Shell Scripts

echo's nice. printf is nicer.

I still see people use echo. Heck, I still use echo.

But POSIX shells have had printf as a built-in for twenty years. The syntax is like C's printf().

You don't always need fancy formatting, so echo's still good for simple strings, like this:

echo 'What are you -- nuts?'
but there's no reason to use echo -n when you can use printf. None.

Just the reverse. Experiments like this:

time for i in {1..10000}; do printf $i; done > /dev/null
time for i in {1..10000}; do echo -n $i; done > /dev/null
will show you that printf is about 10% faster.

Similarly, you can discard echo -e "foo\tbar" for printf "foo\tbar\n" . Why keep a bunch of flags to echo in your head if you're comfy with printf from other languages?

Wednesday, June 24, 2009

Truncating a File

You can make truncating a file harder than it needs to be.

I'm walking through Hal Pomeranz's older Command-Line Kung Fu postings, both because I know there'll be useful stuff and because he has some fun challenges.

His Episode #19 attacks zeroing out an existing file. The solutions suggested are

cat /dev/null > my_file
and

cp /dev/null my_file
(which is both shorter and works even if noclobber is set, to prevent accidentally overwriting pre-existing files with > ).

Both of these are more typing than necessary. This works just as well:
> my_file
and never even invokes a command. It's all done by the shell.

What's that? You're running with set -o noclobber ?

>| my_file
If you want to do it in fewer characters, you can omit the space. :-)

Cut! It's a wrap.

Tuesday, June 23, 2009

TIMTOWTDI: There Is More Than One Way To Do It

My favorite programming language is still the shell.

Hal Pomeranz, whom I finally met at the OS Bridge conference in Portland last week, has a blog called Command-line Kung Fu full of shell tricks and tips. He posts to it on Tuesdays.

In today's, a post on how to decide what tools to use and when, he recommends cut(1) for some tasks because "cut is the only tool that allows you to easily pull out ranges of characters."

Ah. A challenge.

Well, you can do his example:

grep -l spammer@example.com qf* | cut -c3- | xargs -I'{}' rm qf{} df{}
without cut at all.

grep -l spammer@example.com qf* | while read i; do echo ${i:2}; done | ...
I confess I'd do it differently, but that's because I like substitution better than cuts.

for f in $(grep -l spammer@example.com qf*); do rm $f ${f/q/d}; done
It's pronounced "Tim-toady." Hal emailed me saying he still likes his way better. YMMV.

For jobs like this, where I'm using a program to write commands on-the-fly, I usually play it safe. I start off using echo to see what the shell thinks I want to do

for f in $(grep -l spammer@example.com qf*); do echo rm $f ${f/q/d}; done
which spits out a list of the generated commands.

rm qf137965 df137965
rm qf200920 df200920
...
Then, when I think it's right, instead of removing the echo -- which risks a typo -- I recall the command, and tack on a pipe to a subshell, which executes the commands.

for f in $(grep -l spammer@example.com qf*); do echo rm $f ${f/q/d}; done | bash

Monday, June 22, 2009

Twitter, Again

I'm giving Twitter another shot.

I tried Twitter a while back and couldn't get excited about it. I didn't know why I'd use it, or how.

I just got back from Open Source Bridge 2009, in Portland, OR, where it was all the rage. (The other things most attendees were into were Ruby and git.)

"Okay," I said, "let me find someone who gets it, and I'll just ask how to approach it."

Ms. Amye was willing to be my evangelista, so I logged in, set up my account, and am now following a few folks that I met at the conferences (#osb09 is the Twitter "hash code"). I've even tweeted.

I'm @goyishekop.