Skip to main content

CLI Tricks: Spongebob Sarcasm in awk

Sometimes you want to turn some text into "sarcastic Spongebob" text so this little fragment of awk will make that transformation for you:

#!/usr/bin/awk -f
BEGIN { srand() }
{
    n = split($0, a, //)
    for (i=1; i<=n; i++) {
        c=a[i]
        printf("%c", rand() < 0.5 ? toupper(c) : tolower(c))
    }
    print ""
}

Read more…

CLI Tricks: Full justify text with awk

I wanted to do some full-justification of text. So a little awk later, I could set the desired width and pipe to justify it.

#!/usr/bin/awk -f
BEGIN {c = "COLUMNS" in ENVIRON ? ENVIRON["COLUMNS"] : 80}
{
    if (NF > 1) {
        s = $0
        wc = split(s, a)
        gsub(FS, "", s)
        totalfill = c - length(s)
        fill = totalfill / (wc - 1)
        intfill = int(fill)
        errdelta = fill - intfill
        err = 0.5
        printf(a[1])
        for (i=2; i<=length(a); i++) {
            width = intfill
            err += errdelta
            if (err >= 1) {
                ++width
                err -= 1
            }
            printf("%*s%s", width, " ", a[i])
        }
        printf("\n")
    } else print
}

Read more…

CLI Tricks: Freeze dance with cmus

Our daughter wanted to "freeze dance" (music plays, stopping at random intervals, at which the dancing kids freeze in place until the music resumes). So what is a geek dad to do? Load up a playlist of kids' music in cmus, start playing the music, and let the shell randomly freeze and resume the music

while true ; do sleep $(( $RANDOM % 15 + 5)) ; cmus-remote -u ; sleep 4; cmus-remote -u ;  done

This does as sleep for some random interval between 5 and 20 seconds, then does as sleep for 4 seconds before unpausing the music.

You could do something similar with mpd/mpc if you prefer them.

CLI Tricks: reboot/shutdown mollyguard function

Over on Twitter I was having a discussion about creating a mollyguard to protect yourself from accidentally rebooting the wrong machine by requiring you to type the hostname of the machine you wanted to reboot. If you need such, you can add these functions to your ~/.bashrc or ~/.profile to create such a reboot function. Note: The function shown on Twitter had the logic reversed so make sure you get the "=" and "!=" correct. If $1 equals the hostname, you want to proceed with the shutdown; if they are not equal you want a warning.

reboot() { [ "x$1" = "x$(hostname)" ] && sudo /sbin/shutdown -r now || echo "Failed to specify $(hostname)" ; }
halt() { [ "x$1" = "x$(hostname)" ] && sudo /sbin/shutdown -p now || echo "Failed to specify $(hostname)" ; }

If you run on OpenBSD you can specify doas instead of sudo.

CLI Tricks: Find the Nth word of a text file

While I'm not sure when others would use this I wanted to find the Nth word of a text file so I used this to find the, say, 318th word of the document:

tr -cs "a-zA-Z'" '\012' < document.txt | sed -n '318{p;q;}'

Yes, it can get tripped up by non-Latin characters, hyphenated words, or other edge-cases, but for my 7-bit ASCII input text, it did the job.

CLI Tricks: diff files in git and then add them

Occasionally I want to diff one or more files in git and, if everything looks good, then add them. To do so, I often take advantage of bash's ^ notation to do a replacement:

git diff -- file1.txt file2.txt
^diff^add
git commit -m "Looks good to me"

The second command replaces diff with add turning the command into git add -- file1.txt file2.txt without having to retype the entire command.

CLI Tricks: Compare directory sizes when hard-linking

I have directories with lots of image files (or mail files), some of which I hard-link into other directories to save space or make sure they're in sync. How much space am I saving by hard-linking?

(du -sl ; printf '-\n' ; du -s) | awk '{print $1}' | paste -s - | bc

With a little math, most of it can happen in one awk statement:

(du -sl ; du -s) | awk '{t+=(-NR*2+3)*$1} END{print t}'

Aggressive pf(4) configuration for SSH protection

If some unknown person came up to your house and started trying to open each window, jiggling each door handle, and punching in random codes on your garage-door opener, you could safely assume that they sought trouble and that you should stop them. Similarly, if some unknown computer on the internet started probing at ports on your server where you offer no services, it should set off alarm-bells and you should prevent this miscreant from interacting with your server. Especially SSH access. You know what services your machine provides. If anybody attempts to connect to some other service on your machine, in all likelihood they don't have your best interests in mind.

In this post, we configure pf on OpenBSD to catch inappropriate connection-attempts and block the offender.

Read more…