2010/09/30

Bash and dialog

First, I rant about dialog: WHAT THE HELL WERE THEY ON WHEN THEY WROTE THIS THING?! First, they make keyboard inteaction completely different from what normal users would expect. And then they make it very very hard to actually get the results into a shell script. What's more, every time I write a new script that uses dialog, I realise it lacks many features I need. So I have my own forked / hacked version that I deploy. Thank God for Open Source Code.

Second, I give you some code snippets that will show you how to do simple things with dialog:

Say you want a menulist and want to know which one was selected:
resp="$( dialog ..... 2>&1 >/dev/tty)"

Here we assign a value to the variable resp. The "" preserves whitespace. The $() causes the value to come from a sub command. dialog ... is the "normal" dialog invocation. 2>&1 causes stderr to be sent to sdtout, so that it ends up on resp. >/dev/tty causes dialog's stdout (its original stdout, that is, not the bits of stdout that are coming from stderr) to go straight to the controling TTY, rather then ending up in resp, which would be silly.

Now, say you had a shell function call do_something. This function sets serveral variables as a side effect and takes a while doing so. Maybe you'd want to use dialog's --progressbox or --gauge so the user has something nice to look at while work is going on. If so you might try
do_something | dialog --progressbox

And you would quickly encounter failure; do_something ends up in a sub-shell, whence it can not set the variables in the main shell.

So, you spend time with the BashFAQ and on #bash complaining trying to find an answer. It turns out that FIFOs (aka named pipes) are the only way:
fifo=$(mktemp -u)

mkfifo $fifo
trap "rm -f $fifo" EXIT
dialog --progressbox "Wait" 12 40 <$fifo &
pid=$!
do_something > $fifo
rm -f $fifo
wait $pid

So, we create a randomly named FIFO. Run dialog in the background, reading from the FIFO. Run our function in the foreground, stdout going to the FIFO. When do_something is done, we remove the FIFO and wait for dialog to exit. Which it should, after the fifo has been removed. The trap makes sure the FIFO is deleted even if we exit early.

If you ask me, there's got be a better way, one that doesn't involve mkfifo and does involve exec. But I can't get my head around exec.

EDIT: turns out there is a better way:
do_something > >(dialog .....)

A mixed bag

I ordered a BlacX SATA/IDE/USB docking station today. Should have bought one back when I first discovered them. Would have made all the farting around with Corey a lot less painful.

In other news, I discovered that getting xterm to use TrueType fonts is as simple as -fa NAME-PT. As in:
xterm -r -fa Consolas-17

Consolas, if you didn't know, is a nice little monospaced font that MS commissioned, tuned to LCD screens. The previous link downloads a bloody useless setup.exe. So I downloaded the .ttf from elsewhere.

Also, you might be interested in this review of programming fonts from 2007. Though he seems to think that 11pt is legible.

Finally, a snippet to get the current X resolution. I needed this to adjust the font size based on resolution. My users want the terminal to fill the screen.
read prop Xsize Ysize < <(xprop -notype -root 32cc ' $0 $1\n' _NET_DESKTOP_GEOMETRY)

echo "${DISPLAY:-Display} is ($Xsize,$Ysize)"

If you didn't know about Process Substitution you do now.

2010/09/28

LTSP and x11vnc

A few years back, I wrote a system that ended up replacing about 200 terminals in 5 different offices with LTSP running on VMs. 2 and bit years latter, the last bunch of users were moved from terminals to LTSP. This time, they got FIT-PC2s as opposed to diskless PCs. Lucky them; less noise. But they are complaining that they find the mouse to unwieldy. And because they have locked down desktops, they don't have a config app to tweak the mouse acceleration and threshold. So I poked around and found that the settings are in :
/home/$USER/.config/xfce4/mcs_settings/mouse.xml

Now, that is hardly exciting.

What is exciting: I then wanted to set up x11vnc so that I could see what the users were experiencing. I've used x11vnc often on the same desktop, but my first attempt at doing it with LTSP failed. So I go on #LTSP to complainlook for a solution, which turned out to be -noshm.

They first complained that LTSP-4 (which I'm using) is far to archaic (which it is). They then got me on track for the next little bit of bash:
#!/bin/bash


USER=$1

error () { echo "$*" >&2; exit 5 }

if [[ ! $USER ]] ; then
error "Usage: $0 user"
fi

userpid="$(pgrep -u $USER xfwm4)"
if [[ ! $userpid ]] ; then
error "User $USER isn't logged in"
elif [[ $userpid =~ ' ' ]] ; then
error "User $USER is probably logged on several times: $userpid"
fi

userenv="$(tr '\0' '\n' < /proc/$userpid/environ | egrep '^DISPLAY=|^XAUTHORITY=')"

eval "$userenv"
if [[ ! $XAUTHORITY ]] ; then
XAUTHORITY=/home/$USER/.Xauthority
fi
export DISPLAY XAUTHORITY

echo DISPLAY=$DISPLAY
echo XAUTHORITY=$XAUTHORITY

exec x11vnc -noshm -nopw

All the error checking might be hiding the really interesting bit: I'm pulling DISPLAY and XAUTHORITY from the user's environment, via /proc. Something I'd never thought of doing before.

2010/09/24

Tools

This is a short list of commands and idioms that make life at the command prompt bearable. Some of them are trivial, but if you don't know they are there, you'll never find them. Reading their man pages is recommended.
screen

perl -lane 'short script'
find [....] -print0 | xargs -0 cmd
( set -e ; cmd1; cmd2; cmd3 ) && killall some-daemon
pstree
ps -C cmd
lsof -c cmd
ssh-keygen
pidof
chkconfig
$(( arithmetic ))
${var/match/replace}
lshw
for n in [...] ; do cmd $n ; done
some-command | grep | while read n ; do cmd $n ; done

lshw combines lspci, lsusb, lshal, lspcmcia and more. Forgot what motherboard a server has? lshw knows. What size/manufacture/number of DIMMs you installed? lshw knows. What version of BIOS? lshw might know. Under CentOS you have to get it from RPMForge.

One should learn variable expansion syntax if one spend any time with bash.

And yes, I do type while and for loops at the command line.

2010/09/23

Silence!

Newegg.ca sent me email yesterday with a tracking number. Click on it and BUH "we have no information about this package." Yes, you are idiots.

But this morning, I try again at 8h00. And BUH PACKAGE ON TRUCK IN SHERBROOKE!

So I don't go back to bed, wait impatiently for the truck to show up. Three cheers for eCommerce!

2010/09/21

I give up

So concluded on how to get partitions aligned on 4k boundaries (WDC WD15EARS-00Z), for ~60 MB/s. But if I add a ext3 FS on top, speed drops to 20 MB/s. With LVM or without.

2010/09/18

This makes no sense

The great big huge 1.5 TB drive I bought for Corey is a WD15EARS. One thing all those letters mean is that it's an Advanced Format drive. Which is Western Digital Marketing Speak for "we use the new, modern 4k sector size." But of course the drive reports its block as 512 bytes, because otherwise it would fail under Windows. So hoops must be jumped through.

And some of them make no sense.

2010/09/17

The parable of the ax.

You know the old parable : I've got this great ax. Had it for years. I've changed the handle 5-6 times. Only changed the head twice though. Still works great!

This also applies to computers. Corey (my desktop computer WAKE UP WAKE UP! DARLING COREY! HOW CAN YOU SLEEP SO SOUND?) started life as a Compaq Presario running WinXP. I never used the original mouse or keyboard. The heatsink clips broke, so I had to replacethe mobo, CPU and RAM. I've replaced the CD-R drive with a DVD-R. And now I've replaced the hard drive with one 100 times bigger and upgraded the OS from Fedora 5 to CentOS 5.5.

Basically all that's left of the original computer is the case and power supply.

So, do I change the computer's name? My feeling is "yes." Mainly because an OS upgrade is a large change in a computer's personality. But on the other hand, changing the might mean going through a bunch of forgotten config files and changing it here and there too. Mind you, the important ones prolly just use the IP.

But on the gripping hand, I'm lazy. And I'll get to tell the Ax Parable during cocktail parties or other inapropriate moments.

2010/09/15

SSDs are not ready

The drive on Corey is/was in the process of dieing. It is going on 7 years old, has been spinning at 7200rpm every hour of most days for those 7 years. I don't begrudge it failing. It did mean I had to spend 2-3 days reconfiguring my daily use computer. Annoying. More details later.

I bought a Western Digital Green 1.5TB drive. 100 CAD! Cheap! It's only 5400rpm in the hopes it will make less noise. But it's still to noisy. Not so much rotation whine but an annoying click every 4-5 seconds. Something I had back in the day with SCSI drives, but I don't think the old IDE drive had this problem. So either I get an SSD or a fit-pc2 to put it in. Or boot LTSP from the basement.

Also, Jean-Phillipp brought up that maybe we should look into moving our clients' MySQL DBs to SSD. Even at the high prices of SSD, it would cost less then what we are currently doing, which is throwing RAM at the problem. Also, there's only so much RAM that a server can hold. And with datasets getting larger then 33 GB, that approach has reached its limit. However, it turns out that SSD, along with being expensive aren't quite ready for prime-time.

The name

This blog's name, User Tolerant Liveware, comes from Doonsebury, by way of Daniel.  Way back in the day, there was a Doonesbury cartoon with "Ah, the User Friendly Liveware" as the punch-line.  Years ago, Susan refered to me as such to Daniel.  He harumpfed and responded to the effect that I was closer to "User Tolerant" then "Friendly."

Can't say I disagree.

Anyway, this is where I plan to complain about computers, so many posts will be technical.  It also gives me a blogger account so that I can contradict Daniel.