Skip to content

Instantly share code, notes, and snippets.

@Herbert8
Last active August 1, 2019 14:07
Show Gist options
  • Save Herbert8/a4166c07b47b739001493919565c819f to your computer and use it in GitHub Desktop.
Save Herbert8/a4166c07b47b739001493919565c819f to your computer and use it in GitHub Desktop.
获取脚本位置
#!/bin/bash
# Bash 脚本 set 命令教程
# http://www.ruanyifeng.com/blog/2017/11/bash-set.html
set -o errexit
set -o xtrace
set -o nounset
set -o pipefail
# 脚本所在路径作为基础路径
# 判断系统类型
UNAME=$(uname)
BASH_SOURCE_NAME=${BASH_SOURCE[0]}
# 兼容 Linux 的场景
if [[ "${UNAME}" == "Linux" ]]; then
SCRIPT_FILE=$(readlink -f "${BASH_SOURCE_NAME}")
fi
# 兼容 macOS 的场景
if [[ "${UNAME}" == "Darwin" ]]; then
if [[ $(echo ${BASH_SOURCE_NAME} | awk '/^\//') == ${BASH_SOURCE_NAME} ]]; then
SCRIPT_FILE=${BASH_SOURCE_NAME}
else
SCRIPT_FILE=$PWD/${BASH_SOURCE_NAME}
fi
fi
# 根据脚本名获取脚本所在文件夹
BASE_PATH_SCRIPT=$(dirname "${SCRIPT_FILE}")
# 以当前时间作为时间戳
TIMESTAMP_CMD='date +%Y%m%d%H%M%S'
TIMESTAMP=$($TIMESTAMP_CMD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment