分享一个日常使用的一段shell脚本

日常开发经常要简化很多操作可以用make 工具封装很多命令干活,但是在Docker 容器内有时候为了简化镜像的大小和避免风险都没有安装make,但是又需要用很多已命令的命令执行任务,索性就用shell封装好一个类似make 的模板,如下所示

#!/usr/bin/env bash
#/ PATH=./node_modules/.bin:$PATH
#/ https://www.tldp.org/LDP/abs/html/options.html
# Similar to -v (Print each command to stdout before executing it), but expands commands
# set -o xtrace
# set -o verbose
# Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
set -o errexit
# Apply to subprocesses too
shopt -s inherit_errexit 2>/dev/null || true
# Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.
set -o pipefail
# Attempt to use undefined variable outputs error message, and forces an exit
set -o nounset
# makes iterations and splitting less surprising
IFS=$'nt'
# full path current folder
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# full path of the script.sh (including the name)
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
# name of the script
__base="$(basename ${__file} .sh)"
# full path of the parent folder
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app
# Log function
# This disables and re-enables debug trace mode (only if it was already set)
# Sources: https://superuser.com/a/1338887/922762 -- https://github.com/Kurento/adm-scripts/blob/master/bash.conf.sh
shopt -s expand_aliases # This trick requires enabling aliases in Bash
BASENAME="$(basename "$0")" # Complete file name
echo_and_restore() {
echo "[${BASENAME}] $(cat -)"
# shellcheck disable=SC2154
case "$flags" in (*x*) set -x; esac
}
alias log='({ flags="$-"; set +x; } 2>/dev/null; echo_and_restore) <<<'
# Exit trap
# This runs at the end or, thanks to 'errexit', upon any error
on_exit() {
{ _RC="$?"; set +x; } 2>/dev/null
if ((_RC)); then log "ERROR ($_RC)"; else log "SUCCESS"; fi
log "#################### END ####################"
}
trap on_exit EXIT
log "==================== BEGIN ===================="
export appdir="$__dir"/
export SOURCE_FILES="$appdir tests"
function assert_env () {
source "$__dir"/".venv/bin/activate" || exit 1
echo "Pip location:"
pip_cmd=$(command -v pip)
echo "$pip_cmd"
current=$(pwd)
pip_path="$current/.venv/bin/pip"
echo "$pip_path"
if [[ "$pip_cmd" -ef "$pip_path" ]]; then
echo "paths match"
else
exit 1
fi
}
function clean () {
log "cleaning files"
find . -name '__pycache__' -exec rm -fr {} +;
find . -name '.ipynb_checkpoints' -exec rm -fr {} +;
find . -name '*.pyo' -exec rm -f {} +;
find . -name '*.pyc' -exec rm -f {} +;
find . -name '*.egg-info' -exec rm -fr {} +;
find . -name '*~' -exec rm -f {} +;
find . -name '*.egg' -exec rm -f {} +
}
function deps () {
assert_env
pip install pip-tools pip setuptools
pip-compile -v --allow-unsafe --output-file requirements/main.txt requirements/main.in &&
pip-compile -v --allow-unsafe --output-file requirements/dev.txt requirements/dev.in
}
function update () {
assert_env
# --build-isolation --generate-hashes
pip install --upgrade pip-tools pip setuptools
pip-compile -v --upgrade --allow-unsafe --output-file requirements/main.txt requirements/main.in &&
pip-compile -v --upgrade --allow-unsafe --output-file requirements/dev.txt requirements/dev.in
wait
pip-sync requirements/*.txt
}
function install {
assert_env
if ! command -v pip-sync; then echo "pip-tools not installed" && exit; fi
pip-sync requirements/*.txt
}
function lint () {
assert_env
autoflake --remove-all-unused-imports --recursive --remove-unused-variables --in-place "$appdir" --exclude=__init__.py
isort --profile black "$appdir"
black "$appdir"
}
function report () {
echo "Flake8 report:" > "$__dir"/code_report.txt
flake8 "$appdir" >> code_report.txt


推荐阅读