From b89c055933320f38e325e06de556b419ad65e1c0 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 2 Mar 2018 21:37:10 +0800 Subject: [PATCH 1/5] add seperator option for find-in-jars --- docs/java.md | 9 +++++---- find-in-jars | 10 ++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/java.md b/docs/java.md index 5b43fa6f..2b59adc2 100644 --- a/docs/java.md +++ b/docs/java.md @@ -358,14 +358,15 @@ Find file in the jar files under specified directory(recursive, include subdirec The pattern default is *extended* regex. Example: - find-in-jars 'log4j\.properties' - find-in-jars '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root - find-in-jars 'log4j\.properties$' -d /path/to/find/directory - find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2 + find-in-jars 'log4j\.properties' + find-in-jars '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root + find-in-jars 'log4j\.properties$' -d /path/to/find/directory + find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2 Options: -d, --dir the directory that find jar files, default is current directory. this option can specify multiply times to find in multiply directories. + -s, --seperator seperator for jar file and file entry, default is `!'. -E, --extended-regexp PATTERN is an extended regular expression (*default*) -F, --fixed-strings PATTERN is a set of newline-separated strings -G, --basic-regexp PATTERN is a basic regular expression diff --git a/find-in-jars b/find-in-jars index 374117e4..4f8ce386 100755 --- a/find-in-jars +++ b/find-in-jars @@ -30,6 +30,7 @@ Example: Options: -d, --dir the directory that find jar files, default is current directory. this option can specify multiply times to find in multiply directories. + -s, --seperator seperator for jar file and file entry, default is \`!'. -E, --extended-regexp PATTERN is an extended regular expression (*default*) -F, --fixed-strings PATTERN is a set of newline-separated strings -G, --basic-regexp PATTERN is a basic regular expression @@ -56,6 +57,10 @@ while [ $# -gt 0 ]; do dirs=("${dirs[@]}" "$2") shift 2 ;; + -s|--seperator) + seperator="$2" + shift 2 + ;; -E|--extended-regexp) regex_mode=-E shift @@ -100,6 +105,7 @@ while [ $# -gt 0 ]; do esac done dirs=${dirs:-.} +seperator="${seperator:-!}" [ "${#args[@]}" -eq 0 ] && { echo "No find file pattern!" 1>&2 ; usage 1; } [ "${#args[@]}" -gt 1 ] && { echo "More than 1 file pattern!" 1>&2 ; usage 1; } @@ -207,8 +213,8 @@ while read jar_file; do clearResponsiveMessage $is_console && - echo "$ec[1;31m${jar_file}$eend$ec[1;32m"\!"$eend${file}" || - echo "${jar_file}"\!"${file}" + echo "$ec[1;31m${jar_file}${eend}${ec}[1;32m${seperator}${eend}${file}" || + echo "${jar_file}${seperator}${file}" done clearResponsiveMessage From c8b226567e238d9cb5d1b38cbf5e50acf8ea2d12 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 2 Mar 2018 23:42:38 +0800 Subject: [PATCH 2/5] add extension option for find-in-jars --- docs/java.md | 28 +++++++++++++++++++++++----- find-in-jars | 29 +++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/docs/java.md b/docs/java.md index 2b59adc2..5ae3132c 100644 --- a/docs/java.md +++ b/docs/java.md @@ -340,17 +340,27 @@ class paths to find: find-in-jars 'log4j\.properties' find-in-jars 'log4j\.xml$' find-in-jars log4j\\.xml$ # 和上面命令一样,Shell转义的不同写法而已 -find-in-jars 'log4j(\.properties|\.xml)$' +find-in-jars 'log4j\.(properties|xml)$' # -d选项 指定 查找目录(覆盖缺省的当前目录) find-in-jars 'log4j\.properties$' -d /path/to/find/directory -# 支持多个查找目录 +# 支持多个查找目录,多次指定这个选项即可 find-in-jars 'log4j\.properties' -d /path/to/find/directory1 -d /path/to/find/directory2 +# -e选项 指定 查找`zip`文件的扩展名,缺省是`jar` +find-in-jars 'log4j\.properties' -e zip +# 支持多种查找扩展名,多次指定这个选项即可 +find-in-jars 'log4j\.properties' -e jar -e zip + # -a选项 指定 查找结果中的Jar文件使用绝对路径 # 分享给别人时,Jar文件路径是完整的,方便别人找到文件 find-in-jars 'log4j\.properties' -a +# -s选项 指定 查找结果中的Jar文件和Jar文件里的查找Entry间分隔符,缺省是『!』 +# 方便你喜欢的人眼查看,或是与工具脚本如`awk`的处理 +find-in-jars 'log4j\.properties' -s ' <-> ' +find-in-jars 'log4j\.properties' -s ' ' | awk '{print $2}' + # 帮助信息 $ find-in-jars -h Usage: find-in-jars [OPTION]... PATTERN @@ -359,20 +369,28 @@ The pattern default is *extended* regex. Example: find-in-jars 'log4j\.properties' - find-in-jars '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root + find-in-jars '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root find-in-jars 'log4j\.properties$' -d /path/to/find/directory find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2 + find-in-jars 'log4j\.properties' -e jar -e zip + find-in-jars 'log4j\.properties' -s ' <-> ' -Options: +Find control: -d, --dir the directory that find jar files, default is current directory. this option can specify multiply times to find in multiply directories. - -s, --seperator seperator for jar file and file entry, default is `!'. + -e, --extension set find file extension, default is jar. + this option can specify multiply times to find in multiply extension. -E, --extended-regexp PATTERN is an extended regular expression (*default*) -F, --fixed-strings PATTERN is a set of newline-separated strings -G, --basic-regexp PATTERN is a basic regular expression -P, --perl-regexp PATTERN is a Perl regular expression -i, --ignore-case ignore case distinctions + +Output control: -a, --absolute-path always print absolute path of jar file + -s, --seperator seperator for jar file and file entry, default is `!'. + +Miscellaneous: -h, --help display this help and exit ``` diff --git a/find-in-jars b/find-in-jars index 4f8ce386..50d72214 100755 --- a/find-in-jars +++ b/find-in-jars @@ -23,20 +23,28 @@ The pattern default is *extended* regex. Example: ${PROG} 'log4j\.properties' - ${PROG} '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root + ${PROG} '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root ${PROG} 'log4j\.properties$' -d /path/to/find/directory ${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2 + ${PROG} 'log4j\.properties' -e jar -e zip + ${PROG} 'log4j\.properties' -s ' <-> ' -Options: +Find control: -d, --dir the directory that find jar files, default is current directory. this option can specify multiply times to find in multiply directories. - -s, --seperator seperator for jar file and file entry, default is \`!'. + -e, --extension set find file extension, default is jar. + this option can specify multiply times to find in multiply extension. -E, --extended-regexp PATTERN is an extended regular expression (*default*) -F, --fixed-strings PATTERN is a set of newline-separated strings -G, --basic-regexp PATTERN is a basic regular expression -P, --perl-regexp PATTERN is a Perl regular expression -i, --ignore-case ignore case distinctions + +Output control: -a, --absolute-path always print absolute path of jar file + -s, --seperator seperator for jar file and file entry, default is \`!'. + +Miscellaneous: -h, --help display this help and exit EOF @@ -57,6 +65,10 @@ while [ $# -gt 0 ]; do dirs=("${dirs[@]}" "$2") shift 2 ;; + -e|--extension) + extension=("${extension[@]}" "$2") + shift 2 + ;; -s|--seperator) seperator="$2" shift 2 @@ -105,6 +117,7 @@ while [ $# -gt 0 ]; do esac done dirs=${dirs:-.} +extension=${extension:-jar} seperator="${seperator:-!}" [ "${#args[@]}" -eq 0 ] && { echo "No find file pattern!" 1>&2 ; usage 1; } @@ -122,6 +135,14 @@ $use_absolute_path && { dirs=( "${tmp_dirs[@]}" ) } +# convert to find -iname options +for e in "${extension[@]}"; do + [ 0 -eq "${#find_iname_options[@]}" ] && + find_iname_options=( -iname "*.$e" ) || + find_iname_options=( "${find_iname_options[@]}" -o -iname "*.$e" ) +done + + ################################################################################ # Check the existence of command for listing zip entry! ################################################################################ @@ -194,7 +215,7 @@ clearResponsiveMessage() { # find logic ################################################################################ -readonly jar_files="$(find "${dirs[@]}" -iname '*.jar' -type f)" +readonly jar_files="$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)" readonly total_count="$(echo "$jar_files" | wc -l)" [ -z "$jar_files" ] && { echo "No jar found!" 1>&2 From 0cdd262e8b4517cf08cb6b93939a50110066b386 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 3 Mar 2018 00:08:34 +0800 Subject: [PATCH 3/5] improve usage function to simplify exit code --- c | 6 +++--- docs/java.md | 2 +- find-in-jars | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/c b/c index e17d1c55..256e8322 100755 --- a/c +++ b/c @@ -17,6 +17,8 @@ readonly PROG="`basename "$0"`" usage() { [ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout + [ $# -gt 1 ] && { echo "$2"; echo; } > $out + > $out cat <&2 - echo 1>&2 - usage 2; + usage 2 "${PROG}: unrecognized option '$1'" ;; *) # if not option, treat all follow args as command diff --git a/docs/java.md b/docs/java.md index 5ae3132c..1c33b504 100644 --- a/docs/java.md +++ b/docs/java.md @@ -371,7 +371,7 @@ Example: find-in-jars 'log4j\.properties' find-in-jars '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root find-in-jars 'log4j\.properties$' -d /path/to/find/directory - find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2 + find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d path/to/find/dir2 find-in-jars 'log4j\.properties' -e jar -e zip find-in-jars 'log4j\.properties' -s ' <-> ' diff --git a/find-in-jars b/find-in-jars index 50d72214..400450f8 100755 --- a/find-in-jars +++ b/find-in-jars @@ -16,6 +16,8 @@ readonly PROG="`basename "$0"`" usage() { [ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout + [ $# -gt 1 ] && { echo "$2"; echo; } > $out + > $out cat < ' @@ -106,9 +108,7 @@ while [ $# -gt 0 ]; do break ;; -*) - echo "${PROG}: unrecognized option '$1'" 1>&2 - echo 1>&2 - usage 2; + usage 2 "${PROG}: unrecognized option '$1'" ;; *) args=("${args[@]}" "$1") @@ -120,8 +120,8 @@ dirs=${dirs:-.} extension=${extension:-jar} seperator="${seperator:-!}" -[ "${#args[@]}" -eq 0 ] && { echo "No find file pattern!" 1>&2 ; usage 1; } -[ "${#args[@]}" -gt 1 ] && { echo "More than 1 file pattern!" 1>&2 ; usage 1; } +[ "${#args[@]}" -eq 0 ] && usage 1 "No find file pattern!" +[ "${#args[@]}" -gt 1 ] && usage 1 "More than 1 file pattern!" readonly pattern="${args[0]}" # convert dirs to Absolute Path. From c10c9b40931e0c08211d7a9a1cb0b762a69fb546 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Mon, 5 Mar 2018 17:42:49 +0800 Subject: [PATCH 4/5] use `if` instead `&&/||` avoid exit logic bug when "if clause" fail --- c | 7 +++++-- docs/java.md | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/c b/c index 256e8322..39f292b8 100755 --- a/c +++ b/c @@ -97,5 +97,8 @@ teeAndCopy() { ) > $out } -[ ${#args[@]} -eq 0 ] && teeAndCopy || - "${args[@]}" | teeAndCopy +if [ ${#args[@]} -eq 0 ]; then + teeAndCopy +else + "${args[@]}" | teeAndCopy +fi diff --git a/docs/java.md b/docs/java.md index 1c33b504..10581e49 100644 --- a/docs/java.md +++ b/docs/java.md @@ -32,7 +32,7 @@ 用于快速排查`Java`的`CPU`性能问题(`top us`值过高),自动查出运行的`Java`进程中消耗`CPU`多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用。 目前只支持`Linux`。原因是`Mac`、`Windows`的`ps`命令不支持列出线程线程,更多信息参见[#33](https://github.com/oldratlee/useful-scripts/issues/33),欢迎提供解法。 -PS,如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)的[《分布式Java应用》](https://book.douban.com/subject/4848587/)的【5.1.1 cpu消耗分析】一节,说得很详细: +PS,如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)的[《分布式Java应用》](https://book.douban.com/subject/4848587/)的【5.1.1 `CPU`消耗分析】一节,说得很详细: 1. `top`命令找出有问题`Java`进程及线程`id`: 1. 开启线程显示模式(`top -H`,或是打开`top`后按`H`) From c447262a42f9283c31051df6061c8c16b62428db Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 30 Mar 2018 21:33:49 +0800 Subject: [PATCH 5/5] parse option by hand instead of getopt, so as to support mac #66 --- xpf | 8 ++++---- xpl | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/xpf b/xpf index c929ac87..85652989 100755 --- a/xpf +++ b/xpf @@ -1,7 +1,7 @@ #!/bin/bash # @Function -# Open file in file explorer. -# same as xpl --selected [FILE] +# Open file in file explorer, file is selected. +# same as xpl --selected [file [file ...] ] # # @Usage # $ ./xpf file @@ -9,5 +9,5 @@ # @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#beer-xpl-and-xpf # @author Jerry Lee (oldratlee at gmail dot com) -BASE=`dirname $0` -. $BASE/xpl "$@" +BASE="$(dirname "$0")" +source "$BASE/xpl" "$@" diff --git a/xpl b/xpl index beb43734..9e703721 100755 --- a/xpl +++ b/xpl @@ -3,7 +3,7 @@ # Open file in file explorer. # # @Usage -# $ ./xpf file +# $ ./xpf [file [file ...] ] # # @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#beer-xpl-and-xpf # @author Jerry Lee (oldratlee at gmail dot com) @@ -11,6 +11,10 @@ PROG=`basename $0` usage() { + [ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout + + [ $# -gt 1 ] && { echo "$2"; echo; } > $out + cat <