Skip to content

Commit fdc1ca6

Browse files
committed
Improve the Shell code style.
1 parent 1e60d40 commit fdc1ca6

8 files changed

Lines changed: 86 additions & 85 deletions

File tree

_scripts/sh/create_pages.sh

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ tag_count=0
2020

2121

2222
_read_yaml() {
23-
local _endline=$(grep -n "\-\-\-" $1 | cut -d: -f 1 | sed -n '2p')
24-
head -$_endline $1
23+
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
24+
head -"$_endline" "$1"
2525
}
2626

2727

2828
read_categories() {
29-
local _yaml=$(_read_yaml $1)
30-
local _categories=$(echo "$_yaml" | grep "^categories:")
31-
local _category=$(echo "$_yaml" | grep "^category:")
29+
local _yaml="$(_read_yaml "$1")"
30+
local _categories="$(echo "$_yaml" | grep "^categories:")"
31+
local _category="$(echo "$_yaml" | grep "^category:")"
3232

3333
if [[ ! -z "$_categories" ]]; then
3434
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
@@ -39,7 +39,7 @@ read_categories() {
3939

4040

4141
read_tags() {
42-
local _yaml=$(_read_yaml $1)
42+
local _yaml="$(_read_yaml "$1")"
4343
echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
4444
}
4545

@@ -65,14 +65,14 @@ init() {
6565
create_category() {
6666
if [[ ! -z $1 ]]; then
6767
local _name=$1
68-
local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html"
68+
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
6969

70-
if [[ ! -f $_filepath ]]; then
71-
echo "---" > $_filepath
72-
echo "layout: category" >> $_filepath
73-
echo "title: $_name" >> $_filepath
74-
echo "category: $_name" >> $_filepath
75-
echo "---" >> $_filepath
70+
if [[ ! -f "$_filepath" ]]; then
71+
echo "---" > "$_filepath"
72+
echo "layout: category" >> "$_filepath"
73+
echo "title: $_name" >> "$_filepath"
74+
echo "category: $_name" >> "$_filepath"
75+
echo "---" >> "$_filepath"
7676

7777
((category_count=category_count+1))
7878
fi
@@ -83,15 +83,15 @@ create_category() {
8383
create_tag() {
8484
if [[ ! -z $1 ]]; then
8585
local _name=$1
86-
local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
86+
local _filepath="tags/$( echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
8787

88-
if [[ ! -f $_filepath ]]; then
88+
if [[ ! -f "$_filepath" ]]; then
8989

90-
echo "---" > $_filepath
91-
echo "layout: tag" >> $_filepath
92-
echo "title: $_name" >> $_filepath
93-
echo "tag: $_name" >> $_filepath
94-
echo "---" >> $_filepath
90+
echo "---" > "$_filepath"
91+
echo "layout: tag" >> "$_filepath"
92+
echo "title: $_name" >> "$_filepath"
93+
echo "tag: $_name" >> "$_filepath"
94+
echo "---" >> "$_filepath"
9595

9696
((tag_count=tag_count+1))
9797
fi
@@ -116,13 +116,13 @@ create_pages() {
116116

117117
$TYPE_CATEGORY)
118118
for i in ${_string#,}; do
119-
create_category $i
119+
create_category "$i"
120120
done
121121
;;
122122

123123
$TYPE_TAG)
124124
for i in ${_string#,}; do
125-
create_tag $i
125+
create_tag "$i"
126126
done
127127
;;
128128

_scripts/sh/dump_lastmod.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ _init() {
3535

3636

3737
_has_changed() {
38-
local _log_count=`git log --pretty=%ad $1 | wc -l | sed 's/ *//'`
39-
_log_count=$(($_log_count + 0))
38+
local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
39+
_log_count=$((_log_count + 0))
4040

4141
if [[ $_log_count > 1 ]]; then
4242
return 0 # true
@@ -56,7 +56,7 @@ _has_changed() {
5656
# the file '_data/updates.yml'
5757
###################################
5858
_dump() {
59-
local _lasmod="`git log -1 --pretty=%ad --date=iso $2`"
59+
local _lasmod="$(git log -1 --pretty=%ad --date=iso "$2")"
6060

6161
if [[ ! -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then
6262
touch "$OUTPUT_DIR/$OUTPUT_FILE"
@@ -76,7 +76,7 @@ main() {
7676

7777
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \))
7878
do
79-
_filename=$(basename $_file | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' ) # remove date and extension
79+
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' )" # remove date and extension
8080

8181
if _has_changed "$_file"; then
8282
_dump "$_filename" "$_file"

_scripts/sh/sync_monitor.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
# $3 -> the destination sync directory
1212

1313
# Omit the system temp file
14-
if [[ ! -f $1 ]]; then
14+
if [[ ! -f "$1" ]]; then
1515
exit 0
1616
fi
1717

18-
src_dir=`dirname $(realpath $1)`
18+
src_dir="$(dirname $(realpath "$1"))"
1919

20-
dir_prefix="$(realpath $2)/"
20+
dir_prefix="$(realpath "$2")/"
2121

2222
related_dir="${src_dir:${#dir_prefix}}"
2323

2424

25-
dest="$(realpath $3)/${related_dir}"
25+
dest="$(realpath "$3")/${related_dir}"
2626

2727
if [[ ! -d "$dest" ]]; then
2828
mkdir -p "$dest"
2929
fi
3030

3131
if [[ -f "$1" ]]; then
32-
cp $1 $dest
32+
cp "$1" "$dest"
3333
fi
3434

35-
if [[ $related_dir == "_posts" ]]; then
36-
bash $3/_scripts/sh/create_pages.sh
37-
bash $3/_scripts/sh/dump_lastmod.sh
35+
if [[ "$related_dir" == "_posts" ]]; then
36+
bash "$3"/_scripts/sh/create_pages.sh
37+
bash "$3"/_scripts/sh/dump_lastmod.sh
3838
fi

tools/build.sh

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ set -eu
1010

1111
CMD="JEKYLL_ENV=production bundle exec jekyll b"
1212

13-
WORK_DIR=$(dirname $(dirname $(realpath "$0")))
13+
WORK_DIR="$(dirname $(dirname $(realpath "$0")))"
1414

15-
CONTAINER=${WORK_DIR}/.container
15+
CONTAINER="${WORK_DIR}/.container"
1616

17-
DEST=${WORK_DIR}/_site
17+
DEST="${WORK_DIR}/_site"
1818

1919

2020
_help() {
@@ -30,44 +30,44 @@ _help() {
3030

3131

3232
_init() {
33-
cd $WORK_DIR
33+
cd "$WORK_DIR"
3434

35-
if [[ -d $CONTAINER ]]; then
36-
rm -rf $CONTAINER
35+
if [[ -d "$CONTAINER" ]]; then
36+
rm -rf "$CONTAINER"
3737
fi
3838

39-
if [[ -d _site ]]; then
39+
if [[ -d "_site" ]]; then
4040
jekyll clean
4141
fi
4242

43-
local _temp=$(mktemp -d)
44-
cp -r * $_temp
45-
cp -r .git $_temp
46-
mv $_temp $CONTAINER
43+
local _temp="$(mktemp -d)"
44+
cp -r ./* "$_temp"
45+
cp -r ./.git "$_temp"
46+
mv "$_temp" "$CONTAINER"
4747
}
4848

4949

5050
_build() {
51-
cd $CONTAINER
51+
cd "$CONTAINER"
5252
echo "$ cd $(pwd)"
5353

54-
bash _scripts/sh/create_pages.sh
55-
bash _scripts/sh/dump_lastmod.sh
54+
bash "_scripts/sh/create_pages.sh"
55+
bash "_scripts/sh/dump_lastmod.sh"
5656

57-
CMD+=" -d ${DEST}"
57+
CMD+=" -d $DEST"
5858
echo "\$ $CMD"
59-
eval $CMD
59+
eval "$CMD"
6060
echo -e "\nBuild success, the site files have been placed in '${DEST}'."
6161

62-
if [[ -d ${DEST}/.git ]]; then
63-
if [[ ! -z $(git -C $DEST status -s) ]]; then
64-
git -C $DEST add .
65-
git -C $DEST commit -m "[Automation] Update site files." -q
62+
if [[ -d "${DEST}/.git" ]]; then
63+
if [[ ! -z $(git -C "$DEST" status -s) ]]; then
64+
git -C "$DEST" add .
65+
git -C "$DEST" commit -m "[Automation] Update site files." -q
6666
echo -e "\nPlease push the changes of $DEST to remote master branch.\n"
6767
fi
6868
fi
6969

70-
cd .. && rm -rf $CONTAINER
70+
cd .. && rm -rf "$CONTAINER"
7171
}
7272

7373

@@ -95,8 +95,8 @@ main() {
9595
shift
9696
;;
9797
-d|--destination)
98-
_check_unset $2
99-
DEST=$(realpath $2)
98+
_check_unset "$2"
99+
DEST="$(realpath "$2")"
100100
shift;
101101
shift;
102102
;;

tools/deploy.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ PAGES_BRANCH="gh-pages"
1414

1515
_no_branch=false
1616

17-
if [[ -z `git branch -av | grep $PAGES_BRANCH` ]]; then
17+
if [[ -z $(git branch -av | grep "$PAGES_BRANCH") ]]; then
1818
_no_branch=true
19-
git checkout -b $PAGES_BRANCH
19+
git checkout -b "$PAGES_BRANCH"
2020
else
21-
git checkout $PAGES_BRANCH
21+
git checkout "$PAGES_BRANCH"
2222
fi
2323

2424
mv _site ../
2525
mv .git ../
2626

27-
rm -rf * && rm -rf .[^.] .??*
27+
rm -rf ./*
28+
rm -rf .[^.] .??*
2829

2930
mv ../_site/* .
3031
mv ../.git .

tools/publish.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ CATEGORIES=false
1313
TAGS=false
1414
LASTMOD=false
1515

16-
WORK_DIR=$(dirname $(dirname $(realpath "$0")))
16+
WORK_DIR="$(dirname $(dirname $(realpath "$0")))"
1717

1818
check_status() {
1919
local _change=$(git status . -s)
2020

21-
if [[ ! -z ${_change} ]]; then
21+
if [[ ! -z $_change ]]; then
2222
echo "Warning: Commit the following changes first:"
2323
echo "$_change"
2424
exit 1
@@ -81,7 +81,7 @@ push() {
8181

8282
main() {
8383

84-
cd $WORK_DIR
84+
cd "$WORK_DIR"
8585

8686
check_status
8787

tools/pv.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
set -eu
1717

18-
WORK_DIR=$(dirname $(dirname $(realpath "$0")))
19-
URL_FILE=${WORK_DIR}/_config.yml
20-
PV_CACHE=${WORK_DIR}/assets/js/data/pageviews.json
18+
WORK_DIR="$(dirname $(dirname $(realpath "$0")))"
19+
URL_FILE="${WORK_DIR}/_config.yml"
20+
PV_CACHE="${WORK_DIR}/assets/js/data/pageviews.json"
2121

22-
PROXY_URL=$(grep "proxy_endpoint:" $URL_FILE | sed "s/.*: '//g;s/'.*//")
22+
PROXY_URL="$(grep "proxy_endpoint:" "$URL_FILE" | sed "s/.*: '//g;s/'.*//")"
2323

24-
wget $PROXY_URL -O $PV_CACHE
24+
wget "$PROXY_URL" -O "$PV_CACHE"
2525

2626
echo "ls $PV_CACHE"

0 commit comments

Comments
 (0)