💾 Archived View for 9til.de › journal › two-shell-tricks.gemini captured on 2023-11-14 at 07:44:28. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

[2020-05-13] Two little shell tricks

I was recently reminded of textfiles.com existence via a gopher mirror.

http://textfiles.com

Textfiles.com gopher mirror

This gave me a silly idea, how about adding a random textfile from textfiles.com to the finger space?

This was relatively easy as textfiles.com provides zip files for all of its categories. I downloaded them, cleaned up the pictures and html files I didn't want to display and uploaded these files to my finger server.

In the process I learned two neat little shell tricks.

First I wanted to get a random files from a directory structure, a quick search engine lookup made me aware of the `shuf` command which I didn't knew.

shuf(1) manpage

Getting a random file looks like:

	find /var/finger/textfiles -type f | shuf -n 1

Pretty cool!

When returning the content I added a small header with the path to the file. This path mimicks the one on the web version of textfiles.com. The only issue is that I wanted to remove the `/var/finger` part. I initially tried with awk but then stubbled upon a much nicer solution in:

	file=$(find /var/finger/textfiles -type f | shuf -n 1)
	echo -e "\nRandom Textfile: ${file#/var/finger/}\n"

The `file#/var/finger/` part will remove `/var/finger/` from file thanks to POSIX shell variable expansion. As seen here:

Shell Parameter Expansion

That's it! I learned two shell tricks doing this funny finger experiment, very happy I tried it!

finger textfile@typed-hole.org (can be NSFW)

---

Served by Pollux Gemini Server.