前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享一个日常使用的一段shell脚本

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

作者头像
IT大咖说
发布2022-08-26 14:18:10
2660
发布2022-08-26 14:18:10
举报
文章被收录于专栏:IT大咖说IT大咖说

日常开发经常要简化很多操作可以用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=$'\n\t' # 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 echo "Bandit report:" >> code_report.txt bandit -r "$appdir" >> "$__dir"/code_report.txt } function publish () { assert_env if ! command -v twine &>/dev/null ; then echo "Unable to find the 'twine' command." echo "Install from PyPI, using 'pip install twine'." exit 1 fi if ! pip show wheel &>/dev/null ; then echo "Unable to find the 'wheel' command." echo "Install from PyPI, using 'pip install wheel'." exit 1 fi clean log "building package" python3 setup.py sdist bdist_wheel log "uploading to PyPi" python3 -m twine upload dist/* log "cleaning..." clean echo "You probably want to also tag the version now:" echo "git tag -a ${VERSION} -m 'version ${VERSION}'" echo "git push --tags" } function build () { echo "build task not implemented" } function tester () { echo "not implemented" assert_env # pytest ... } function buildprod { echo "build task not implemented" assert_env # this runs in parallel format & deps & tester & wait echo "not implemented" # shiv ... } function run { echo "running app with uvicorn" assert_env uvicorn --workers 2 app.main:app --reload --reload-dir "$appdir" } function default { # start clean } function tasks { echo "$0 <task> <args>" echo "Tasks:" compgen -A function | cat -n } # Help message (extracted from script headers) usage() { grep '^#/' "$0" | cut --characters=4-; exit 0; } REGEX='(^|\W)(-h|--help)($|\W)' [[ "$*" =~ $REGEX ]] && usage || true TIMEFORMAT="Task completed in %3lR" time ${@:-default}

来源:

https://www.toutiao.com/article/7117773212569616929/?log_from=c70723eb58584_1660185655838

“IT大咖说”欢迎广大技术人员投稿,投稿邮箱:aliang@itdks.com

来都来了,走啥走,留个言呗~

 IT大咖说  |  关于版权

由“IT大咖说(ID:itdakashuo)”原创的文章,转载时请注明作者、出处及微信公众号。投稿、约稿、转载请加微信:ITDKS10(备注:投稿),茉莉小姐姐会及时与您联系!

感谢您对IT大咖说的热心支持!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 IT大咖说 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档