dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit be06e4014181b963b54d0a26b46636f55e087743
parent b2c43e8271a34ded78e539282fc422d7780ff258
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Thu, 16 May 2019 13:57:26 -0500

Transition several scripts to POSIX sh

Diffstat:
Mbin/anders | 4++--
Mbin/board | 16++++++++--------
Mbin/displayselect | 20++++++++++----------
Mbin/dmenu_themed | 6+++---
Mbin/forecast | 20++++++++++----------
Mbin/forecast-sky | 17++++++++---------
Mbin/getdoi | 18+++++++++---------
Mbin/getref | 20++++++++++----------
Mbin/shdl | 16++++++++--------
Dbin/t-followers-diff.sh | 48------------------------------------------------
10 files changed, 68 insertions(+), 117 deletions(-)

diff --git a/bin/anders b/bin/anders @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh # cmd-line args as message -msg="$@" +msg="$*" # use STDIN if no args are passed if [ $# -eq 0 ]; then diff --git a/bin/board b/bin/board @@ -1,23 +1,23 @@ #!/usr/bin/env bash -function board { +board() { #dir="$HOME/iawriter/" dir="." format="-O" if [ $# -gt 0 ]; then - if [ "$1" == "-h" ]; then + if [ "$1" = "-h" ]; then echo "board: Open a progress board of markdown files." echo "usage: $0 [options] [target_directory]" echo "options:" - echo -e "\\t-h show this info" - echo -e "\\t-s open view as horizontal splits" - echo -n "By default the {todo,progress,done}.md files are opened in " - echo -n "the current directory. Optionally, a different directory " - echo "is used if a 'target_directory' is specified." + printf "\\t-h show this info\n" + printf "\\t-s open view as horizontal splits\n" + printf "By default the {todo,progress,done}.md files are opened in " + printf "the current directory. Optionally, a different directory " + printf "is used if a 'target_directory' is specified.\n" exit 0 - elif [ "$1" == "-h" ]; then + elif [ "$1" = "-h" ]; then format="-o" if [ $# -gt 1 ]; then dir="$2" diff --git a/bin/displayselect b/bin/displayselect @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # A UI for detecting and selecting all displays. # Probes xrandr for connected displays and lets user select one to use. @@ -19,10 +19,10 @@ twoscreen() { # If multi-monitor is selected and there are two screens. res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \ tail -n 1 | awk '{print $1}') - res_ext_x=$(echo $res_external | sed 's/x.*//') - res_ext_y=$(echo $res_external | sed 's/.*x//') - res_int_x=$(echo $res_internal | sed 's/x.*//') - res_int_y=$(echo $res_internal | sed 's/.*x//') + res_ext_x=$(echo "$res_external" | sed 's/x.*//') + res_ext_y=$(echo "$res_external" | sed 's/.*x//') + res_int_x=$(echo "$res_internal" | sed 's/x.*//') + res_int_y=$(echo "$res_internal" | sed 's/.*x//') scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l) scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l) @@ -58,8 +58,8 @@ morescreen() { # If multi-monitor is selected and there are more than two screen multimon() { # Multi-monitor handler case "$(echo "$screens" | wc -l)" in - 1) xrandr $(echo "$allposs" | awk '{print "--output", $1, "--off"}' \ - | tr '\n' ' ') ;; + 1) xrandr "$(echo "$allposs" | awk '{print "--output", $1, "--off"}' \ + | tr '\n' ' ')" ;; 2) twoscreen ;; *) morescreen ;; esac ; @@ -72,15 +72,15 @@ allposs=$(xrandr -q | grep "connected") screens=$(echo "$allposs" | grep " connected" | awk '{print $1}') # Get user choice including multi-monitor and manual selection: -if [ $(echo "$screens" | wc -l) -gt 1 ]; then +if [ "$(echo "$screens" | wc -l)" -gt 1 ]; then chosen=$(printf "%s\\nmulti-monitor\\nmanual selection" "$screens" \ | dmenu_themed -i -p "Select display arangement:") && case "$chosen" in "manual selection") arandr ; exit ;; "multi-monitor") multimon ;; *) xrandr --output "$chosen" --auto --scale 1.0x1.0 \ - $(echo "$screens" | grep -v "$chosen" | \ - awk '{print "--output", $1, "--off"}' | tr '\n' ' ') ;; + "$(echo "$screens" | grep -v "$chosen" | \ + awk '{print "--output", $1, "--off"}' | tr '\n' ' ')" ;; esac fi diff --git a/bin/dmenu_themed b/bin/dmenu_themed @@ -1,6 +1,6 @@ -#!/usr/bin/env bash +#!/bin/sh -function get_xresources_color { +get_xresources_color() { rg "\*\.$1:" ~/.Xresources | awk '{ print $2 }' } @@ -10,7 +10,7 @@ dmenuopts="-nb $(get_xresources_color background)\ -sf $(get_xresources_color background) \ -fn PragmataPro-9" -if [[ "$1" = "run" ]]; then +if [ "$1" = "run" ]; then dmenu_run $dmenuopts else dmenu $dmenuopts "$@" diff --git a/bin/forecast b/bin/forecast @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/sh # vim: ft=sh: set -e @@ -6,41 +6,41 @@ set -e #zipcode=7700 # Thisted default_zipcode=9520 # Skoerping -function show_help { +show_help() { echo "usage: ${0##*/} [radar | 3-9 | wind] [ZIPCODE]" echo "gets DMI forecast, with ZIPCODE=$default_zipcode by default" } -if command -v open &>/dev/null; then +if command -v open >/dev/null 2>&1; then open=open -elif command -v xdg-open &>/dev/null; then +elif command -v xdg-open >/dev/null 2>&1; then open=xdg-open else open="echo" fi -function get_url { +get_url() { echo "http://servlet.dmi.dk/byvejr/servlet/byvejr_dag1?by=${1}&mode=long&eps=true" } if [ $# -gt 0 ]; then - if [ "$1" == "radar" ]; then + if [ "$1" = "radar" ]; then $open "http://www.dmi.dk/vejr/maalinger/radar-nedboer/" exit 0 - elif [ "$1" == "3-9" ]; then + elif [ "$1" = "3-9" ]; then if [ $# -gt 1 ]; then zip="$2" else zip="$default_zipcode" fi url="http://servlet.dmi.dk/byvejr/servlet/byvejr?by=${zip}&tabel=dag3_9&eps=true" - elif [ "$1" == "wind" ]; then + elif [ "$1" = "wind" ]; then url="http://servlet.dmi.dk/bv/servlet/bvImage?stat=6065&param=wind" - elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then + elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then show_help exit 0 else - url=$(get_url $1) + url=$(get_url "$1") fi else url=$(get_url $default_zipcode) diff --git a/bin/forecast-sky b/bin/forecast-sky @@ -1,15 +1,14 @@ -#!/bin/bash -e -# vim: ft=sh: +#!/bin/sh -tmpfile=`mktemp`.png +tmpfile=$(mktemp).png curl --location \ 'http://www.cleardarksky.com/c/AAAP1_NJcsk.gif?c=468010'\ - --output $tmpfile -if command -v open &>/dev/null; then - open $tmpfile -elif command -v xdg-open &>/dev/null; then - xdg-open $tmpfile + --output "$tmpfile" +if command -v open >/dev/null 2>&1; then + open "$tmpfile" +elif command -v xdg-open >/dev/null 2>&1; then + xdg-open "$tmpfile" else - echo $tmpfile + echo "$tmpfile" fi diff --git a/bin/getdoi b/bin/getdoi @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh version=1.0 host="http://api.crossref.org/works" -function show_help { +show_help() { echo "usage: ${0##*/} [OPTIONS] QUERY" echo "will attempt to get the DOI from $host" echo "where QUERY can consist of publication title, author, DOI, ORCID id," @@ -22,24 +22,24 @@ function show_help { echo " -- do not consider any following args as options" } -function show_version { +show_version() { echo "${0##*/} version $version" echo "Licensed under the GNU Public License, v3+" echo "written by Anders Damsgaard, anders@adamsgaard.dk" echo "https://gitlab.com/admesg/dotfiles" } -function die { +die() { printf '%s\n' "$1" >&2 exit 1 } -function extract_dois { +extract_dois() { tr ',' '\n' | grep DOI | \ sed 's/.*DOI":"//' | sed 's/"}.*//' | sed 's|\\\/|/|g' } -function browser_open { +browser_open() { if command -v xdg-open >/dev/null 2>&1; then [ "$verbose" = 1 ] && echo "launching default browser" xdg-open "$1" @@ -51,7 +51,7 @@ function browser_open { fi } -function get_doi_from_crossref { +get_doi_from_crossref() { query="$(echo "$@" | sed 's/ /+/g')" url="$host?rows=$number&select=DOI&query=$query" [ "$verbose" = 1 ] && echo "connecting to $url" @@ -62,7 +62,7 @@ function get_doi_from_crossref { echo "$result" | extract_dois } -function get_doi_from_file { +get_doi_from_file() { doi=$(pdfinfo "$1" | grep -io "doi.*") || doi=$(pdftotext "$1" 2>/dev/null - | grep -io "doi.*" -m 1 ) || die "Error: Could not extract DOI from file $doi" @@ -70,7 +70,7 @@ function get_doi_from_file { echo "$doi" } -function get_doi { +get_doi() { if [ -e "$1" ]; then doi=$(get_doi_from_file "$1") else diff --git a/bin/getref b/bin/getref @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh version=1.0 host="https://doi.org" -function show_help { +show_help() { echo "usage: ${0##*/} [OPTIONS] DOI1 [DOI2 ...]" echo "will attempt to get a BibTeX citation from $host" echo "If no DOIs are specified, this program will expect DOIs as stdin." @@ -20,23 +20,23 @@ function show_help { echo " -- do not consider any following args as options" } -function show_version { +show_version() { echo "${0##*/} version $version" echo "Licensed under the GNU Public License, v3+" echo "written by Anders Damsgaard, anders@adamsgaard.dk" echo "https://gitlab.com/admesg/dotfiles" } -function die { +die() { printf '%s\n' "$1" >&2 exit 1 } -function format_bibtex_key { +format_bibtex_key() { sed '/@/ s/_\([0-9]\)/\1/' } -function abbreviate_journal_name { +abbreviate_journal_name() { sed '/journal = / { s/Journal/J./ s/Geophysical/Geophys./ @@ -79,15 +79,15 @@ function abbreviate_journal_name { }' } -function abbreviate_author_name { +abbreviate_author_name() { sed '/author = / { s/\([A-Z]\)[A-Za-z]* \([A-Z]\)/\1. \2/g }' } -function strip_doi { +strip_doi() { sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//' } -function get_citation { +get_citation() { doi=$(echo "$1" | strip_doi) url="$host/$1" [ "$verbose" = 1 ] && echo "connecting to $url" @@ -99,7 +99,7 @@ function get_citation { [ "$fullauthor" = 0 ] && result="$(echo "$result" | abbreviate_author_name)" result="$(echo "$result" | sed 's/\t/ /g')" if [ "$nonewline" = 1 ]; then - echo -en "\n$result" + printf "\n%s" "$result" else echo "$result" fi diff --git a/bin/shdl b/bin/shdl @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh version=1.0 shhost="http://sci-hub.tw" -function show_help { +show_help() { echo "usage: ${0##*/} [OPTIONS] DOI1 [DOI2...[DOI N]]" echo "will fetch each DOI from $shhost" echo "If no DOI is specified, this program will expect DOIs as stdin." @@ -23,18 +23,18 @@ function show_help { echo " -- do not consider any following args as options" } -function show_version { +show_version() { echo "${0##*/} version $version" echo "Licensed under the GNU Public License, v3+" echo "written by Anders Damsgaard, anders@adamsgaard.dk" echo "https://gitlab.com/admesg/dotfiles" } -function strip_doi { +strip_doi() { sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//' } -function browser_open { +browser_open() { echo "$1" if command -v tor-browser 2>/dev/null; then [ "$verbose" = 1 ] && echo "attempting to launch tor-browser" @@ -50,7 +50,7 @@ function browser_open { fi } -function shdl_fetch { +shdl_fetch() { shurl="$shhost/$doi" [ "$verbose" = 1 ] && echo "processing $doi" [ "$verbose" = 1 ] && echo "connecting to $shurl $prefix" @@ -77,7 +77,7 @@ function shdl_fetch { fi } -function handle_doi { +handle_doi() { if [ "$browser" = 1 ]; then shurl="$shhost/$1" browser_open "$shurl" @@ -96,7 +96,7 @@ function handle_doi { fi } -function die { +die() { printf '%s\n' "$1" >&2 exit 1 } diff --git a/bin/t-followers-diff.sh b/bin/t-followers-diff.sh @@ -1,48 +0,0 @@ -#!/bin/bash - -# https://github.com/sferik/t - -# test if t is available and working correctly -t help >/dev/null 2>&1 -if [ $? -ne 0 ]; then - echo "t not working. Trying to (re)install ruby and gems" - - rm -rf /usr/local/lib/ruby/gems/ - brew uninstall ruby --ignore-dependencies - brew install ruby - brew link --overwrite ruby - - command -v gem >/dev/null 2>&1 || \ - { (>&2 echo "ERROR: gem command not available. Bye."); exit 1; } - - gem update - gem install t - - t help >/dev/null 2>&1 - if [ $? -ne 0 ]; then - (>&2 echo "ERROR: t still not working. Bye.") - exit 1 - fi -fi - -tmpfile=~/tmp/t-followers.txt -mv $tmpfile{,-old} - -RED='\033[0;31m' -GREEN='\033[0;32m' -NC='\033[0m' - -#t followers -l | awk '{ print $13 }' > $tmpfile -t followers | \ - sed 's/ */ /' | sed 's/ *//g' \ - > $tmpfile - -#echo -e "${RED}< unfollowed ${GREEN}> new follower${NC}" -colordiff $tmpfile{-old,} | sed 's/ / https:\/\/twitter.com\//' -nold="$(wc -l $tmpfile-old | awk '{ print $1 }')" -nnew="$(wc -l $tmpfile | awk '{ print $1 }')" -if [ "$nold" -eq "$nnew" ]; then - echo -e "No change in followers (${GREEN}$nnew${NC})" -else - echo -e "From ${RED}$nold${NC} to ${GREEN}$nnew${NC} followers" -fi