From ce4c0f1d1a8db78699606fe03d110b7fad529b18 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Mon, 16 Mar 2026 19:06:17 +0800 Subject: [PATCH] =?UTF-8?q?dot:=20bash=20add-to-path=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20-s=20=E9=9D=99=E9=BB=98=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dot/.bashrc.d/android-studio.bash | 6 ++++-- dot/.bashrc.d/function.bash | 33 +++++++++++++++++++++++++++---- dot/.bashrc.d/pnpm.bash | 2 +- dot/.bashrc.d/volta.bash | 2 -- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/dot/.bashrc.d/android-studio.bash b/dot/.bashrc.d/android-studio.bash index a1fbd7f..2fb75cf 100644 --- a/dot/.bashrc.d/android-studio.bash +++ b/dot/.bashrc.d/android-studio.bash @@ -5,7 +5,9 @@ unset basedir export JAVA_HOME="/opt/android-studio/jbr" export ANDROID_HOME="${HOME}/Android/Sdk" -export NDK_HOME="$ANDROID_HOME/ndk/$(ls -1 $ANDROID_HOME/ndk)" -add-to-path "${ANDROID_HOME}/emulator" "${ANDROID_HOME}/platform-tools" +if [[ -d "${ANDROID_HOME}/ndk" ]]; then + export NDK_HOME="$ANDROID_HOME/ndk/$(ls -1 $ANDROID_HOME/ndk)" +fi +add-to-path -s "${ANDROID_HOME}/emulator" "${ANDROID_HOME}/platform-tools" diff --git a/dot/.bashrc.d/function.bash b/dot/.bashrc.d/function.bash index 3fef356..3714360 100644 --- a/dot/.bashrc.d/function.bash +++ b/dot/.bashrc.d/function.bash @@ -31,13 +31,36 @@ generate-bash-completion() { } add-to-path() { - if [[ $# -eq 0 ]]; then - echo "usage: add-to-path [path2 ...]" >&2 + local silence=false + local -a dirs=() + + while [[ $# -gt 0 ]]; do + case "$1" in + -s) + silence=true + shift + ;; + --) + shift + dirs+=("$@") + break + ;; + *) + dirs+=("$1") + shift + ;; + esac + done + + if [[ ${#dirs[@]} -eq 0 ]]; then + echo "usage: add-to-path [-s] [path2 ...]" >&2 + echo "" >&2 + echo " -s: if some path not exist, just skip it silently" >&2 + echo "" >&2 echo "example: add-to-path /usr/local/bin /tmp/my-dir" >&2 return 1 fi - local -a dirs=("$@") local dir local dir_normalized local new_path="$PATH" @@ -47,7 +70,9 @@ add-to-path() { dir_normalized="${dir%/}" if [[ ! -d "$dir_normalized" ]]; then - echo "WARNING: '${dir}' is not a valid directory, skipped." >&2 + if [[ "$silence" == false ]]; then + echo "WARNING: '${dir}' is not a valid directory, skipped." >&2 + fi continue fi diff --git a/dot/.bashrc.d/pnpm.bash b/dot/.bashrc.d/pnpm.bash index 57633fd..5929129 100755 --- a/dot/.bashrc.d/pnpm.bash +++ b/dot/.bashrc.d/pnpm.bash @@ -4,7 +4,7 @@ source "${basedir}/function.bash" unset basedir export PNPM_HOME="${HOME}/.local/share/pnpm" -add-to-path $PNPM_HOME +add-to-path -s $PNPM_HOME if [[ -x "$(type -p pnpm)" ]]; then generate-bash-completion pnpm "pnpm completion bash" diff --git a/dot/.bashrc.d/volta.bash b/dot/.bashrc.d/volta.bash index 8d3d572..a0b2628 100644 --- a/dot/.bashrc.d/volta.bash +++ b/dot/.bashrc.d/volta.bash @@ -21,5 +21,3 @@ if [[ -x $(type -p volta) || -x "${VOLTA_HOME}/bin/volta" ]]; then unhash-command-in-dirs "$bin_dir" } fi - -