From e7b414a31fe7feb9138a00cc580370012599f359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 2 Feb 2023 12:09:16 +0100 Subject: wiki script shellcheck + arrays. Shellcheck for good meassure, and it showed that I handled wiki names incorrectly. Replace old string concatenation version with proper array base version (which also shows why you shouldn't use bash...). --- wiki | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/wiki b/wiki index 1f7dcc1..45b2065 100755 --- a/wiki +++ b/wiki @@ -1,12 +1,19 @@ #!/bin/bash WIKIROOT=/home/hugo/wiki/ -WIKI_LIST=$(ls $WIKIROOT | grep -v html) +# Causes the last function in a non-background pipeline to use the +# current shell. Needed for readarray to set a variable in the script. +shopt -s lastpipe +declare -a WIKI_LIST +printf '%s\0' "$WIKIROOT"/* \ + | grep --null-data -v html \ + | xargs -0 -n1 basename -z \ + | readarray -d '' -t WIKI_LIST # A simple script for managing a vimwiki git repo. # Allows slightly simpler edits and commits. -wiki_list="" +declare -a wiki_list GIT='git -c color.status=always -c color.ui=always' @@ -44,20 +51,20 @@ function wiki_do { wiki=$1 command=$2 - pushd $WIKIROOT/$wiki + pushd "$WIKIROOT/$wiki" || exit 1 shift case $command in commit) shift - commit $@ + commit "$@" ;; ammend) shift - ammend $@ + ammend "$@" ;; g|go) - commit $@ + commit "$@" $GIT push ;; echo) @@ -78,14 +85,14 @@ while [ $# -ne 0 ]; do case $1 in -w|--wiki) shift - wiki_list="$1 $wiki_list" + wiki_list+=("$1") shift ;; -l|--list) shift echo "Available Wikis:" echo "================" - for w in $WIKI_LIST; do + for w in "${WIKI_LIST[@]}"; do echo " - $w" done exit @@ -115,14 +122,14 @@ done # Edit the given wiki if no further argument is given if [ $# -eq 0 ]; then - set -- ${wiki_list:-$WIKI_LIST} - vim $WIKIROOT/$1/index.* + set -- "${wiki_list[0]:-${WIKI_LIST[0]}}" + vim "$WIKIROOT/$1"/index.* exit fi # Run given command for all wikis -for wiki in ${wiki_list:-$WIKI_LIST}; do - wiki_do $wiki $@ | sed "s/^/$wiki /" & +for wiki in "${wiki_list[@]:-${WIKI_LIST[@]}}"; do + wiki_do "$wiki" "$@" | sed "s/^/$wiki /" & done -- cgit v1.2.3