-- Leo's gemini proxy

-- Connecting to typed-hole.org:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

[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.

-- Response ended

-- Page fetched on Sat Apr 20 00:56:03 2024