diff --git a/c b/c index e17d1c55..39f292b8 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 @@ -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 5b43fa6f..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`) @@ -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 @@ -358,20 +368,29 @@ 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 - -Options: + 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' -e jar -e zip + find-in-jars 'log4j\.properties' -s ' <-> ' + +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. + -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 374117e4..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 < ' -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. + -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 @@ -56,6 +67,14 @@ while [ $# -gt 0 ]; do dirs=("${dirs[@]}" "$2") shift 2 ;; + -e|--extension) + extension=("${extension[@]}" "$2") + shift 2 + ;; + -s|--seperator) + seperator="$2" + shift 2 + ;; -E|--extended-regexp) regex_mode=-E shift @@ -89,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") @@ -100,9 +117,11 @@ 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; } -[ "${#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. @@ -116,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! ################################################################################ @@ -188,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 @@ -207,8 +234,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