Shell sins

Shell sins

You won't go to hell for these, but you should probably look at the manpage. Some programs are just limited, but hey, you can't expect every program to provide a printf function, a date formatter and handling of input from stdin.

Undeclarative grep

grep is a true power tool in Unix, but can be abused. Here is an example (by me 😮):

date --date="$(stat "$4/${i%\.html}.md" | grep Modify | cut -d':' -f2-

Poor old skuzzymiglet then had no idea about stat --printf, which has useful options for printing times:

%w     time of file birth, human-readable; - if unknown
%W     time of file birth, seconds since Epoch; 0 if unknown
%x     time of last access, human-readable
%X     time of last access, seconds since Epoch
%y     time of last data modification, human-readable
%Y     time of last data modification, seconds since Epoch
%z     time of last status change, human-readable
%Z     time of last status change, seconds since Epoch

The human-readable dates are fixed-format, so you can just date "your_cool_format" --date="@$(stat --printf="%Y" whatever_you_want)"

Generally, a printf like option is useful in programs - it discourages scraping and greatly shortens pipe sequences. But alas, most programs will need to be grepped.

Functions

It sounds weird but I believe functions are a shell code smell. The problem is that with a function, you expect paramater names (and types if you're a reasonable person 😀). However, you'll find it's just like command-line arguments, and if you don't have the time to use getopt(1), you'll be addressing by index.

Or you could just reassign to variables:

(from ssg5)

render_sitemap() {
    urls="$1"
    base_url="$2"
    date="$3"

    echo '<?xml version="1.0" encoding="UTF-8"?>'
    echo '<urlset'
    echo 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
    echo 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'
    echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"'
    echo 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
    echo "$urls" |
        sed -E 's#^(.*)$#<url><loc>'"$base_url"'/\1</loc><lastmod>'"$date"'</lastmod><priority>1.0</priority></url>#'
    echo '</urlset>'
}

But who would use this?

func render_sitemap(params []string)

I consider shell function with more than one parameter a contributor to write-only code.

Multiple text processors

The text processor hierarchy follows:

Generally, you should use one and only one of those specified above. Start with the lower-down tool, then instead of adding a command to the pipe use a higher-tier text processor.

In pipes, programming languages get out of hand quickly. Putting everything in quotes and no syntax highlighting is difficult.

This list will probably get a few updates in the future. For now, happy scripting!


.
├── my
│   ├── matrix
│   ├── email
│   ├── sourcehut
│   ├── lobsters
│   └── github
├── code
│   ├── i
│   ├── feef
│   ├── greedy
│   ├── typeup
│   ├── metro
│   └── point
├── blog
│   ├── Shell sins
│   ├── DWM tips
│   ├── How this site was made (old site)
│   └── Go as a dynamic plugin language for Go
├── wiki
├── notes
│   └── Tree-style blogs
├── pictures
├── webring
│   ├── bakpakin.com
│   ├── macwright.com
│   ├── arp242.net
│   ├── icyphox.sh
│   ├── zdsmith.com
│   ├── christine.website
│   ├── wiki.alopex.li
│   ├── erkin.party
│   ├── dave.cheney.net
│   ├── technomancy.us
│   ├── rxi.github.io
│   ├── hisham.hm
│   ├── junglecoder.com
│   ├── kivikakk.ee
│   ├── bernsteinbear.com
│   ├── 100r.co
│   ├── acha.ninja
│   ├── hillelwayne.com
│   ├── jvns.ca
│   ├── research.swtch.com
│   ├── eigenstate.org
│   └── drewdevault.com