From 51ef409d276e4e0b4a36901497c8bea0314a5e22 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 8 Sep 2023 01:45:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20use=20`${array[@]:-}`/`${array[@]:+?= =?UTF-8?q?}`=20simplify=20codes=20=F0=9F=9A=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/ap | 3 ++- bin/find-in-jars | 4 +--- bin/uq | 2 +- bin/xpl | 12 +++++++----- legacy-bin/swtrunk | 3 ++- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/bin/ap b/bin/ap index 35d0dce2..1c598985 100755 --- a/bin/ap +++ b/bin/ap @@ -130,7 +130,8 @@ while [ $# -gt 0 ]; do esac done -[ ${#files[@]} -eq 0 ] && files=(.) +# if files is empty, use "." +files=("${files[@]:-.}") readonly files ################################################################################ diff --git a/bin/find-in-jars b/bin/find-in-jars index c69cdbc8..5ff3e892 100755 --- a/bin/find-in-jars +++ b/bin/find-in-jars @@ -268,9 +268,7 @@ readonly dirs # convert extensions to find -iname options find_iname_options=() for e in "${extensions[@]}"; do - (("${#find_iname_options[@]}" == 0)) && - find_iname_options=(-iname "*.$e") || - find_iname_options=("${find_iname_options[@]}" -o -iname "*.$e") + find_iname_options=(${find_iname_options[@]:+"${find_iname_options[@]}" -o} -iname "*.$e") done readonly find_iname_options diff --git a/bin/uq b/bin/uq index c4fce26d..8f833f6b 100755 --- a/bin/uq +++ b/bin/uq @@ -194,7 +194,7 @@ while (($# > 0)); do ;; --) shift - argv=("${argv[@]}" "$@") + argv=(${argv[@]:+"${argv[@]}"} "$@") break ;; -) diff --git a/bin/xpl b/bin/xpl index 0ee0269d..38b92461 100755 --- a/bin/xpl +++ b/bin/xpl @@ -66,7 +66,7 @@ progVersion() { # parse options ################################################################################ -declare -a args=() +declare -a files=() selected=false while [ $# -gt 0 ]; do case "$1" in @@ -82,23 +82,26 @@ while [ $# -gt 0 ]; do ;; --) shift - args=(${args[@]:+"${args[@]}"} "$@") + files=(${files[@]:+"${files[@]}"} "$@") break ;; -*) usage 2 "${PROG}: unrecognized option '$1'" ;; *) - args=(${args[@]:+"${args[@]}"} "$1") + files=(${files[@]:+"${files[@]}"} "$1") shift ;; esac done +# if files is empty, use one element "." +files=("${files[@]:-.}") + # if program name is xpf, set option selected! [ "xpf" == "${PROG}" ] && selected=true -readonly args selected +readonly files selected ################################################################################ # biz logic @@ -143,7 +146,6 @@ openOneFile() { printf 'open %14s: %s\n' "$selected_msg" "$file" } -[ "${#args[@]}" == 0 ] && files=(.) || files=("${args[@]}") has_error=false for file in "${files[@]}"; do diff --git a/legacy-bin/swtrunk b/legacy-bin/swtrunk index cde45c6e..fc14abfd 100755 --- a/legacy-bin/swtrunk +++ b/legacy-bin/swtrunk @@ -27,7 +27,8 @@ greenEcho() { colorEcho 32 "$@" } -[ $# -eq 0 ] && dirs=(.) || dirs=("$@") +# if dirs is empty, use "." +dirs=("${dirs[@]:-.}") for d in "${dirs[@]}"; do [ ! -d "${d}/.svn" ] && {