Those who are tired of reading the same old repetitive fortune cookies, you have come to the right place to learn the magic of configuring dynamic and more useful tips.
Prerequisites:
Kindly install the following tools from your package manager
- curl (pull the html page)
- html-xml-utils (manipulate html content)
- w3m (display text only from html)
Create a Bash Function to pull from your desired website:
For educational purposes, I’m picking up https://til.hashrocket.com.
curl -s https://til.hashrocket.com/random | hxnormalize -x | hxselect 'div.post__content'
- Use
curlto pull a random html page from the website and put it in stdin-sflag hxnormalizepretty prints html contenthxselectselects the target with our useful tip
In your .rc file (bashrc, .etc,.):
- Put the function and call it in a bash variable
TIP=$(til_function).
if [ ! "$TIP" ]; then
fortune 30% linux 30% beam 10% computers 10% science 10% debian 5% wisdom 5% work
else
echo "$RANDOM_TIP" | w3m -dump -T 'text/html'
echo "Pulled from https://til.hashrocket.com on $(date +'%d-%m-%Y %H:%M:%S')."
fi
- If the
$TIPhas no content (no internet connection / timeout), then fallback to local fortune cookies - If
$TIPhas content, then usew3mto display or render the html content showing only textual content
The end result should look like the above screenshot Whenever you open a new terminal window or tab. Hope you enjoyed the tutorial!