#!/usr/bin/env bash git_clone() { local repo=$1 local dir=$2 if [ ! -d "$dir" ]; then if [ "$3" = "--depth=1" ]; then git clone --depth=1 "$github/$repo" $dir elif [ "$3" = "--norecursive" ]; then git clone --single-branch "$github/$repo" $dir else git clone --single-branch --recursive "$github/$repo" $dir fi fi } # offline: 是否离线 # abroad: 是否海外 check_internet_access() { offline=0 local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.baidu.com") if [ "$response" -ne 200 ]; then offline=1 echo "Internet: offline" return fi abroad=0 local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.google.com") if [ "$response" -eq 200 ]; then abroad=1 echo "Internet: abroad" github="https://github.com" else abroad=0 echo "Internet: internal" github="https://g.nano.ac/https://github.com" fi } confirm_action() { local prompt="$1" local default_response="$2" read -p "$prompt [$default_response]: " response if [[ -z "$response" ]]; then response="$default_response" fi [[ $response =~ ^[Yy]$ ]] } command_exists() { command -v "$@" >/dev/null 2>&1 } pkg_install() { # 如果已经安装了,就直接返回(只检查第一个) command_exists "$1" && return if [ "$(uname)" = "Darwin" ]; then brew install "$@" else sudo apt-get install "$@" -y fi } read_password() { echo -n "Password: " password="" while IFS= read -r -s -n1 char; do if [[ $char == $'\0' ]]; then break elif [[ $char == $'\177' ]]; then if [ -n "$password" ]; then password=${password%?} echo -ne "\b \b" fi else password+="$char" echo -n "*" fi done echo } # ============================================================ check_internet_access