Mark Furland

Tool recommendations and Bash Snippets

While reading links from an article on dotfiles in the first issue of

Paged Out

 I came across a tool I hadn't seen.
I like reading about other people's tools and their dotfiles, so here are some tools and bash snippets I think are useful.

I run a bash shell, with tmux and vim.

Tool recommendations

No particular order, and just a random sampling of the first few things filtered by how recently I used them.

Facebook Path Picker


https://github.com/facebook/pathpicker/

Can take the output of git status or something, and let you pick some of the files, and then open it in an editor.



Diff So Fancy


https://github.com/so-fancy/diff-so-fancy

Better git diffs. Shows what in a line changed, and clear file headers.


up

 

https://github.com/akavel/up

"the ultimate pipe plumber", you pipe some data in and it interactively write stuff to pipe it to.



jq (and xq/yq)


https://github.com/stedolan/jq

jq is a command line json parser.
xq/yq are just xml->json or yaml->json parsers that automatically call jq.

Example:

We want to get the latest rasbian image.

The [raspberry pi download RSS](http://downloads.raspberrypi.org/rss.xml) feed looks like this:

<?xml version='1.0' encoding='UTF-8' ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Raspberry Pi Root Filesystem Downloads</title>
        <link>http://www.raspberrypi.org/downloads</link>
        <description>The latest root filesystems for the Raspberry Pi computer!</description>

        <item>
        <title><![CDATA[archlinux-hf-2013-06-15]]></title>
        <link>http://downloads.raspberrypi.org/arch/images/archlinux-hf-2013-06-15/archlinux-hf-2013-06-15.img.zip.torrent</link>
        </item>
        ...
        <item>
        <title><![CDATA[raspbian_lite-2019-07-12]]></title>
        <link>http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-07-12/2019-07-10-raspbian-buster-lite.zip.torrent</link>
        </item>
        ...
        <item>
        <title><![CDATA[raspbian_lite-2019-06-24]]></title>
        <link>http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-06-24/2019-06-20-raspbian-buster-lite.zip.torrent</link>
        </item>
    </channel>
</rss>
> curl http://downloads.raspberrypi.org/rss.xml | xq '[.rss.channel.item[] | select(.title | contains("raspbian_lite"))] | .[-1]'
{
  "title": "raspbian_lite-2019-07-12",
  "link": "http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-07-12/2019-07-10-raspbian-buster-lite.zip.torrent"
}

 

rename

Run regex on filenames.
Example: rename all .txt files to .md

rename ‘s/txt/md/’ *.txt

ncdu
https://dev.yorhel.nl/ncdu
NCurses Disk Usage shows disk usage.

Bash snippets

  • [Shebang line](https://en.wikipedia.org/wiki/Shebang_(Unix)) says how to execute this file, it’s for more than just bash. Please use one.
  • Second line makes sure that the script executes from the directory it is stored in.
  • set -e causes bash to exit on any non-zero status.
  • set -u causes bash to exit on undefined variables.
    • For more on those, see http://redsymbol.net/articles/unofficial-bash-strict-mode/

 

#!/bin/bash
cd $(dirname "$0")
set -eu
  • Take command line flags and parse them, have a help function.
  • Also how to use true/false, as they don’t exist in bash but if you use them like this it will work.
  • getopt can do some fancier things, see `man getopt`
help () {
    echo "Usage: gsource FILE [LINE NUMBER]"
    echo "Link to the online git file, with optional line number"
    echo "  -h, --help       display this help and exit"
}

# getopt short options go together, long options have commas
TEMP=`getopt -o hd:f --long help,dir,force -n 'test.sh' -- "$@"`
if [ $? != 0 ] ; then
    echo "Something wrong with getopt" >&2
    exit 1
fi
eval set -- "$TEMP"

dir=""
while true ; do
    case "$1" in
        -h|--help) help; exit 0; shift ;;
        -d|--dir) dir=$2 ; shift 2 ;;
        -f|--force) force=true ; shift ;;
        --) shift ; break ;;
        *) echo "Internal error, unexpected argument '$0'!" ; exit 1 ;;
    esac
done

if [ "$force" = true ]; then
  echo "force was true or whatever"
fi
  • y/n prompt
  • press any key to continue prompt
read -r -p "some yes/no question [Y/n] " response
case "$response" in
  [nN][oO]|[nN])
    echo "Alright setup your own key"
    ;;
  [yY][eE][sS]|[yY])
    echo "Alright good luck"
    ;;
esac


read -n 1 -s -r -p "Press any key to continue"

 

Lastly
Just run it:

curl wttr.in/?m

Hope you learned something, and I hope you share other things with me.

Feel free to look at

my dotfiles