LJ Archive

UpFront

Tech Tip

Venky TV

Issue #165, January 2008

Removing Duplicate Lines in Unsorted Text without Losing Input Order

Here's the problem. We need to remove duplicate lines from unsorted text from within the shell. This normally would be the job of sort -u or sort | uniq, except that in either case, we lose the input order. For example, if this is the input file:


$ cat >/tmp/numbers <<EOF
one
two
three
one
three
four
EOF

Running it through sort -u would get this:

$ sort -u /tmp/numbers
four
one
three
two

The solution:

$ nl /tmp/numbers | sort -k2 -u | sort -n | cut -f2-
one
two
three
four

For platforms where the nl command is not available, awk could be used to simulate this behavior:

$ awk -v 'OFS=\t' '{print NR, $0}' /tmp/numbers | sort -k2 -u 
 ↪| sort -n | cut -f2-
one
two
three
four

What it does is pretty simple—it adds a record number field, sorts the input ignoring that field, restores the original order using the record numbers and then strips that field out.

Tech Tip

Max Luebbe

Issue #165, January 2008

Tweaking Forced Hard Disk Checks

There is nothing worse than when you boot up your Linux machine to show Windows users a neat feature, such as your slick 3-D Compiz desktop, and while booting, you get the message that one of your partitions needs to be force-checked because it hasn't been checked in 23 boots. So while e2fsck trudges through that giant partition scanning for errors, the people you were attempting to convert are rolling their eyes and trying to contain their laughter about your so-called superior OS.

You can tweak the conditions under which the filesystem check will run at boot time using the command tune2fs. Using a -c option allows you to tweak the number of boots that will trigger a forced check, and using -i allows you to change the time interval between checks in the format: [days|weeks|months].

If you want to initiate disk checks manually only (a risky idea if you forget—proceed at your own risk!), use this variant of the command:

tune2fs -c 0 - i 0

Tech Tip

Suman Chakravartula

Issue #165, January 2008

Cool SSH Automation Trick

Do you need to give passwordless SSH access to users but also need to restrict what can be run? Here I give interactive and non-interactive examples.

First, you need to generate authentication keys (no passphrase) using ssh-keygen. In this tip, I generated rsa keys. The keys, by default, are saved as ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

You need to append the public key as follows:

suman@shri:~/.ssh$ cat id_rsa.pub >> authorized_keys

Prepend the command (that you want to be executed automatically) to the signature. For example, if uptime is the command to be executed automatically whenever an SSH request comes from the user for which you generated the key, the key entry in authorized_keys file should look like this:


command="/usr/bin/uptime" ssh-rsa <LONG ENCRYPTED STRING>

Now, you can use the private key to execute the uptime command automatically. The private key in this example, by default, was saved as ~/.ssh/id_rsa. I copied it to the remote host and saved it as uptime.key. Make sure this file has 600 permissions. Then, you can do the following (from the remote machine):

suman@strangeloop:~ % ssh -T -i uptime.key suman@shri
 15:11:46 up 4 days, 3 min,  3 users,  load average: 0.00, 0.00, 0.00

This technique also can be used for interactive programs. Below is a simple interactive shell script:

#!/bin/sh
echo -n "Hi! Enter your fav distro: "
read DISTRO
echo "Your fav distro is $DISTRO"

Here I have created another set of keys as I did above. I saved the private key as distro.key. Prepend command="<full_path_to_script>" to the public key in the authorized_keys file, and you will be able to do:

suman@strangeloop:~ % ssh -T -i distro.key suman@shri
Hi! Enter your fav distro: Debian
Your fav distro is Debian

LJ Archive