From 10ab9d3364a54ab695803e4c4fc86f5f90f14a17 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 19 Aug 2020 17:05:58 +0800 Subject: [PATCH] Improve the Shell code style. --- _scripts/sh/create_pages.sh | 44 +++++++++++++++++------------------ _scripts/sh/dump_lastmod.sh | 8 +++---- _scripts/sh/sync_monitor.sh | 16 ++++++------- tools/build.sh | 46 ++++++++++++++++++------------------- tools/deploy.sh | 9 ++++---- tools/publish.sh | 6 ++--- tools/pv.sh | 10 ++++---- tools/run.sh | 32 +++++++++++++------------- 8 files changed, 86 insertions(+), 85 deletions(-) diff --git a/_scripts/sh/create_pages.sh b/_scripts/sh/create_pages.sh index 12e8068..248ae77 100755 --- a/_scripts/sh/create_pages.sh +++ b/_scripts/sh/create_pages.sh @@ -20,15 +20,15 @@ tag_count=0 _read_yaml() { - local _endline=$(grep -n "\-\-\-" $1 | cut -d: -f 1 | sed -n '2p') - head -$_endline $1 + local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')" + head -"$_endline" "$1" } read_categories() { - local _yaml=$(_read_yaml $1) - local _categories=$(echo "$_yaml" | grep "^categories:") - local _category=$(echo "$_yaml" | grep "^category:") + local _yaml="$(_read_yaml "$1")" + local _categories="$(echo "$_yaml" | grep "^categories:")" + local _category="$(echo "$_yaml" | grep "^category:")" if [[ ! -z "$_categories" ]]; then echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" @@ -39,7 +39,7 @@ read_categories() { read_tags() { - local _yaml=$(_read_yaml $1) + local _yaml="$(_read_yaml "$1")" echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" } @@ -65,14 +65,14 @@ init() { create_category() { if [[ ! -z $1 ]]; then local _name=$1 - local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html" + local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html" - if [[ ! -f $_filepath ]]; then - echo "---" > $_filepath - echo "layout: category" >> $_filepath - echo "title: $_name" >> $_filepath - echo "category: $_name" >> $_filepath - echo "---" >> $_filepath + if [[ ! -f "$_filepath" ]]; then + echo "---" > "$_filepath" + echo "layout: category" >> "$_filepath" + echo "title: $_name" >> "$_filepath" + echo "category: $_name" >> "$_filepath" + echo "---" >> "$_filepath" ((category_count=category_count+1)) fi @@ -83,15 +83,15 @@ create_category() { create_tag() { if [[ ! -z $1 ]]; then local _name=$1 - local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html" + local _filepath="tags/$( echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html" - if [[ ! -f $_filepath ]]; then + if [[ ! -f "$_filepath" ]]; then - echo "---" > $_filepath - echo "layout: tag" >> $_filepath - echo "title: $_name" >> $_filepath - echo "tag: $_name" >> $_filepath - echo "---" >> $_filepath + echo "---" > "$_filepath" + echo "layout: tag" >> "$_filepath" + echo "title: $_name" >> "$_filepath" + echo "tag: $_name" >> "$_filepath" + echo "---" >> "$_filepath" ((tag_count=tag_count+1)) fi @@ -116,13 +116,13 @@ create_pages() { $TYPE_CATEGORY) for i in ${_string#,}; do - create_category $i + create_category "$i" done ;; $TYPE_TAG) for i in ${_string#,}; do - create_tag $i + create_tag "$i" done ;; diff --git a/_scripts/sh/dump_lastmod.sh b/_scripts/sh/dump_lastmod.sh index ceb8ba5..235adac 100755 --- a/_scripts/sh/dump_lastmod.sh +++ b/_scripts/sh/dump_lastmod.sh @@ -35,8 +35,8 @@ _init() { _has_changed() { - local _log_count=`git log --pretty=%ad $1 | wc -l | sed 's/ *//'` - _log_count=$(($_log_count + 0)) + local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')" + _log_count=$((_log_count + 0)) if [[ $_log_count > 1 ]]; then return 0 # true @@ -56,7 +56,7 @@ _has_changed() { # the file '_data/updates.yml' ################################### _dump() { - local _lasmod="`git log -1 --pretty=%ad --date=iso $2`" + local _lasmod="$(git log -1 --pretty=%ad --date=iso "$2")" if [[ ! -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then touch "$OUTPUT_DIR/$OUTPUT_FILE" @@ -76,7 +76,7 @@ main() { for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)) do - _filename=$(basename $_file | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' ) # remove date and extension + _filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' )" # remove date and extension if _has_changed "$_file"; then _dump "$_filename" "$_file" diff --git a/_scripts/sh/sync_monitor.sh b/_scripts/sh/sync_monitor.sh index fd7840e..fa74896 100644 --- a/_scripts/sh/sync_monitor.sh +++ b/_scripts/sh/sync_monitor.sh @@ -11,28 +11,28 @@ # $3 -> the destination sync directory # Omit the system temp file -if [[ ! -f $1 ]]; then +if [[ ! -f "$1" ]]; then exit 0 fi -src_dir=`dirname $(realpath $1)` +src_dir="$(dirname $(realpath "$1"))" -dir_prefix="$(realpath $2)/" +dir_prefix="$(realpath "$2")/" related_dir="${src_dir:${#dir_prefix}}" -dest="$(realpath $3)/${related_dir}" +dest="$(realpath "$3")/${related_dir}" if [[ ! -d "$dest" ]]; then mkdir -p "$dest" fi if [[ -f "$1" ]]; then - cp $1 $dest + cp "$1" "$dest" fi -if [[ $related_dir == "_posts" ]]; then - bash $3/_scripts/sh/create_pages.sh - bash $3/_scripts/sh/dump_lastmod.sh +if [[ "$related_dir" == "_posts" ]]; then + bash "$3"/_scripts/sh/create_pages.sh + bash "$3"/_scripts/sh/dump_lastmod.sh fi diff --git a/tools/build.sh b/tools/build.sh index d99b67f..db9ff0c 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -10,11 +10,11 @@ set -eu CMD="JEKYLL_ENV=production bundle exec jekyll b" -WORK_DIR=$(dirname $(dirname $(realpath "$0"))) +WORK_DIR="$(dirname $(dirname $(realpath "$0")))" -CONTAINER=${WORK_DIR}/.container +CONTAINER="${WORK_DIR}/.container" -DEST=${WORK_DIR}/_site +DEST="${WORK_DIR}/_site" _help() { @@ -30,44 +30,44 @@ _help() { _init() { - cd $WORK_DIR + cd "$WORK_DIR" - if [[ -d $CONTAINER ]]; then - rm -rf $CONTAINER + if [[ -d "$CONTAINER" ]]; then + rm -rf "$CONTAINER" fi - if [[ -d _site ]]; then + if [[ -d "_site" ]]; then jekyll clean fi - local _temp=$(mktemp -d) - cp -r * $_temp - cp -r .git $_temp - mv $_temp $CONTAINER + local _temp="$(mktemp -d)" + cp -r ./* "$_temp" + cp -r ./.git "$_temp" + mv "$_temp" "$CONTAINER" } _build() { - cd $CONTAINER + cd "$CONTAINER" echo "$ cd $(pwd)" - bash _scripts/sh/create_pages.sh - bash _scripts/sh/dump_lastmod.sh + bash "_scripts/sh/create_pages.sh" + bash "_scripts/sh/dump_lastmod.sh" - CMD+=" -d ${DEST}" + CMD+=" -d $DEST" echo "\$ $CMD" - eval $CMD + eval "$CMD" echo -e "\nBuild success, the site files have been placed in '${DEST}'." - if [[ -d ${DEST}/.git ]]; then - if [[ ! -z $(git -C $DEST status -s) ]]; then - git -C $DEST add . - git -C $DEST commit -m "[Automation] Update site files." -q + if [[ -d "${DEST}/.git" ]]; then + if [[ ! -z $(git -C "$DEST" status -s) ]]; then + git -C "$DEST" add . + git -C "$DEST" commit -m "[Automation] Update site files." -q echo -e "\nPlease push the changes of $DEST to remote master branch.\n" fi fi - cd .. && rm -rf $CONTAINER + cd .. && rm -rf "$CONTAINER" } @@ -95,8 +95,8 @@ main() { shift ;; -d|--destination) - _check_unset $2 - DEST=$(realpath $2) + _check_unset "$2" + DEST="$(realpath "$2")" shift; shift; ;; diff --git a/tools/deploy.sh b/tools/deploy.sh index 66580d3..f3aebfd 100755 --- a/tools/deploy.sh +++ b/tools/deploy.sh @@ -14,17 +14,18 @@ PAGES_BRANCH="gh-pages" _no_branch=false -if [[ -z `git branch -av | grep $PAGES_BRANCH` ]]; then +if [[ -z $(git branch -av | grep "$PAGES_BRANCH") ]]; then _no_branch=true - git checkout -b $PAGES_BRANCH + git checkout -b "$PAGES_BRANCH" else - git checkout $PAGES_BRANCH + git checkout "$PAGES_BRANCH" fi mv _site ../ mv .git ../ -rm -rf * && rm -rf .[^.] .??* +rm -rf ./* +rm -rf .[^.] .??* mv ../_site/* . mv ../.git . diff --git a/tools/publish.sh b/tools/publish.sh index a794f75..755d0ae 100755 --- a/tools/publish.sh +++ b/tools/publish.sh @@ -13,12 +13,12 @@ CATEGORIES=false TAGS=false LASTMOD=false -WORK_DIR=$(dirname $(dirname $(realpath "$0"))) +WORK_DIR="$(dirname $(dirname $(realpath "$0")))" check_status() { local _change=$(git status . -s) - if [[ ! -z ${_change} ]]; then + if [[ ! -z $_change ]]; then echo "Warning: Commit the following changes first:" echo "$_change" exit 1 @@ -81,7 +81,7 @@ push() { main() { - cd $WORK_DIR + cd "$WORK_DIR" check_status diff --git a/tools/pv.sh b/tools/pv.sh index cd45992..19a9b92 100755 --- a/tools/pv.sh +++ b/tools/pv.sh @@ -15,12 +15,12 @@ set -eu -WORK_DIR=$(dirname $(dirname $(realpath "$0"))) -URL_FILE=${WORK_DIR}/_config.yml -PV_CACHE=${WORK_DIR}/assets/js/data/pageviews.json +WORK_DIR="$(dirname $(dirname $(realpath "$0")))" +URL_FILE="${WORK_DIR}/_config.yml" +PV_CACHE="${WORK_DIR}/assets/js/data/pageviews.json" -PROXY_URL=$(grep "proxy_endpoint:" $URL_FILE | sed "s/.*: '//g;s/'.*//") +PROXY_URL="$(grep "proxy_endpoint:" "$URL_FILE" | sed "s/.*: '//g;s/'.*//")" -wget $PROXY_URL -O $PV_CACHE +wget "$PROXY_URL" -O "$PV_CACHE" echo "ls $PV_CACHE" \ No newline at end of file diff --git a/tools/run.sh b/tools/run.sh index 339ea7e..24896ad 100755 --- a/tools/run.sh +++ b/tools/run.sh @@ -41,21 +41,21 @@ _cleanup() { jekyll clean fi - rm -rf ${WORK_DIR}/${CONTAINER} + rm -rf "${WORK_DIR}/${CONTAINER}" ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1 } _init() { - if [[ -d ${WORK_DIR}/${CONTAINER} ]]; then - rm -rf ${WORK_DIR}/${CONTAINER} + if [[ -d "${WORK_DIR}/${CONTAINER}" ]]; then + rm -rf "${WORK_DIR}/${CONTAINER}" fi - temp=$(mktemp -d) - cp -r ${WORK_DIR}/* $temp - cp -r ${WORK_DIR}/.git $temp - mv $temp ${WORK_DIR}/${CONTAINER} + temp="$(mktemp -d)" + cp -r "$WORK_DIR"/* "$temp" + cp -r "${WORK_DIR}/.git" "$temp" + mv "$temp" "${WORK_DIR}/${CONTAINER}" trap _cleanup INT } @@ -70,7 +70,7 @@ _check_unset() { _check_command() { - if [[ -z $(command -v $1) ]]; then + if [[ -z $(command -v "$1") ]]; then echo "Error: command '$1' not found !" echo "Hint: Get '$1' on <$2>" exit 1 @@ -81,16 +81,16 @@ _check_command() { main() { _init - cd ${WORK_DIR}/${CONTAINER} + cd "${WORK_DIR}/${CONTAINER}" bash _scripts/sh/create_pages.sh bash _scripts/sh/dump_lastmod.sh if [[ $realtime = true ]]; then - fswatch -0 -e "\\$CONTAINER" -e "\.git" ${WORK_DIR} | xargs -0 -I {} bash ./${SYNC_TOOL} {} $WORK_DIR . & + fswatch -0 -e "\\$CONTAINER" -e "\.git" "$WORK_DIR" | xargs -0 -I {} bash "./${SYNC_TOOL}" {} "$WORK_DIR" . & fi echo "\$ $cmd" - eval $cmd + eval "$cmd" } @@ -99,20 +99,20 @@ do opt="$1" case $opt in -H|--host) - _check_unset $2 + _check_unset "$2" cmd+=" -H $2" shift # past argument shift # past value ;; -P|--port) - _check_unset $2 + _check_unset "$2" cmd+=" -P $2" shift shift ;; -b|--baseurl) - _check_unset $2 - if [[ $2 == \/* ]] + _check_unset "$2" + if [[ "$2" == \/* ]] then cmd+=" -b $2" else @@ -127,7 +127,7 @@ do shift ;; -r|--realtime) - _check_command fswatch 'http://emcrisostomo.github.io/fswatch/' + _check_command fswatch "http://emcrisostomo.github.io/fswatch/" realtime=true shift ;;