[Image: the ‘improved’ version, see below for the text to copy/paste.]
Published: 2023-06-15
This short article extends the previous in the SocTech series, and wont make much sense unless you’ve read that first.
Scratching an itch
I've been sitting on/working on an article for a while. Its has lots of sources and I wish to be able to peruse them quickly while writing. "Ah-Ha!" says I. We just use the idea from the 'news.sh' script.
So, I made a file with all the links I wanted, in the order I wanted them, and then just typed in the operational part of the news.sh script, substituting the new file which I'd called srcs.txt:
$ ./tor-browser_en-US/Browser/start-tor-browser $(cat srcs.txt)
Up pops Tor with all my sources. Yay!
Then, still being the silly programmer I have been, my mind whispers "default argument". The 'news.sh' script uses a default argument, the .firefox.start.urls.txt file. This can be expanded by allowing an actual argument to be supplied to the script for the file with the URL list. If an argument is not supplied then the default is used.
The script becomes (the copy/paste text is here, but keep reading first).
(You can see why I included the screenshot image above with the colour highlighting. It is much easier to read than this. Let’s say Substack is not designed to include code snippets. It even kills the indentation.)
#!/usr/bin/bash
BROWSER=/usr/bin/firefox
# The default argument
URL_FILE=$HOME/.firefox.start.urls.rc
# if the length of the first argument as a string is not zero, i.e
# there is a first argument ...
if [ -n "$1" ]
then
if [ -f "$1" ] # and it is actually a file
then
URL_FILE="$1"
else
# otherwise inform of the error exit
echo "news.sh: Error: argument '$1' is not a file"
echo "Usage: news.sh [path to a file with one url per line]"
exit 1
fi
fi
# Start with a set of URLs to open
#
$BROWSER $(cat $URL_FILE)
Now, this could be improved further by also providing a '-h' flag which prints a helpful usage message, but then I'd need to introduce the 'case' statement and it all becomes a bit too much. This will do for now.
So, in normal operation (using the default argument) one just types 'news' and the alias does the whole thing. Or, one can now type:
$ news.sh my-new-url-file.txt
to have the script use that file instead of the default.
If you'd like to try this out, go nuts! But, start by taking a backup of what works (cp ./bin/news.sh ./bin/news.sh.backup) in case things go sideways.
A note for the technically minded
The 'if [ some-condition ]' statement is syntactic sugar. bash will translate it to:
if test some-condition
and test is a command. Read about it in the manual page:
$ man test #
If you get stuck in the manual page, press ‘q’
[Technically this is false, the manual page for test refers to the actual test command, and the ‘test’ being run by bash is internal to bash but they are so similiar that the difference only needs be noted for the pedants.]
The real learning here is that, whether the if clause is used is not dependent on whether the argument to the script exists but on whether the exit status of the test command is zero. This is a subtle distinction but matters if one wishes to learn about how to use bash as a scripting (programming) language. Now we really are getting down into the weeds; exit statuses.
Before long you'll have me talking about the Linux kernel and the core architecture of UNIX. Another time, perhaps.
Notification
Subscription is optional. Subscribers can expect notifications for most articles. Better is to use RSS (feed), or bookmark the Archive page and visit at leisure. If you use Twitter, following @YesXorNo1 is also a partially effective notifications strategy. A reliable notification mechanism is the use of Substack’s Notes facility.
Copyright and Licensing
None. Go nuts.
Comments: on topic, no abuse.