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() {
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
;;

View File

@ -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"

View File

@ -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

View File

@ -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;
;;

View File

@ -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 .

View File

@ -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

View File

@ -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"

View File

@ -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
;;