Improve the Shell code style.

This commit is contained in:
Cotes Chung 2020-08-19 17:05:58 +08:00
parent 038bccb3ca
commit 10ab9d3364
8 changed files with 86 additions and 85 deletions

View file

@ -20,15 +20,15 @@ tag_count=0
_read_yaml() { _read_yaml() {
local _endline=$(grep -n "\-\-\-" $1 | cut -d: -f 1 | sed -n '2p') local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
head -$_endline $1 head -"$_endline" "$1"
} }
read_categories() { read_categories() {
local _yaml=$(_read_yaml $1) local _yaml="$(_read_yaml "$1")"
local _categories=$(echo "$_yaml" | grep "^categories:") local _categories="$(echo "$_yaml" | grep "^categories:")"
local _category=$(echo "$_yaml" | grep "^category:") local _category="$(echo "$_yaml" | grep "^category:")"
if [[ ! -z "$_categories" ]]; then if [[ ! -z "$_categories" ]]; then
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
@ -39,7 +39,7 @@ read_categories() {
read_tags() { 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" echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
} }
@ -65,14 +65,14 @@ init() {
create_category() { create_category() {
if [[ ! -z $1 ]]; then if [[ ! -z $1 ]]; then
local _name=$1 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 if [[ ! -f "$_filepath" ]]; then
echo "---" > $_filepath echo "---" > "$_filepath"
echo "layout: category" >> $_filepath echo "layout: category" >> "$_filepath"
echo "title: $_name" >> $_filepath echo "title: $_name" >> "$_filepath"
echo "category: $_name" >> $_filepath echo "category: $_name" >> "$_filepath"
echo "---" >> $_filepath echo "---" >> "$_filepath"
((category_count=category_count+1)) ((category_count=category_count+1))
fi fi
@ -83,15 +83,15 @@ create_category() {
create_tag() { create_tag() {
if [[ ! -z $1 ]]; then if [[ ! -z $1 ]]; then
local _name=$1 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 "---" > "$_filepath"
echo "layout: tag" >> $_filepath echo "layout: tag" >> "$_filepath"
echo "title: $_name" >> $_filepath echo "title: $_name" >> "$_filepath"
echo "tag: $_name" >> $_filepath echo "tag: $_name" >> "$_filepath"
echo "---" >> $_filepath echo "---" >> "$_filepath"
((tag_count=tag_count+1)) ((tag_count=tag_count+1))
fi fi
@ -116,13 +116,13 @@ create_pages() {
$TYPE_CATEGORY) $TYPE_CATEGORY)
for i in ${_string#,}; do for i in ${_string#,}; do
create_category $i create_category "$i"
done done
;; ;;
$TYPE_TAG) $TYPE_TAG)
for i in ${_string#,}; do for i in ${_string#,}; do
create_tag $i create_tag "$i"
done done
;; ;;

View file

@ -35,8 +35,8 @@ _init() {
_has_changed() { _has_changed() {
local _log_count=`git log --pretty=%ad $1 | wc -l | sed 's/ *//'` local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
_log_count=$(($_log_count + 0)) _log_count=$((_log_count + 0))
if [[ $_log_count > 1 ]]; then if [[ $_log_count > 1 ]]; then
return 0 # true return 0 # true
@ -56,7 +56,7 @@ _has_changed() {
# the file '_data/updates.yml' # the file '_data/updates.yml'
################################### ###################################
_dump() { _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 if [[ ! -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then
touch "$OUTPUT_DIR/$OUTPUT_FILE" touch "$OUTPUT_DIR/$OUTPUT_FILE"
@ -76,7 +76,7 @@ main() {
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)) for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \))
do 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 if _has_changed "$_file"; then
_dump "$_filename" "$_file" _dump "$_filename" "$_file"

View file

@ -11,28 +11,28 @@
# $3 -> the destination sync directory # $3 -> the destination sync directory
# Omit the system temp file # Omit the system temp file
if [[ ! -f $1 ]]; then if [[ ! -f "$1" ]]; then
exit 0 exit 0
fi 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}}" related_dir="${src_dir:${#dir_prefix}}"
dest="$(realpath $3)/${related_dir}" dest="$(realpath "$3")/${related_dir}"
if [[ ! -d "$dest" ]]; then if [[ ! -d "$dest" ]]; then
mkdir -p "$dest" mkdir -p "$dest"
fi fi
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
cp $1 $dest cp "$1" "$dest"
fi fi
if [[ $related_dir == "_posts" ]]; then if [[ "$related_dir" == "_posts" ]]; then
bash $3/_scripts/sh/create_pages.sh bash "$3"/_scripts/sh/create_pages.sh
bash $3/_scripts/sh/dump_lastmod.sh bash "$3"/_scripts/sh/dump_lastmod.sh
fi fi

View file

@ -10,11 +10,11 @@ set -eu
CMD="JEKYLL_ENV=production bundle exec jekyll b" 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() { _help() {
@ -30,44 +30,44 @@ _help() {
_init() { _init() {
cd $WORK_DIR cd "$WORK_DIR"
if [[ -d $CONTAINER ]]; then if [[ -d "$CONTAINER" ]]; then
rm -rf $CONTAINER rm -rf "$CONTAINER"
fi fi
if [[ -d _site ]]; then if [[ -d "_site" ]]; then
jekyll clean jekyll clean
fi fi
local _temp=$(mktemp -d) local _temp="$(mktemp -d)"
cp -r * $_temp cp -r ./* "$_temp"
cp -r .git $_temp cp -r ./.git "$_temp"
mv $_temp $CONTAINER mv "$_temp" "$CONTAINER"
} }
_build() { _build() {
cd $CONTAINER cd "$CONTAINER"
echo "$ cd $(pwd)" echo "$ cd $(pwd)"
bash _scripts/sh/create_pages.sh bash "_scripts/sh/create_pages.sh"
bash _scripts/sh/dump_lastmod.sh bash "_scripts/sh/dump_lastmod.sh"
CMD+=" -d ${DEST}" CMD+=" -d $DEST"
echo "\$ $CMD" echo "\$ $CMD"
eval $CMD eval "$CMD"
echo -e "\nBuild success, the site files have been placed in '${DEST}'." echo -e "\nBuild success, the site files have been placed in '${DEST}'."
if [[ -d ${DEST}/.git ]]; then if [[ -d "${DEST}/.git" ]]; then
if [[ ! -z $(git -C $DEST status -s) ]]; then if [[ ! -z $(git -C "$DEST" status -s) ]]; then
git -C $DEST add . git -C "$DEST" add .
git -C $DEST commit -m "[Automation] Update site files." -q git -C "$DEST" commit -m "[Automation] Update site files." -q
echo -e "\nPlease push the changes of $DEST to remote master branch.\n" echo -e "\nPlease push the changes of $DEST to remote master branch.\n"
fi fi
fi fi
cd .. && rm -rf $CONTAINER cd .. && rm -rf "$CONTAINER"
} }
@ -95,8 +95,8 @@ main() {
shift shift
;; ;;
-d|--destination) -d|--destination)
_check_unset $2 _check_unset "$2"
DEST=$(realpath $2) DEST="$(realpath "$2")"
shift; shift;
shift; shift;
;; ;;

View file

@ -14,17 +14,18 @@ PAGES_BRANCH="gh-pages"
_no_branch=false _no_branch=false
if [[ -z `git branch -av | grep $PAGES_BRANCH` ]]; then if [[ -z $(git branch -av | grep "$PAGES_BRANCH") ]]; then
_no_branch=true _no_branch=true
git checkout -b $PAGES_BRANCH git checkout -b "$PAGES_BRANCH"
else else
git checkout $PAGES_BRANCH git checkout "$PAGES_BRANCH"
fi fi
mv _site ../ mv _site ../
mv .git ../ mv .git ../
rm -rf * && rm -rf .[^.] .??* rm -rf ./*
rm -rf .[^.] .??*
mv ../_site/* . mv ../_site/* .
mv ../.git . mv ../.git .

View file

@ -13,12 +13,12 @@ CATEGORIES=false
TAGS=false TAGS=false
LASTMOD=false LASTMOD=false
WORK_DIR=$(dirname $(dirname $(realpath "$0"))) WORK_DIR="$(dirname $(dirname $(realpath "$0")))"
check_status() { check_status() {
local _change=$(git status . -s) local _change=$(git status . -s)
if [[ ! -z ${_change} ]]; then if [[ ! -z $_change ]]; then
echo "Warning: Commit the following changes first:" echo "Warning: Commit the following changes first:"
echo "$_change" echo "$_change"
exit 1 exit 1
@ -81,7 +81,7 @@ push() {
main() { main() {
cd $WORK_DIR cd "$WORK_DIR"
check_status check_status

View file

@ -15,12 +15,12 @@
set -eu set -eu
WORK_DIR=$(dirname $(dirname $(realpath "$0"))) WORK_DIR="$(dirname $(dirname $(realpath "$0")))"
URL_FILE=${WORK_DIR}/_config.yml URL_FILE="${WORK_DIR}/_config.yml"
PV_CACHE=${WORK_DIR}/assets/js/data/pageviews.json 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" echo "ls $PV_CACHE"

View file

@ -41,21 +41,21 @@ _cleanup() {
jekyll clean jekyll clean
fi 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 ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
} }
_init() { _init() {
if [[ -d ${WORK_DIR}/${CONTAINER} ]]; then if [[ -d "${WORK_DIR}/${CONTAINER}" ]]; then
rm -rf ${WORK_DIR}/${CONTAINER} rm -rf "${WORK_DIR}/${CONTAINER}"
fi fi
temp=$(mktemp -d) temp="$(mktemp -d)"
cp -r ${WORK_DIR}/* $temp cp -r "$WORK_DIR"/* "$temp"
cp -r ${WORK_DIR}/.git $temp cp -r "${WORK_DIR}/.git" "$temp"
mv $temp ${WORK_DIR}/${CONTAINER} mv "$temp" "${WORK_DIR}/${CONTAINER}"
trap _cleanup INT trap _cleanup INT
} }
@ -70,7 +70,7 @@ _check_unset() {
_check_command() { _check_command() {
if [[ -z $(command -v $1) ]]; then if [[ -z $(command -v "$1") ]]; then
echo "Error: command '$1' not found !" echo "Error: command '$1' not found !"
echo "Hint: Get '$1' on <$2>" echo "Hint: Get '$1' on <$2>"
exit 1 exit 1
@ -81,16 +81,16 @@ _check_command() {
main() { main() {
_init _init
cd ${WORK_DIR}/${CONTAINER} cd "${WORK_DIR}/${CONTAINER}"
bash _scripts/sh/create_pages.sh bash _scripts/sh/create_pages.sh
bash _scripts/sh/dump_lastmod.sh bash _scripts/sh/dump_lastmod.sh
if [[ $realtime = true ]]; then 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 fi
echo "\$ $cmd" echo "\$ $cmd"
eval $cmd eval "$cmd"
} }
@ -99,20 +99,20 @@ do
opt="$1" opt="$1"
case $opt in case $opt in
-H|--host) -H|--host)
_check_unset $2 _check_unset "$2"
cmd+=" -H $2" cmd+=" -H $2"
shift # past argument shift # past argument
shift # past value shift # past value
;; ;;
-P|--port) -P|--port)
_check_unset $2 _check_unset "$2"
cmd+=" -P $2" cmd+=" -P $2"
shift shift
shift shift
;; ;;
-b|--baseurl) -b|--baseurl)
_check_unset $2 _check_unset "$2"
if [[ $2 == \/* ]] if [[ "$2" == \/* ]]
then then
cmd+=" -b $2" cmd+=" -b $2"
else else
@ -127,7 +127,7 @@ do
shift shift
;; ;;
-r|--realtime) -r|--realtime)
_check_command fswatch 'http://emcrisostomo.github.io/fswatch/' _check_command fswatch "http://emcrisostomo.github.io/fswatch/"
realtime=true realtime=true
shift shift
;; ;;