這篇主要會觀摩 vue-good-table-next 的 release shell script 內容
主要用到這五個指令
set
grep
read
sed
if ... fi
用法:
set -options arguments
# use to exit when the command exits with a non-zero status
# 指令回傳值若不等於 0,則立即退出 shell
# 當錯誤發生,用來中斷程式
set -e
用來搜尋匹配 pattern 的字串,支援 Regular Expression
用法:
grep [options] pattern [files]
# 例如 grep "string" file name
假使有一個 test.txt 檔案內容
Welcome to Linux !
Linux is a free and opensource Operating system that is mostly used by
developers and in production servers for hosting crucial components such as web
and database servers. Linux has also made a name for itself in PCs.
Beginners looking to experiment with Linux can get started with friendlier linux
distributions such as Ubuntu, Mint, Fedora and Elementary OS.
執行
grep "Linux" test.txt # 搜尋檔案中 Linux 字串
會印出整行文字,並把搜尋的關鍵字 highlight !
如果關鍵字沒有出現顏色,可以加上 --color
grep --color "Linux" test.txt # 搜尋
如果要搜尋整個目錄下的檔案 (包含子目錄),可以使用 -r
(recursively)
grep -r "linux" *
忽略搜尋關鍵字的大小寫,使用 -i
grep -i "linux" welcome.txt
以上 grep 範例摘自 Grep Command in Linux/UNIX
從輸入中讀取一行,把輸入行的每個欄位的值指定給 shell 變數
# read 用來讀取螢幕輸入或是讀取檔案內容
read -p "please input you name: " firstname #獲取輸入變數,存入 firstname
read -p "please input you age: " age #獲取輸入變數,存入 age
echo "you name is $firstname ,age is $age" #輸出變數內容
-p
(prompt): 輸入文字前,會印出的訊息內容-s
(secret/silent): 隱藏使用者輸入的文字,例如輸入密碼時-n
(number): 限制輸入文字的長度-t
(timeout): 限制使用者輸入文字的時間
read –p "Please type your username" username
read –s –p "Please type your password" $password
read –n 8 –p “Please type your username not exceeding 8 characters” username
echo "What is the capital of Japan? Answer in 5 seconds"
read -t 5 answer # 等待使用者 5 秒內輸入
if [ "$answer" = "tokyo" ] || [ "$answer" = "Tokyo" ];
then
echo "Your answer is Correct!"
else
echo "Your answer is Wrong!"
fi
以上 read 範例摘自How to Use the read Command in Bash
Stream editor
處理文字,也支援 Regular Expression
sed 一次處理一行,主要用來搜尋、取代、刪除檔案中的文字
取代用法:
# 會取代每一行第一個出現的 old_string
sed 's/old_string/new_string/' filename.txt
# 取代每一行所有的 old_string
sed 's/old_string/new_string/g' filename.txt
刪除用法:
# 刪除指定行數
# 用法: sed '[first line to delete] [last line to delete] d' test.txt
sed '2,4 d' test.txt # 刪除 test.txt 檔案中第2行到第4行
# 匹配 Regex 規則的刪除
# 用法: sed '/regex/ d' test.txt
sed '/^#/ d' test.txt # 刪除#開頭的每一行
用法:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
結果會印出
a is not equal to b
以上 if...fi 範例摘自Unix / Linux Shell - The if...fi statement
拆解 release.sh
的內容
set -e
# 搜尋 package.json 檔案中有 "version" 出現的那一行
# 使用 Regular Expression 擷取出目前版本號
echo "Current version:" $(grep version package.json | sed -E 's/^.*"([0-9][^"]+)".*$/\1/')
echo "Enter version e.g., 4.0.1: "
# 讀取使用者輸入的版本號
read VERSION
# 再次詢問新版本號是否正確 (-n 1 限制一個字元)
read -p "Releasing v$VERSION - are you sure? (y/n)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Releasing v$VERSION ..."
# clear existing ts cache
rm -rf node_modules/.rts2_cache
# generate the version so that the changelog can be generated too
yarn version --no-git-tag-version --no-commit-hooks --new-version $VERSION
yarn run build
yarn run build:dts
yarn run test:dts
# 自動產生 changelog
yarn run changelog
yarn prettier --write CHANGELOG.md
echo "Please check the git history and the changelog and press enter"
read OKAY
# commit and tag (加入異動的兩隻檔案 CHANGELOG.md 和 package.json)
git add CHANGELOG.md package.json
git commit -m "release: v$VERSION"
git tag "v$VERSION"
# commit
yarn publish --tag next --new-version "$VERSION" --no-commit-hooks --no-git-tag-version
# publish (push version 標籤 以及 檔案變更)
git push origin refs/tags/v$VERSION
git push
fi
過去還沒有體驗到 Shell Script
的好,只覺得
: 都是純文字的介面,好不友善
: 指令好多,參數好多...
: 不學應該沒有關係
但... 幾年過去,覺得真的好方便
: 下指令比起圖形化介面快很多
: 可以在一個 Terminal 做所有的事
: 可以客製化 Terminal,要什麽配色要什麽字體都可以
慢慢的恐懼感好像降低不少
把原本繁雜人工作業都寫進 shell script 中
省下時間外,也避免人工失誤
sed -i命令詳解及入門攻略
sed 官方文件
Linux sed Command: How To Use the Stream Editor
Linux sed command