[
  {
    "path": ".editorconfig",
    "content": "root = true\n\n[*]\ncharset = utf-8\nend_of_line = lf\ninsert_final_newline = true\ntab_width = 4\nindent_style = space\nindent_size = 2\ntrim_trailing_whitespace = true\n\n[*.{py,md,mkd,markdown}]\nindent_size = 4\n\n[*.xml]\nindent_style = tab\n\n[*.{md,mkd,markdown}]\ntrim_trailing_whitespace = false\n\n# python files without extension\n[show-duplicate-java-classes]\nindent_size = 4\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "# To get started with Dependabot version updates, you'll need to specify which\n# package ecosystems to update and where the package manifests are located.\n# Please see the documentation for all configuration options:\n# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates\n\nversion: 2\nupdates:\n  - package-ecosystem: github-actions\n    directory: /\n    schedule:\n      interval: daily\n"
  },
  {
    "path": ".github/workflows/ci.yaml",
    "content": "# Quickstart for GitHub Actions\n# https://docs.github.com/en/actions/quickstart\n\nname: CI\non: [ push, pull_request, workflow_dispatch ]\n\njobs:\n\n  test:\n    runs-on: ${{ matrix.os }}\n    timeout-minutes: 5\n    strategy:\n      matrix:\n        # the OS supported by GitHub Actions\n        # https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources\n        os: [ ubuntu-latest, macos-latest ]\n      fail-fast: false\n      max-parallel: 64\n    name: Test on ${{ matrix.os }}\n\n    steps:\n      - uses: actions/checkout@v6\n        with:\n          submodules: recursive\n      - run: brew install coreutils gnu-sed\n        # https://docs.github.com/en/actions/learn-github-actions/variables#detecting-the-operating-system\n        # https://docs.github.com/en/actions/learn-github-actions/expressions\n        if: runner.os == 'macOS'\n      - run: test/chore/integration-test.sh\n      # https://remarkablemark.org/blog/2017/10/12/check-git-dirty/\n      - name: Check git dirty\n        run: |\n          git status --short\n          [ -z \"$(git status --short)\" ]\n"
  },
  {
    "path": ".github/workflows/lint.yaml",
    "content": "# Quickstart for GitHub Actions\n# https://docs.github.com/en/actions/quickstart\n\nname: Lint\non: [ push, pull_request, workflow_dispatch ]\n\njobs:\n\n  test:\n    runs-on: ubuntu-latest\n    timeout-minutes: 5\n    name: Lint\n\n    steps:\n      - uses: actions/checkout@v6\n        with:\n          submodules: recursive\n      - run: test/chore/lint.sh\n      # https://remarkablemark.org/blog/2017/10/12/check-git-dirty/\n      - name: Check git dirty\n        run: |\n          git status --short\n          [ -z \"$(git status --short)\" ]\n"
  },
  {
    "path": ".gitmodules",
    "content": "[submodule \"test/shunit2\"]\n\tpath = test/shunit2-lib\n\turl = https://github.com/kward/shunit2.git\n"
  },
  {
    "path": "LICENSE",
    "content": "\n\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "# <div align=\"center\"><a href=\"#dummy\"><img src=\"https://github.com/oldratlee/useful-scripts/assets/1063891/990d7ab3-1a84-4024-b1c6-4c8d441dcfc6\" alt=\"🐌 useful-scripts\"></a></div>\n\n<p align=\"center\">\n<a href=\"https://github.com/oldratlee/useful-scripts/actions/workflows/ci.yaml\"><img src=\"https://img.shields.io/github/actions/workflow/status/oldratlee/useful-scripts/ci.yaml?branch=dev-3.x&logo=github&logoColor=white\" alt=\"Github Workflow Build Status\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts/releases\"><img src=\"https://img.shields.io/github/release/oldratlee/useful-scripts.svg\" alt=\"GitHub release\"></a>\n<a href=\"https://www.apache.org/licenses/LICENSE-2.0.html\"><img src=\"https://img.shields.io/github/license/oldratlee/useful-scripts?color=4D7A97&logo=apache\" alt=\"License\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts/stargazers\"><img src=\"https://img.shields.io/github/stars/oldratlee/useful-scripts?style=flat\" alt=\"GitHub Stars\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts/fork\"><img src=\"https://img.shields.io/github/forks/oldratlee/useful-scripts?style=flat\" alt=\"GitHub Forks\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts/issues\"><img src=\"https://img.shields.io/github/issues/oldratlee/useful-scripts\" alt=\"GitHub issues\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts/graphs/contributors\"><img src=\"https://img.shields.io/github/contributors/oldratlee/useful-scripts\" alt=\"GitHub Contributors\"></a>\n<a href=\"https://github.com/oldratlee/useful-scripts\"><img src=\"https://img.shields.io/github/repo-size/oldratlee/useful-scripts\" alt=\"GitHub repo size\"></a>\n</p>\n\n🐌 useful scripts for making developer's everyday life easier and happier, involved java, shell etc.\n\n👉 平时有用的手动操作做成脚本，以便捷地使用，让开发的日常生活更轻松些。 💕\n\n欢迎 👏 💖\n\n- 提问，[提交 Issue](https://github.com/oldratlee/useful-scripts/issues/new)\n- 分享平时自己常用但没有写成脚本的功能（即需求、想法），[提交Issue](https://github.com/oldratlee/useful-scripts/issues/new)\n- 优化改进，[Fork 后提通过 Pull Request 贡献代码](https://github.com/oldratlee/useful-scripts/fork)\n- 提供的自己好用脚本实现，[Fork 后提通过 Pull Request 提供](https://github.com/oldratlee/useful-scripts/fork)\n\n本仓库的脚本（如`Java`相关脚本）在阿里等公司（如随身云，见[`awesome-scripts`仓库](https://github.com/Suishenyun/awesome-scripts)说明）的线上生产环境部署使用。\n\n如果你的公司有部署使用，欢迎使用通过 [Issue：who's using | 用户反馈收集](https://github.com/oldratlee/useful-scripts/issues/96) 告知，方便互相交流反馈～ 💗\n\n<a href=\"#dummy\"><img src=\"https://github.com/oldratlee/useful-scripts/assets/1063891/82f2d184-ca16-4c37-b053-07f21fd8aef1\" alt=\"repo-icon\" width=\"20%\" align=\"right\" /></a>\n\n----------------------\n\n<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->\n\n- [🔰 快速下载&使用](#-%E5%BF%AB%E9%80%9F%E4%B8%8B%E8%BD%BD%E4%BD%BF%E7%94%A8)\n- [📚 使用文档](#-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3)\n    - [☕ `Java`相关脚本](#-java%E7%9B%B8%E5%85%B3%E8%84%9A%E6%9C%AC)\n    - [🐚 `Shell`相关脚本](#-shell%E7%9B%B8%E5%85%B3%E8%84%9A%E6%9C%AC)\n- [🎓 Developer Guide](#-developer-guide)\n    - [🎯 面向开发者的目标](#-%E9%9D%A2%E5%90%91%E5%BC%80%E5%8F%91%E8%80%85%E7%9A%84%E7%9B%AE%E6%A0%87)\n        - [关于`Shell`脚本](#%E5%85%B3%E4%BA%8Eshell%E8%84%9A%E6%9C%AC)\n    - [🚦 开发约定](#-%E5%BC%80%E5%8F%91%E7%BA%A6%E5%AE%9A)\n    - [📚 `Shell`学习与开发的资料](#-shell%E5%AD%A6%E4%B9%A0%E4%B8%8E%E5%BC%80%E5%8F%91%E7%9A%84%E8%B5%84%E6%96%99)\n\n<!-- END doctoc generated TOC please keep comment here to allow auto update -->\n\n----------------------\n\n🔰 快速下载&使用\n----------------------\n\n```bash\nsource <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/release-3.x/test/self-installer.sh)\n```\n\n更多下载&使用方式，参见[下载使用](docs/install.md)。\n\n📚 使用文档\n----------------------\n\n### ☕ [`Java`相关脚本](docs/java.md)\n\n1. [show-busy-java-threads](docs/java.md#-show-busy-java-threads)  \n   用于快速排查`Java`的`CPU`性能问题(`top us`值过高)，自动查出运行的`Java`进程中消耗`CPU`多的线程，并打印出其线程栈，从而确定导致性能问题的方法调用。\n1. [show-duplicate-java-classes](docs/java.md#-show-duplicate-java-classes)  \n   找出`jar`文件和`class`目录中的重复类。用于排查`Java`类冲突问题。\n1. [find-in-jars](docs/java.md#-find-in-jars)  \n   在目录下所有`jar`文件里，查找类或资源文件。\n\n### 🐚 [`Shell`相关脚本](docs/shell.md)\n\n`Shell`使用加强：\n\n1. [c](docs/shell.md#-c)  \n   原样命令行输出，并拷贝标准输出到系统剪贴板，省去`CTRL+C`操作，优化命令行与其它应用之间的操作流。\n1. [coat and taoc](docs/shell.md#-coat)  \n   彩色`cat`/`tac`出文件行，方便人眼区分不同的行。\n1. [a2l](docs/shell.md#-a2l)  \n   按行彩色输出参数，方便人眼查看。\n1. [uq](docs/shell.md#-uq)  \n   不重排序输入完成整个输入行的去重。相比系统的`uniq`命令加强的是可以跨行去重，不需要排序输入。\n1. [ap and rp](docs/shell.md#-ap-and-rp)  \n   批量转换文件路径为绝对路径/相对路径，会自动跟踪链接并规范化路径。\n1. [cp-into-docker-run](docs/shell.md#-cp-into-docker-run)  \n   一个`Docker`使用的便利脚本。拷贝本机的执行文件到指定的`docker container`中并在`docker container`中执行。\n1. [tcp-connection-state-counter](docs/shell.md#-tcp-connection-state-counter)  \n   统计各个`TCP`连接状态的个数。用于方便排查系统连接负荷问题。\n1. [xpl and xpf](docs/shell.md#-xpl-and-xpf)  \n   在命令行中快速完成 在文件浏览器中 打开/选中 指定的文件或文件夹的操作，优化命令行与其它应用之间的操作流。\n\n`Shell`开发/测试加强：\n\n1. [echo-args](docs/shell.md#-echo-args)  \n   输出脚本收到的参数，在控制台运行时，把参数值括起的括号显示成 **红色**，方便人眼查看。用于调试脚本参数输入。\n1. [console-text-color-themes.sh](docs/shell.md#-console-text-color-themessh)  \n   显示`Terminator`的全部文字彩色组合的效果及其打印方式，用于开发`Shell`的彩色输出。\n1. [parseOpts.sh](docs/shell.md#-parseoptssh)  \n   命令行选项解析库，加强支持选项有多个值（即数组）。\n\n## 🎓 Developer Guide\n\n为用户提供有用的功能，当然是这个库的首要的价值体现和存在理由。\n\n但作为一个**开源**项目，每个人都可以看到源码实现，这个库或许能做得更多。\n\n### 🎯 面向开发者的目标\n\n- 将`Shell/Bash`作为线上生产环境使用的专业编程语言。\n- 期望体现`Shell/Bash`脚本 生产环境级的严谨开发方式与最佳实践，进而有可能示例与改善在生产环境中`Shell`脚本的质量状况。\n\nPS：\n\n- 虽然上面是自己期望的目标，但自己在`Shell`语言上一定会有很多理解和使用上的问题、在这些实现脚本中也会很多需要的改进，可以一起学习、讨论与实践～ 💕\n- 这个库中脚本的实现也有使用`Python`。\n\n#### 关于`Shell`脚本\n\n命令行（`CLI`）几乎是每个程序员每天都在使用的工具。相比图形界面工具（`GUI`），命令行有着自己不可替代的便利性和优越性。\n\n命令行里写出来其实就是`Shell`脚本，可以说每个开发者会写`Shell`脚本（或多或少）。在生产环境的功能实现中，也常会看到`Shell`脚本（虽然不如主流语言那么常见）。\n\n可能正因为上面所说的`Shell`脚本的便利性和大众性：\n\n- `Shell`脚本有不少是顺手实现的（包括生产环境用的`Shell`脚本）；\n- `Shell`脚本的实现常常可能质量不高，会引发线上严重的故障。\n\n### 🚦 开发约定\n\n在这个库中的`Shell`脚本：\n\n- 统一使用`Bash 3.2+`；\n- 面向生产环境，尽可能使用严谨安全的开发方式。\n\n`Shell`用`Bash`的原因是：\n\n- 目前仍然是主流的`Shell`，并且在不同环境基本上都缺省部署了。\n- 在[`Google`的`Shell`风格指南](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background.html#shell)中，明确说到了：`Bash`是**唯一**被允许执行的`shell`脚本语言。\n- 统一用`Bash`，可以避免不同`Shell`之间差异所带来的风险与没有收益的复杂性。\n    - 有大量的`Shell`实现，`sh`、`bash`、`zsh`、`fish`、`csh`、`tcsh`、`ksh`、`ash`、`dash`……\n    - 不同的`Shell`有各种差异，深坑勿入。\n- 个人系统学习过的是`Bash`，比较理解熟悉。\n\nPS: 虽然交互`Shell`个人已经使用`Zsh` + [`oh-my-zsh`](https://ohmyz.sh/)，但在严谨的`Shell`脚本开发时还是使用`Bash`。\n\n### 📚 `Shell`学习与开发的资料\n\n> 更多资料参见 [子文档](docs/developer-guide.md)。\n\n- 🛠️ 开发规范与工具\n    - [`Google Shell Style Guide`](https://google.github.io/styleguide/shell.xml) | [中文版](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/contents.html)\n    - [`koalaman/shellcheck`](https://github.com/koalaman/shellcheck): `ShellCheck`, a static analysis tool for shell scripts\n    - [`mvdan/sh(shfmt)`](https://github.com/mvdan/sh): `shfmt` formats shell programs\n- 👷 **`Bash/Shell`最佳实践与安全编程**文章\n    - [Use the Unofficial Bash Strict Mode (Unless You Looove Debugging)](http://redsymbol.net/articles/unofficial-bash-strict-mode/)\n    - Bash Pitfalls: 编程易犯的错误 - 团子的小窝：[Part 1](http://kodango.com/bash-pitfalls-part-1) | [Part 2](http://kodango.com/bash-pitfalls-part-2) | [Part 3](http://kodango.com/bash-pitfalls-part-3) | [Part 4](http://kodango.com/bash-pitfalls-part-4) | [英文原文：Bash Pitfalls](http://mywiki.wooledge.org/BashPitfalls)\n    - [不要自己去指定`sh`的方式去执行脚本](https://github.com/oldratlee/useful-scripts/issues/57#issuecomment-326485965)\n- 🎶 **Tips**\n    - [让你提升命令行效率的 Bash 快捷键 【完整版】](https://linuxtoy.org/archives/bash-shortcuts.html)  \n      补充：`ctrl + x, ctrl + e` 就地打开文本编辑器来编辑当前命令行，对于复杂命令行特别有用\n    - [应该知道的Linux技巧 | 酷 壳 - CoolShell](https://coolshell.cn/articles/8883.html)\n    - 简洁的 Bash Programming 技巧 - 团子的小窝：[Part 1](http://kodango.com/simple-bash-programming-skills) | [Part 2](http://kodango.com/simple-bash-programming-skills-2) | [Part 3](http://kodango.com/simple-bash-programming-skills-3)\n- 💎 **系统学习** — 看文章、了解Tips完全不能替代系统学习才能真正理解并专业开发！\n    - [《Bash Pocket Reference》](https://book.douban.com/subject/26738258/)  \n      力荐！说明简单直接结构体系的佳作，专业`Bash`编程必备！且16年的第二版更新到了新版的`Bash 4`\n    - [《学习bash》](https://book.douban.com/subject/1241361/) 上面那本的展开版\n    - 官方资料\n        - [`bash man`](https://manned.org/bash) | [中文版](http://ahei.info/chinese-bash-man.htm)\n        - [Bash Reference Manual - gnu.org](http://www.gnu.org/software/bash/manual/) | [中文版](https://yiyibooks.cn/Phiix/bash_reference_manual/bash%E5%8F%82%E8%80%83%E6%96%87%E6%A1%A3.html)  \n          Bash参考手册，讲得全面且有深度，比如会全面地讲解不同转义的区别、命令的解析过程，这有助统一深入的方式认识Bash整个执行方式和过程。这些内容在其它书中往往不会讲（因为复杂难于深入浅出的讲解），但却一通百通的关键。\n    - [Advanced Bash-Scripting Guide](https://hangar118.sdf.org/p/bash-scripting-guide/index.html): An in-depth exploration of the art of shell scripting.\n    - [命令行的艺术 - `jlevy/the-art-of-command-line`](https://github.com/jlevy/the-art-of-command-line/blob/master/README-zh.md)\n    - [`awesome-lists/awesome-bash`](https://github.com/awesome-lists/awesome-bash): A curated list of delightful Bash scripts and resources.\n    - [`alebcay/awesome-shell`](https://github.com/alebcay/awesome-shell): A curated list of awesome command-line frameworks, toolkits, guides and gizmos.\n    - 更多书籍参见个人整理的[书籍豆列 **_`Bash/Shell`_**](https://www.douban.com/doulist/1779379/)\n"
  },
  {
    "path": "bin/a2l",
    "content": "#!/usr/bin/env bash\n# @Function\n# print each arguments on one line colorfully.\n#\n# @Usage\n#   $ ./a2l arg1 arg2\n#   $ ./a2l *.txt\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-a2l\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# parse options\n################################################################################\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... ARG...\nprint each arguments on one line colorfully.\n\nExample:\n  $PROG arg1 arg2\n  $PROG */*.py\n\nOptions:\n  -h, --help      display this help and exit\n  -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\nargs=()\nwhile (($# > 0)); do\n  case \"$1\" in\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    # if unrecognized option, treat it and all follow arguments as args\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  *)\n    # if not option, treat it and all follow arguments as args\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  esac\ndone\nreadonly args\n\n################################################################################\n# biz logic\n################################################################################\n\nreadonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)\nCOLOR_INDEX=0\nrotateColorPrint() {\n  local content=$*\n  # - if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  # - skip color for white space\n  if [[ ! -t 1 || $content =~ ^[[:space:]]*$ ]]; then\n    printf '%s\\n' \"$content\"\n  else\n    local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}\n    printf '\\e[1;%sm%s\\e[0m\\n' \"$color\" \"$content\"\n  fi\n}\n\nfor a in ${args[@]:+\"${args[@]}\"}; do\n  rotateColorPrint \"$a\"\ndone\n"
  },
  {
    "path": "bin/ap",
    "content": "#!/usr/bin/env bash\n# @Function\n# convert to Absolute Path.\n#\n# @Usage\n#   # print Absolute Path of current directory.\n#   $ ./ap\n#   # print Absolute Path of arguments.\n#   $ ./ap a.txt ../dir1/b.txt\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-ap-and-rp\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nerrorMsgPrint() {\n  local errorMsg=\"$PROG: $*\"\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$errorMsg\"\n  else\n    printf '%s\\n' \"$errorMsg\"\n  fi\n} >&2\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && errorMsgPrint \"$*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\n# `realpath` command exists on Linux and macOS, return resolved physical path\n#   - realpath command on macOS do NOT support option `-e`;\n#     combined `[ -e $file ]` to check file existence first.\n#   - How can I get the behavior of GNU's readlink -f on a Mac?\n#     https://stackoverflow.com/questions/1055671\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [FILE]...\nconvert to Absolute Path.\n\nExample:\n  $PROG arg1 arg2\n  $PROG */*.py\n\nOptions:\n  -h, --help      display this help and exit\n  -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\nfiles=()\nwhile (($# > 0)); do\n  case \"$1\" in\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    files=(${files[@]:+\"${files[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    # if not option, treat all follow files as args\n    files=(${files[@]:+\"${files[@]}\"} \"$@\")\n    break\n    ;;\n  esac\ndone\n\n# if files is empty, use \".\"\nreadonly files=(\"${files[@]:-.}\")\n\n################################################################################\n# biz logic\n################################################################################\n\nhas_error=false\n\nfor f in \"${files[@]}\"; do\n  realpath \"$f\" || {\n    has_error=true\n    errorMsgPrint \"$f: No such file or directory!\"\n  }\ndone\n\n# set exit status\n! $has_error\n"
  },
  {
    "path": "bin/c",
    "content": "#!/usr/bin/env bash\n# @Function\n# Run command and put output to system clipper.\n#\n# @Usage\n#   $ c ls -l\n#   $ ls -l | c\n#   $ c -q < ~/.ssh/id_rsa.pub\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-c\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [command [command_args ...]]\nRun command and put output to system clipper.\nIf no command is specified, read from stdin(pipe).\n\nExample:\n  $PROG grep -i 'hello world' menu.h main.c\n  set | $PROG\n  $PROG -q < ~/.ssh/id_rsa.pub\n\nOptions:\n  -k, --keep-eol  do not trim new line at end of file\n  -q, --quiet     suppress all normal output, default is false\n  -h, --help      display this help and exit\n  -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\nquiet=false\nkeep_eol=false\ntarget_command=()\nwhile (($# > 0)); do\n  case \"$1\" in\n  -k | --keep-eol)\n    keep_eol=true\n    shift\n    ;;\n  -q | --quiet)\n    quiet=true\n    shift\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    target_command=(${target_command[@]:+\"${target_command[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    # if not option, treat all follow arguments as command\n    target_command=(${target_command[@]:+\"${target_command[@]}\"} \"$@\")\n    break\n    ;;\n  esac\ndone\n\nreadonly keep_eol quiet target_command\n\nif ((${#target_command[@]} > 0)) && ! type -P \"${target_command[0]}\" &>/dev/null; then\n  die \"command '${target_command[0]}' not found on PATH\"\nfi\n\n################################################################################\n# biz logic\n################################################################################\n\nsystemClip() {\n  case \"$(uname)\" in\n  Darwin*)\n    pbcopy\n    ;;\n  CYGWIN* | MINGW*)\n    clip\n    ;;\n  *)\n    xsel -b\n    ;;\n  esac\n}\n\nbufferCopy() {\n  local content\n  content=$(cat)\n  if $keep_eol; then\n    printf '%s\\n' \"$content\"\n  else\n    printf %s \"$content\"\n  fi | systemClip\n}\n\nteeAndCopy() {\n  if $quiet; then\n    bufferCopy\n  else\n    tee >(bufferCopy)\n  fi\n}\n\nif ((${#target_command[@]} == 0)); then\n  teeAndCopy\nelse\n  command \"${target_command[@]}\" | teeAndCopy\nfi\n"
  },
  {
    "path": "bin/coat",
    "content": "#!/usr/bin/env bash\n# @Function\n# cat lines colorfully. coat means *CO*lorful c*AT*.\n#\n# @Usage\n#   $ echo -e 'Hello\\nWorld' | coat\n#   $ coat /path/to/file1\n#   $ coat /path/to/file1 /path/to/file2\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-coat\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# parse options\n################################################################################\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [FILE]...\ncat lines colorfully.\n\nSupport options:\n  --help     display this help and exit\n  --version  output version information and exit\nAll other options and arguments are delegated to command cat,\nmore info see the help/man of command cat(e.g. cat --help).\ncat executable: $(type -P cat)\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s version: %s\\n' \"$PROG\" \"$PROG_VERSION\"\n  printf 'cat executable: %s\\n' \"$(type -P cat)\"\n  exit\n}\n\nargs=(\"$@\")\n# check arguments in reverse, so last option wins.\nfor ((idx = $# - 1; idx >= 0; --idx)); do\n  [ \"${args[idx]}\" = --help ] && usage\n  [ \"${args[idx]}\" = --version ] && progVersion\ndone\nunset args idx\n\n################################################################################\n# biz logic\n################################################################################\n\n# if stdout is not a terminal, use `cat` directly.\n#   '-t' check: is a terminal?\n#   check isatty in bash https://stackoverflow.com/questions/10022323\n[ -t 1 ] || exec cat \"$@\"\n\nreadonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)\nCOLOR_INDEX=0\n# CAUTION: print content WITHOUT new line\nrotateColorPrint() {\n  local content=$*\n  # skip color for white space\n  if [[ $content =~ ^[[:space:]]*$ ]]; then\n    printf %s \"$content\"\n  else\n    local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}\n    printf '\\e[1;%sm%s\\e[0m' \"$color\" \"$content\"\n  fi\n}\n\nrotateColorPrintln() {\n  # NOTE: $'foo' is the escape sequence syntax of bash\n  rotateColorPrint \"$*\"$'\\n'\n}\n\ncolorLines() {\n  local line\n  # Bash read line does not read leading spaces\n  # https://stackoverflow.com/questions/29689172\n  while IFS= read -r line; do\n    rotateColorPrintln \"$line\"\n  done\n  # How to use `while read` (Bash) to read the last line in a file\n  #   if there’s no newline at the end of the file?\n  # https://stackoverflow.com/questions/4165135\n  [ -z \"$line\" ] || rotateColorPrint \"$line\"\n}\n\nif (($# == 0)); then\n  colorLines\nelse\n  cat \"$@\" | colorLines\nfi\n"
  },
  {
    "path": "bin/cp-into-docker-run",
    "content": "#!/usr/bin/env bash\n# @Function\n# Copy the command into docker container and run the command in container.\n#\n# Example:\n#  cp-into-docker-run -c container_foo command_copied_into_container command_arg1\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-cp-into-docker-run\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\ndie() {\n  local prompt_help=false exit_staus=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_staus=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_staus\"\n} >&2\n\nisAbsolutePath() {\n  [[ \"$1\" =~ ^/ ]]\n}\n\n# `realpath` command exists on Linux and macOS, return resolved physical path\n#   - realpath command on macOS do NOT support option `-e`;\n#     combined `[ -e $file ]` to check file existence first.\n#   - How can I get the behavior of GNU's readlink -f on a Mac?\n#     https://stackoverflow.com/questions/1055671\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... command [command-args]...\n\nCopy the command into docker container\nand run the command in container.\n\nExample:\n  $PROG -c container_foo command_copied_into_container command_arg1\n\ndocker options:\n  -c, --container    destination docker container\n  -u, --docker-user  docker username or UID to run command\n                     optional, docker default is (maybe) root user\n  -w, --workdir      absolute working directory inside the container\n                     optional, docker default is (maybe) root dir\n  -t, --tmpdir       tmp dir in docker to copy command\n                     optional, default is /tmp\n  -p, --cp-path      destination path in docker of the command(including file name)\n                     if specified, command will be kept when run finished\n                     optional, default is under tmp dir and deleted when run finished\n\nrun options:\n  -v, --verbose      show operation step infos\n\nmiscellaneous:\n  -h, --help         display this help and exit\n  -V, --version      display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\ncontainer_name=\ndocker_user=\ndocker_workdir=\ndocker_tmpdir=/tmp\ndocker_command_cp_path=\nverbose=false\nargs=()\n\nwhile (($# > 0)); do\n  case \"$1\" in\n  -c | --container)\n    container_name=$2\n    shift 2\n    ;;\n  -u | --docker-user)\n    docker_user=$2\n    shift 2\n    ;;\n  -w | --workdir)\n    docker_workdir=$2\n    shift 2\n    ;;\n  -t | --tmpdir)\n    docker_tmpdir=$2\n    shift 2\n    ;;\n  -p | --cp-path)\n    docker_command_cp_path=$2\n    shift 2\n    ;;\n  -v | --verbose)\n    verbose=true\n    shift\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    # if not option, treat all follow arguments as command\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  esac\ndone\n\nreadonly container_name docker_user docker_workdir docker_tmpdir docker_command_cp_path verbose args\n\n[ -n \"$container_name\" ] ||\n  die -h \"requires destination docker container name, specified by option -c/--container!\"\n\nif [ -n \"$docker_workdir\" ]; then\n  isAbsolutePath \"$docker_workdir\" ||\n    die \"docker workdir(-w/--workdir) must be absolute path: $docker_workdir\"\nelif [ -n \"$docker_command_cp_path\" ]; then\n  isAbsolutePath \"$docker_command_cp_path\" ||\n    die \"when no docker workdir(-w/--workdir) is specified, the command path in docker to copy(-p/--cp-path) must be absolute path: $docker_command_cp_path\"\nfi\n\n################################################################################\n# biz logic\n################################################################################\n\n########################################\n# check docker command existence\n########################################\n\ntype -P docker &>/dev/null || die 'docker command not found!'\n\n########################################\n# prepare vars for docker operation\n########################################\n\nreadonly specified_run_command=${args[0]}\nrun_command=$specified_run_command\nif [ ! -f \"$specified_run_command\" ]; then\n  type -P \"$specified_run_command\" &>/dev/null ||\n    die \"specified command not exists and not found in PATH: $specified_run_command\"\n\n  run_command=$(type -P \"$specified_run_command\")\nfi\n\nrun_command=$(realpath \"$run_command\")\nreadonly run_command run_command_base_name=${run_command##*/}\n\nrun_timestamp=$(date \"+%Y%m%d_%H%M%S\")\nreadonly run_timestamp\nreadonly uuid=\"${PROG}_${run_timestamp}_${$}_${RANDOM}\"\n\nif [ -n \"$docker_command_cp_path\" ]; then\n  if isAbsolutePath \"$docker_command_cp_path\"; then\n    readonly run_command_in_docker=$docker_command_cp_path\n  else\n    readonly run_command_in_docker=\"${docker_workdir:+\"$docker_workdir/\"}$docker_command_cp_path\"\n  fi\n  run_command_dir_in_docker=$(dirname -- \"$run_command_in_docker\")\n  readonly run_command_dir_in_docker\nelse\n  readonly work_tmp_dir_in_docker=$docker_tmpdir/$uuid\n\n  readonly run_command_in_docker=\"$work_tmp_dir_in_docker/$run_command_base_name\"\n  readonly run_command_dir_in_docker=$work_tmp_dir_in_docker\nfi\n\ncleanupWhenExit() {\n  [ -n \"${work_tmp_dir_in_docker:-}\" ] || return 0\n\n  # remove tmp dir in docker by root user\n  docker exec \"$container_name\" rm -rf -- \"$work_tmp_dir_in_docker\" &>/dev/null\n}\ntrap cleanupWhenExit EXIT\n\n########################################\n# docker operations\n########################################\n\nlogAndRun() {\n  $verbose && printf '%s\\n' \"[$PROG] $*\" >&2\n  \"$@\"\n}\n\nlogAndRun docker exec ${docker_user:+\"--user=$docker_user\"} \"$container_name\" \\\n  mkdir -p -- \"$run_command_dir_in_docker\"\nlogAndRun docker cp \"$run_command\" \"$container_name:$run_command_in_docker\"\nlogAndRun docker exec ${docker_user:+\"--user=$docker_user\"} \"$container_name\" \\\n  chmod +x \"$run_command_in_docker\"\n\nlogAndRun docker exec -i -t \\\n  ${docker_user:+\"--user=$docker_user\"} \\\n  ${docker_workdir:+\"--workdir=$docker_workdir\"} \\\n  \"$container_name\" \\\n  \"$run_command_in_docker\" \"${args[@]:1:${#args[@]}}\"\n"
  },
  {
    "path": "bin/echo-args",
    "content": "#!/usr/bin/env bash\n# @Function\n# print arguments in human and debugging friendly style.\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-echo-args\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\ndigitCount() {\n  # argument 1(num) is always a non-negative integer in this script usage,\n  # so NO argument validation logic.\n  local num=$1 count=0\n  while ((num != 0)); do\n    ((++count))\n    ((num = num / 10))\n  done\n  echo \"$count\"\n}\n\ndigit_count=$(digitCount $#)\nreadonly arg_count=$# digit_count\n\nreadonly RED='\\e[1;31m' BLUE='\\e[1;36m' COLOR_RESET='\\e[0m'\nprintArg() {\n  local idx=$1 value=$2\n\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf \"%${digit_count}s/%s: ${RED}[${BLUE}%s${RED}]${COLOR_RESET}\\n\" \"$idx\" \"$arg_count\" \"$value\"\n  else\n    printf \"%${digit_count}s/%s: [%s]\\n\" \"$idx\" \"$arg_count\" \"$value\"\n  fi\n}\n\nprintArg 0 \"$0\"\nidx=1\nfor a; do\n  printArg $((idx++)) \"$a\"\ndone\n"
  },
  {
    "path": "bin/find-in-jars",
    "content": "#!/usr/bin/env bash\n# @Function\n# Find files in the jar files under specified directory, search jar files recursively(include subdirectory).\n#\n# @Usage\n#   $ find-in-jars 'log4j\\.properties'\n#   # search file log4j.properties/log4j.xml at zip root\n#   $ find-in-jars '^log4j\\.(properties|xml)$'\n#   $ find-in-jars 'log4j\\.properties$' -d /path/to/find/directory\n#   $ find-in-jars '\\.properties$' -d /path/to/find/dir1 -d path/to/find/dir2\n#   $ find-in-jars 'Service\\.class$' -e jar -e zip\n#   $ find-in-jars 'Mon[^$/]*Service\\.class$' -s ' <-> '\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/java.md#-find-in-jars\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nreadonly COLOR_RESET='\\e[0m'\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf \"\\e[1;31m%s$COLOR_RESET\\n\" \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\n# How to delete line with echo?\n# https://unix.stackexchange.com/questions/26576\n#\n# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php\n# In particular, to clear from the cursor position to the beginning of the line:\n# echo -e \"\\033[1K\"\n# Or everything on the line, regardless of cursor position:\n# echo -e \"\\033[2K\"\nreadonly LINE_CLEAR='\\e[2K\\r'\n\n# Getting console width using a bash script\n# https://unix.stackexchange.com/questions/299067\n[ -t 2 ] && COLUMNS=$(stty size | awk '{print $2}')\n\nprintResponsiveMessage() {\n  if ! $show_responsive || [ ! -t 2 ]; then\n    return\n  fi\n\n  local content=$*\n  # http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html\n  printf %b%s \"$LINE_CLEAR\" \"${content:0:COLUMNS}\" >&2\n}\n\nclearResponsiveMessage() {\n  if ! $show_responsive || [ ! -t 2 ]; then\n    return\n  fi\n\n  printf %b \"$LINE_CLEAR\" >&2\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  clearResponsiveMessage\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... PATTERN\n\nFind files in the jar files under specified directory,\nsearch jar files recursively(include subdirectory).\nThe pattern default is *extended* regex.\n\nExample:\n  $PROG 'log4j\\.properties'\n  # search file log4j.properties/log4j.xml at zip root\n  $PROG '^log4j\\.(properties|xml)$'\n  $PROG 'log4j\\.properties$' -d /path/to/find/directory\n  $PROG '\\.properties$' -d /path/to/find/dir1 -d path/to/find/dir2\n  $PROG 'Service\\.class$' -e jar -e zip\n  $PROG 'Mon[^$/]*Service\\.class$' -s ' <-> '\n\nFind control:\n  -d, --dir              the directory that find jar files.\n                         default is current directory. this option can specify\n                         multiply times to find in multiply directories.\n  -e, --extension        set find file extension, default is jar. this option\n                         can specify multiply times to find multiply extension.\n  -E, --extended-regexp  PATTERN is an extended regular expression (*default*)\n  -F, --fixed-strings    PATTERN is a set of newline-separated strings\n  -G, --basic-regexp     PATTERN is a basic regular expression\n  -P, --perl-regexp      PATTERN is a Perl regular expression\n  -i, --ignore-case      ignore case distinctions\n\nOutput control:\n  -a, --absolute-path    always print absolute path of jar file\n  -s, --separator        specify the separator between jar file and zip entry.\n                         default is \\`!'.\n  -L, --files-not-contained-found\n                         print only names of JAR FILEs NOT contained found\n  -l, --files-contained-found\n                         print only names of JAR FILEs contained found\n  -R, --no-find-progress do not display responsive find progress\n\nMiscellaneous:\n  -h, --help             display this help and exit\n  -V, --version          display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\ndirs=()\nextensions=()\nargs=()\n\nseparator='!'\nregex_mode=-E\nuse_absolute_path=false\nshow_responsive=true\nonly_print_file_name=false\n\nwhile (($# > 0)); do\n  case \"$1\" in\n  -d | --dir)\n    dirs=(${dirs[@]:+\"${dirs[@]}\"} \"$2\")\n    shift 2\n    ;;\n  -e | --extension)\n    extensions=(${extensions[@]:+\"${extensions[@]}\"} \"$2\")\n    shift 2\n    ;;\n  -E | --extended-regexp)\n    regex_mode=-E\n    shift\n    ;;\n  -F | --fixed-strings)\n    regex_mode=-F\n    shift\n    ;;\n  -G | --basic-regexp)\n    regex_mode=-G\n    shift\n    ;;\n  -P | --perl-regexp)\n    regex_mode=-P\n    shift\n    ;;\n  -i | --ignore-case)\n    ignore_case_option=-i\n    shift\n    ;;\n  -a | --absolute-path)\n    use_absolute_path=true\n    shift\n    ;;\n  # support the legacy typo option name --seperator for compatibility\n  -s | --separator | --seperator)\n    separator=$2\n    shift 2\n    ;;\n  -L | --files-not-contained-found)\n    only_print_file_name=true\n    print_matched_files=false\n    shift\n    ;;\n  -l | --files-contained-found)\n    only_print_file_name=true\n    print_matched_files=true\n    shift\n    ;;\n  -R | --no-find-progress)\n    show_responsive=false\n    shift\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    args=(${args[@]:+\"${args[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    args=(${args[@]:+\"${args[@]}\"} \"$1\")\n    shift\n    ;;\n  esac\ndone\n\nreadonly separator regex_mode ignore_case_option use_absolute_path only_print_file_name print_matched_files show_responsive args\n\n# shellcheck disable=SC2178\ndirs=${dirs:-.}\n# shellcheck disable=SC2178\nreadonly extensions=${extensions:-jar}\n\n((${#args[@]} == 0)) && die -h \"requires file pattern!\"\n((${#args[@]} > 1)) && die -h \"more than 1 file pattern: ${args[*]}\"\nreadonly pattern=${args[0]}\n\ntmp_dirs=()\nfor d in \"${dirs[@]}\"; do\n  [ -e \"$d\" ] || die \"file $d(specified by option -d): No such file or directory!\"\n  [ -d \"$d\" ] || die \"file $d(specified by option -d) exists but is not a directory!\"\n  [ -r \"$d\" ] || die \"directory $d(specified by option -d) exists but is not readable!\"\n\n  # convert dirs to Absolute Path if has option -a, --absolute-path\n  $use_absolute_path && tmp_dirs=(${tmp_dirs[@]:+\"${tmp_dirs[@]}\"} \"$(cd \"$d\" && pwd)\")\ndone\n# set dirs to Absolute Path\n$use_absolute_path && dirs=(\"${tmp_dirs[@]}\")\nreadonly dirs\nunset d tmp_dirs\n\n# convert extensions to find -iname options\nfind_iname_options=()\nfor e in \"${extensions[@]}\"; do\n  find_iname_options=(${find_iname_options[@]:+\"${find_iname_options[@]}\" -o} -iname \"*.$e\")\ndone\nreadonly find_iname_options\nunset e\n\n################################################################################\n# Check the existence of command for listing zip entry!\n################################################################################\n\n__prepareCommandToListZipEntries() {\n  # `zipinfo -1`/`unzip -Z1` is ~25 times faster than `jar tf`, find zipinfo/unzip command first.\n  #\n  # How to list files in a zip without extra information in command line\n  # https://unix.stackexchange.com/a/128304/136953\n\n  if type -P zipinfo &>/dev/null; then\n    command_to_list_zip_entries=(zipinfo -1)\n    is_use_zip_cmd_to_list_zip_entries=true\n  elif type -P unzip &>/dev/null; then\n    command_to_list_zip_entries=(unzip -Z1)\n    is_use_zip_cmd_to_list_zip_entries=true\n  elif [ -n \"$JAVA_HOME\" ]; then\n    # search jar command under JAVA_HOME\n    if [ -f \"$JAVA_HOME/bin/jar\" ]; then\n      [ -x \"$JAVA_HOME/bin/jar\" ] || die \"found \\$JAVA_HOME/bin/jar($JAVA_HOME/bin/jar) is NOT executable!\"\n      command_to_list_zip_entries=(\"$JAVA_HOME/bin/jar\" tf)\n    elif [ -f \"$JAVA_HOME/../bin/jar\" ]; then\n      [ -x \"$JAVA_HOME/../bin/jar\" ] || die \"found \\$JAVA_HOME/../bin/jar($JAVA_HOME/../bin/jar) is NOT executable!\"\n      command_to_list_zip_entries=(\"$JAVA_HOME/../bin/jar\" tf)\n    fi\n    is_use_zip_cmd_to_list_zip_entries=false\n  elif type -P jar &>/dev/null; then\n    # search jar command under PATH\n    command_to_list_zip_entries=(jar tf)\n    is_use_zip_cmd_to_list_zip_entries=false\n  else\n    die \"command to list zip entries NOT found : zipinfo, unzip or jar!\"\n  fi\n\n  readonly command_to_list_zip_entries is_use_zip_cmd_to_list_zip_entries\n}\n__prepareCommandToListZipEntries\n\nlistZipEntries() {\n  local zip_file=$1 msg\n\n  if $is_use_zip_cmd_to_list_zip_entries; then\n    # How to check if zip file is empty in bash\n    # https://superuser.com/questions/438878\n    msg=$(\"${command_to_list_zip_entries[@]}\" -t \"$zip_file\" 2>&1) || {\n      # NOTE:\n      # if list emtpy zip file by zipinfo/unzip command,\n      # exit code is 1, and print 'Empty zipfile.'\n      if [ \"$msg\" != 'Empty zipfile.' ]; then\n        clearResponsiveMessage\n        redPrint \"fail to list zip entries of $zip_file, ignored: $msg\" >&2\n      fi\n      return\n    }\n  fi\n\n  \"${command_to_list_zip_entries[@]}\" \"$zip_file\" || {\n    clearResponsiveMessage\n    redPrint \"fail to list zip entries of $zip_file, ignored!\" >&2\n  }\n}\n\n################################################################################\n# find logic\n################################################################################\n\nsearchJarFiles() {\n  printResponsiveMessage \"searching jars under dir ${dirs[*]} , ...\"\n\n  local jar_files total_jar_count\n\n  jar_files=$(find \"${dirs[@]}\" \"${find_iname_options[@]}\" -type f)\n  [ -n \"$jar_files\" ] || die \"${extensions[*]} file NOT found!\"\n\n  total_jar_count=$(printf '%s\\n' \"$jar_files\" | wc -l)\n  # remove white space, because the `wc -l` output on mac contains white space!\n  total_jar_count=${total_jar_count//[[:space:]]/}\n\n  echo \"$total_jar_count\"\n  printf '%s\\n' \"$jar_files\"\n}\n\nreadonly JAR_COLOR='\\e[1;35m' SEP_COLOR='\\e[1;32m'\n__outputResultOfJarFile() {\n  local jar_file=$1 file\n  # shellcheck disable=SC2206\n  local grep_opt_args=(\"$regex_mode\" ${ignore_case_option:-} ${grep_color_option:-} -- \"$pattern\")\n\n  if $only_print_file_name; then\n    local matched=false\n    # NOTE: Do NOT use -q flag with grep:\n    #   With the -q flag the grep program will stop immediately when the first line of data matches.\n    #   Normally you shouldn't use -q in a pipeline like this\n    #   unless you are sure the program at the other end can handle SIGPIPE.\n    # more info see:\n    # - https://stackoverflow.com/questions/19120263/why-exit-code-141-with-grep-q\n    # - https://unix.stackexchange.com/questions/305547/broken-pipe-when-grepping-output-but-only-with-i-flag\n    # - http://www.pixelbeat.org/programming/sigpipe_handling.html\n    grep -c \"${grep_opt_args[@]}\" &>/dev/null && matched=true\n\n    [ \"$print_matched_files\" != \"$matched\" ] && return\n\n    clearResponsiveMessage\n    if [ -t 1 ]; then\n      printf \"$JAR_COLOR%s$COLOR_RESET\\n\" \"$jar_file\"\n    else\n      printf '%s\\n' \"$jar_file\"\n    fi\n  else\n    {\n      # Prevent grep from exiting in case of no match\n      # https://unix.stackexchange.com/questions/330660\n      grep \"${grep_opt_args[@]}\" || true\n    } | while IFS= read -r file; do\n      clearResponsiveMessage\n      if [ -t 1 ]; then\n        printf \"$JAR_COLOR%s$SEP_COLOR%s$COLOR_RESET%s\\n\" \"$jar_file\" \"$separator\" \"$file\"\n      else\n        printf '%s\\n' \"$jar_file$separator$file\"\n      fi\n    done\n  fi\n}\n\nfindInJarFiles() {\n  [ -t 1 ] && local -r grep_color_option='--color=always'\n  local counter=1 total_jar_count jar_file\n\n  read -r total_jar_count\n  while IFS= read -r jar_file; do\n    printResponsiveMessage \"finding in jar($((counter++))/$total_jar_count): $jar_file\"\n    listZipEntries \"$jar_file\" | __outputResultOfJarFile \"$jar_file\"\n  done\n\n  clearResponsiveMessage\n}\n\nsearchJarFiles | findInJarFiles\n"
  },
  {
    "path": "bin/rp",
    "content": "#!/usr/bin/env bash\n# @Function\n# convert to Relative Path.\n#\n# @Usage\n#   # if 1 argument, print relative path to current dir.\n#   $ ./rp /etc/apache2/httpd.conf\n#   # if more than 1 argument, print relative path to last argument.\n#   $ ./rp a.txt ../b.txt /etc/passwd /etc/apache2\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-ap-and-rp\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nportableRelPath() {\n  local file=$1 relTo=$2 uname\n\n  uname=$(uname)\n  case \"$uname\" in\n  Linux* | CYGWIN* | MINGW*)\n    realpath \"$f\" --relative-to=\"$relTo\"\n    ;;\n  Darwin*)\n    local py_args=(-c 'import os, sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' \"$file\" \"$relTo\")\n    if type -P grealpath >/dev/null; then\n      grealpath \"$f\" --relative-to=\"$relTo\"\n    elif type -P python3 >/dev/null; then\n      python3 \"${py_args[@]}\"\n    elif type -P python >/dev/null; then\n      python \"${py_args[@]}\"\n    else\n      die \"fail to find command(grealpath/python3/python) to get relative path!\"\n    fi\n    ;;\n  *)\n    die \"uname($uname) NOT support!\"\n    ;;\n  esac\n}\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [FILE]...\nconvert to Relative Path.\n\nExample:\n  $PROG path    # relative to current dir\n  $PROG path1 relativeToPath\n  $PROG */*.c relativeToPath\n\nOptions:\n  -h, --help      display this help and exit\n  -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\nfiles=()\nwhile (($# > 0)); do\n  case \"$1\" in\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    files=(${files[@]:+\"${files[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    # if not option, treat all follow files as args\n    files=(${files[@]:+\"${files[@]}\"} \"$@\")\n    break\n    ;;\n  esac\ndone\n\n((${#files[@]} == 0)) && die -h \"requires at least one argument!\"\n\nif ((${#files[@]} == 1)); then\n  relativeTo=.\nelse\n  argc=${#files[@]}\n\n  # Get last argument\n  relativeTo=${files[argc - 1]}\n  files=(\"${files[@]:0:argc-1}\")\nfi\n\n[ -f \"$relativeTo\" ] && relativeTo=$(dirname -- \"$relativeTo\")\n[ -e \"$relativeTo\" ] || die \"relativeTo dir($relativeTo): No such file or directory!\"\n\nreadonly files relativeTo\n\n################################################################################\n# biz logic\n################################################################################\n\nhas_error=false\n\nfor f in \"${files[@]}\"; do\n  if [ -e \"$f\" ]; then\n    portableRelPath \"$f\" \"$relativeTo\"\n  else\n    redPrint \"$PROG: $f: No such file or directory!\" >&2\n    has_error=true\n  fi\ndone\n\n# set exit status\n! $has_error\n"
  },
  {
    "path": "bin/show-busy-java-threads",
    "content": "#!/usr/bin/env bash\n# @Function\n# Find out the highest cpu consumed threads of java processes, and print the stack of these threads.\n#\n# @Usage\n#   $ ./show-busy-java-threads\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/java.md#-show-busy-java-threads\n# @author Jerry Lee (oldratlee at gmail dot com)\n# @author superhj1987 (superhj1987 at 126 dot com)\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n# choosing between $0 and BASH_SOURCE\n# https://stackoverflow.com/a/35006505/922688\n# How can I get the source directory of a Bash script from within the script itself?\n# https://stackoverflow.com/questions/59895\n# Will $0 always include the path to the script?\n# https://unix.stackexchange.com/questions/119929\nreadonly -a COMMAND_LINE=(\"${BASH_SOURCE[0]}\" \"$@\")\n# CAUTION: env var $USER is not reliable!\n#   $USER may be overwritten; if run command by `sudo -u`, may is not `root`.\n#   more info see https://www.baeldung.com/linux/get-current-user\n#\n# DO NOT declare and assign var(as readonly) in ONE line!\n#   more info see https://github.com/koalaman/shellcheck/wiki/SC2155\nWHOAMI=$(whoami)\nUNAME=$(uname)\nreadonly WHOAMI UNAME\n\n################################################################################\n# util functions\n################################################################################\n\n# NOTE: $'foo' is the escape sequence syntax of bash\nreadonly NL=$'\\n' # new line\n\ncolorPrint() {\n  local color=$1\n  shift\n\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;%sm%s\\e[0m\\n' \"$color\" \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\n__appendToFile() {\n  [[ -n \"$append_file\" && -w \"$append_file\" ]] && printf '%s\\n' \"$*\" >>\"$append_file\"\n  [[ -n \"$store_dir\" && -w \"$store_dir\" ]] && printf '%s\\n' \"$*\" >>\"$store_file_prefix$PROG\"\n}\n\ncolorOutput() {\n  local color=$1\n  shift\n\n  colorPrint \"$color\" \"$*\"\n  __appendToFile \"$*\"\n}\n\n# shellcheck disable=SC2120\nnormalOutput() {\n  printf '%s\\n' \"$*\"\n  __appendToFile \"$*\"\n}\n\nredOutput() {\n  colorOutput 31 \"$*\"\n}\n\ngreenOutput() {\n  colorOutput 32 \"$*\"\n}\n\nyellowOutput() {\n  colorOutput 33 \"$*\"\n}\n\nblueOutput() {\n  colorOutput 36 \"$*\"\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && colorPrint \"1;31\" \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nlogAndRun() {\n  printf '%s\\n' \"$*\"\n  echo\n  \"$@\"\n}\n\nlogAndCat() {\n  printf '%s\\n' \"$*\"\n  echo\n  cat\n}\n\n# Bash RegEx to check floating point numbers from user input\n# https://stackoverflow.com/questions/13790763\nisNonNegativeFloatNumber() {\n  [[ \"$1\" =~ ^[+]?[0-9]+\\.?[0-9]*$ ]]\n}\n\nisNaturalNumber() {\n  [[ \"$1\" =~ ^[+]?[0-9]+$ ]]\n}\n\nisNaturalNumberList() {\n  [[ \"$1\" =~ ^([0-9]+)(,[0-9]+)*$ ]]\n}\n\n# print calling(quoted) command line which is able to copy and paste to rerun safely\n#\n# How to get the complete calling command of a BASH script from inside the script (not just the arguments)\n# https://stackoverflow.com/questions/36625593\nprintCallingCommandLine() {\n  local arg isFirst=true\n  for arg in \"${COMMAND_LINE[@]}\"; do\n    if $isFirst; then\n      isFirst=false\n    else\n      printf ' '\n    fi\n    printf '%q' \"$arg\"\n  done\n  echo\n}\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [delay [count]]\nFind out the highest cpu consumed threads of java processes,\nand print the stack of these threads.\n\nExample:\n  $PROG       # show busy java threads info\n  $PROG 1     # update every 1 second, (stop by eg: CTRL+C)\n  $PROG 3 10  # update every 3 seconds, update 10 times\n\nOutput control:\n  -p, --pid <java pid(s)>   find out the highest cpu consumed threads from\n                            the specified java process.\n                            support pid list(eg: 42,47).\n                            default from all java process.\n  -c, --count <num>         set the thread count to show, default is 5.\n                            set count 0 to show all threads.\n  -a, --append-file <file>  specifies the file to append output as log.\n  -S, --store-dir <dir>     specifies the directory for storing\n                            the intermediate files, and keep files.\n                            default store intermediate files at tmp dir,\n                            and auto remove after run. use this option to keep\n                            files so as to review jstack/top/ps output later.\n  delay                     the delay between updates in seconds.\n  count                     the number of updates.\n                            delay/count arguments imitates the style of\n                            vmstat command.\n\njstack control:\n  -s, --jstack-path <path>  specifies the path of jstack command.\n  -F, --force               set jstack to force a thread dump.\n                            use when jstack does not respond (process is hung).\n  -m, --mix-native-frames   set jstack to print both java and\n                            native frames (mixed mode).\n  -l, --lock-info           set jstack with long listing.\n                            prints additional information about locks.\n\nCPU usage calculation control:\n  -i, --cpu-sample-interval specifies the delay between cpu samples to get\n                            thread cpu usage percentage during this interval.\n                            default is 0.5 (second).\n                            set interval 0 to get the percentage of time spent\n                            running during the *entire lifetime* of a process.\n\nMiscellaneous:\n  -h, --help                display this help and exit.\n  -V, --version             display version information and exit.\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# check os support\n################################################################################\n\n[[ $UNAME = Linux* ]] || die \"only support Linux, not support $UNAME yet!\"\n\n################################################################################\n# parse options\n################################################################################\n\n# DO NOT declare and assign var ARGS(as readonly) in ONE line!\nARGS=$(\n  getopt -n \"$PROG\" -a -o c:p:a:s:S:i:Pd:FmlhV \\\n    -l count:,pid:,append-file:,jstack-path:,store-dir:,cpu-sample-interval:,use-ps,top-delay:,force,mix-native-frames,lock-info,help,version \\\n    -- \"$@\"\n) || die -h\neval set -- \"$ARGS\"\nunset ARGS\n\ncount=5\ncpu_sample_interval=0.5\n\nwhile true; do\n  case \"$1\" in\n  -c | --count)\n    count=$2\n    shift 2\n    ;;\n  -p | --pid)\n    pid_list=$2\n    shift 2\n    ;;\n  -a | --append-file)\n    append_file=$2\n    shift 2\n    ;;\n  -s | --jstack-path)\n    jstack_path=$2\n    shift 2\n    ;;\n  -S | --store-dir)\n    store_dir=$2\n    shift 2\n    ;;\n  # support the legacy option name -P,--use-ps for compatibility\n  -P | --use-ps)\n    cpu_sample_interval=0\n    shift\n    ;;\n  # support the legacy option name -d,--top-delay for compatibility\n  -i | --cpu-sample-interval | -d | --top-delay)\n    cpu_sample_interval=$2\n    shift 2\n    ;;\n  -F | --force)\n    force=-F\n    shift\n    ;;\n  -m | --mix-native-frames)\n    mix_native_frames=-m\n    shift\n    ;;\n  -l | --lock-info)\n    lock_info=-l\n    shift\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    break\n    ;;\n  esac\ndone\n\nreadonly count cpu_sample_interval force mix_native_frames lock_info\nreadonly update_delay=${1:-0}\nisNonNegativeFloatNumber \"$update_delay\" || die \"update delay($update_delay) is not a non-negative float number!\"\n\n[ -z \"$1\" ] && update_count=1 || update_count=${2:-0}\nisNaturalNumber \"$update_count\" || die \"update count($update_count) is not a natural number!\"\nreadonly update_count\n\nif [ -n \"$pid_list\" ]; then\n  pid_list=${pid_list//[[:space:]]/} # delete white space\n  isNaturalNumberList \"$pid_list\" || die \"pid(s)($pid_list) is illegal! example: 42 or 42,99,67\"\nfi\nreadonly pid_list\n\n# check the directory of append-file(-a) mode, create if not existed.\nif [ -n \"$append_file\" ]; then\n  if [ -e \"$append_file\" ]; then\n    [ -f \"$append_file\" ] || die \"$append_file(specified by option -a, for storing run output files) exists but is not a file!\"\n    [ -w \"$append_file\" ] || die \"file $append_file(specified by option -a, for storing run output files) exists but is not writable!\"\n  else\n    append_file_dir=$(dirname -- \"$append_file\")\n    if [ -e \"$append_file_dir\" ]; then\n      [ -d \"$append_file_dir\" ] || die \"directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!\"\n      [ -w \"$append_file_dir\" ] || die \"directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!\"\n    else\n      mkdir -p \"$append_file_dir\" || die \"fail to create directory $append_file_dir(specified by option -a, for storing run output files)!\"\n    fi\n  fi\nfi\nreadonly append_file\n\n# check store directory(-S) mode, create directory if not existed.\nif [ -n \"$store_dir\" ]; then\n  if [ -e \"$store_dir\" ]; then\n    [ -d \"$store_dir\" ] || die \"$store_dir(specified by option -S, for storing output files) exists but is not a directory!\"\n    [ -w \"$store_dir\" ] || die \"directory $store_dir(specified by option -S, for storing output files) exists but is not writable!\"\n  else\n    mkdir -p \"$store_dir\" || die \"fail to create directory $store_dir(specified by option -S, for storing output files)!\"\n  fi\nfi\nreadonly store_dir\n\nisNonNegativeFloatNumber \"$cpu_sample_interval\" || die \"cpu sample interval($cpu_sample_interval) is not a non-negative float number!\"\n\n################################################################################\n# search/check the existence of jstack command\n#\n# search order/priority:\n#    1. from -s option\n#    2. from under env var JAVA_HOME\n#    3. from under env var PATH\n################################################################################\n\nif [ -n \"$jstack_path\" ]; then\n  # 1. check jstack_path set by -s option\n  [ -f \"$jstack_path\" ] || die \"$jstack_path (set by -s option) is NOT found!\"\n  [ -x \"$jstack_path\" ] || die \"$jstack_path (set by -s option) is NOT executable!\"\nelif [ -n \"$JAVA_HOME\" ]; then\n  # 2. search jstack under JAVA_HOME\n  if [ -f \"$JAVA_HOME/bin/jstack\" ]; then\n    [ -x \"$JAVA_HOME/bin/jstack\" ] || die -h \"found \\$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually.\"\n    jstack_path=\"$JAVA_HOME/bin/jstack\"\n  elif [ -f \"$JAVA_HOME/../bin/jstack\" ]; then\n    [ -x \"$JAVA_HOME/../bin/jstack\" ] || die -h \"found \\$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually.\"\n    jstack_path=\"$JAVA_HOME/../bin/jstack\"\n  fi\nelif type -P jstack &>/dev/null; then\n  # 3. search jstack under PATH\n  jstack_path=$(type -P jstack)\n  [ -x \"$jstack_path\" ] || die -h \"found $jstack_path from PATH is NOT executable!${NL}Use -s option set jstack path manually.\"\nelse\n  die -h \"jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${NL}Use -s option set jstack path manually.\"\nfi\nreadonly jstack_path\n\n################################################################################\n# biz logic\n################################################################################\n\n# DO NOT declare and assign var run_timestamp(as readonly) in ONE line!\nrun_timestamp=$(date \"+%Y-%m-%d_%H:%M:%S.%N\")\nreadonly run_timestamp\nreadonly uuid=\"${PROG}_${run_timestamp}_${$}_${RANDOM}\"\n\nreadonly tmp_store_dir=\"/tmp/$uuid\"\nif [ -n \"$store_dir\" ]; then\n  readonly store_file_prefix=\"$store_dir/${run_timestamp}_\"\nelse\n  readonly store_file_prefix=\"$tmp_store_dir/${run_timestamp}_\"\nfi\nmkdir -p \"$tmp_store_dir\"\n\ncleanupWhenExit() {\n  rm -rf \"$tmp_store_dir\" &>/dev/null\n}\ntrap cleanupWhenExit EXIT\n\nheadInfo() {\n  local timestamp=$1\n  colorPrint \"0;34;42\" ================================================================================\n  printf '%s\\n' \"$timestamp [$((update_round_num + 1))/$update_count]: $(printCallingCommandLine)\"\n  colorPrint \"0;34;42\" ================================================================================\n  echo\n}\n\nif [ -n \"$pid_list\" ]; then\n  readonly ps_process_select_options=\"-p $pid_list\"\nelse\n  readonly ps_process_select_options=\"-C java -C jsvc\"\nfi\n\n__die_when_no_java_process_found() {\n  if [ -n \"$pid_list\" ]; then\n    die \"process($pid_list) is not running, or not java process!\"\n  else\n    die 'No java process found!'\n  fi\n}\n\n# output field: pid, thread id(lwp), pcpu, user\n#   order by pcpu(percentage of cpu usage)\n#\n# NOTE:\n# use ps command to find busy thread(cpu usage)\n# cpu usage of ps command is expressed as\n# the percentage of time spent running during the *entire lifetime* of a process,\n# this is not ideal in general.\nfindBusyJavaThreadsByPs() {\n  # 1. sort by %cpu by ps option `--sort -pcpu`\n  #    unfortunately, ps from `procps-ng 3.3.12`, `--sort` does not work properly with other options,\n  #    use\n  #       ps <other options>\n  #    combined\n  #       sort -k3,3nr\n  #    instead of\n  #       ps <other options> --sort -pcpu\n  # 2. use wide output(unlimited width) by ps option `-ww`\n  #    avoid trunk user column to username_fo+ or $uid alike\n\n  # shellcheck disable=SC2206\n  local -a ps_cmd_line=(ps $ps_process_select_options -wwLo 'pid,lwp,pcpu,user' --no-headers)\n  # DO NOT combine var ps_out declaration and assignment in ONE line!\n  #   more info see https://github.com/koalaman/shellcheck/wiki/SC2155\n  local ps_out\n  ps_out=$(\"${ps_cmd_line[@]}\" | sort -k3,3nr)\n  [ -n \"$ps_out\" ] || __die_when_no_java_process_found\n\n  if [ -n \"$store_dir\" ]; then\n    printf '%s\\n' \"$ps_out\" | logAndCat \"${ps_cmd_line[*]} | sort -k3,3nr\" >\"$store_file_prefix$((update_round_num + 1))_ps\"\n  fi\n\n  if ((count > 0)); then\n    printf '%s\\n' \"$ps_out\" | head -n \"$count\"\n  else\n    printf '%s\\n' \"$ps_out\"\n  fi\n}\n\n# top with output field: thread id, %cpu\n__top_threadId_cpu() {\n  # DO NOT combine var java_pid_list declaration and assignment in ONE line!\n  local java_pid_list\n  # shellcheck disable=SC2086\n  java_pid_list=$(ps $ps_process_select_options -o pid --no-headers)\n  [ -n \"$java_pid_list\" ] || __die_when_no_java_process_found\n  # shellcheck disable=SC2086\n  java_pid_list=$(echo $java_pid_list | tr ' ' ,) # join with ,\n\n  # 1. sort by %cpu by top option `-o %CPU`\n  #    unfortunately, top version 3.2 does not support -o option(supports from top version 3.3+),\n  #    use\n  #       HOME=$tmp_store_dir top -H -b -n 1\n  #    combined\n  #       sort\n  #    instead of\n  #       HOME=$tmp_store_dir top -H -b -n 1 -o %CPU\n  # 2. change HOME env var when run top,\n  #    so as to prevent top command output format being change by .toprc user config file unexpectedly\n  # 3. use option `-d 0.5`(update interval 0.5 second) and `-n 2`(update 2 times),\n  #    and use second time update data to get cpu percentage of thread in 0.5 second interval\n  # 4. top v3.3, there is 1 black line between 2 update;\n  #    but top v3.2, there is 2 blank lines between 2 update!\n  local -a top_cmd_line=(top -H -b -d \"$cpu_sample_interval\" -n 2 -p \"$java_pid_list\")\n  # DO NOT combine var top_out declaration and assignment in ONE line!\n  local top_out\n  top_out=$(HOME=$tmp_store_dir \"${top_cmd_line[@]}\")\n  if [ -n \"$store_dir\" ]; then\n    printf '%s\\n' \"$top_out\" | logAndCat \"${top_cmd_line[@]}\" >\"$store_file_prefix$((update_round_num + 1))_top\"\n  fi\n\n  # DO NOT combine var result_threads_top_info declaration and assignment in ONE line!\n  local result_threads_top_info\n  result_threads_top_info=$(printf '%s\\n' \"$top_out\" | awk '{\n        # from text line to empty line, increase block index\n        if (previousLine && !$0) blockIndex++\n        # only print 4th text block(blockIndex == 3), aka. process info of second top update\n        if (blockIndex == 3 && $1 ~ /^[0-9]+$/)\n          print $1, $9  # $1 is thread id field, $9 is %cpu field\n        previousLine = $0\n      }')\n  [ -n \"$result_threads_top_info\" ] || __die_when_no_java_process_found\n\n  printf '%s\\n' \"$result_threads_top_info\" | sort -k2,2nr\n}\n\n__complete_pid_user_by_ps() {\n  # ps output field: pid, thread id(lwp), user\n  # shellcheck disable=SC2206\n  local -a ps_cmd_line=(ps $ps_process_select_options -wwLo 'pid,lwp,user' --no-headers)\n  # DO NOT combine var ps_out declaration and assignment in ONE line!\n  local ps_out\n  ps_out=$(\"${ps_cmd_line[@]}\")\n  if [ -n \"$store_dir\" ]; then\n    printf '%s\\n' \"$ps_out\" | logAndCat \"${ps_cmd_line[@]}\" >\"$store_file_prefix$((update_round_num + 1))_ps\"\n  fi\n\n  local idx=0 threadId pcpu output_fields\n  while read -r threadId pcpu; do\n    ((count <= 0 || idx < count)) || break\n\n    # output field: pid, threadId, pcpu, user\n    output_fields=$(printf '%s\\n' \"$ps_out\" | awk -v \"threadId=$threadId\" -v \"pcpu=$pcpu\" '$2==threadId {\n          print $1, threadId, pcpu, $3; exit\n        }')\n    if [ -n \"$output_fields\" ]; then\n      ((idx++))\n      printf '%s\\n' \"$output_fields\"\n    fi\n  done\n}\n\n# output format is same as function findBusyJavaThreadsByPs\nfindBusyJavaThreadsByTop() {\n  __top_threadId_cpu | __complete_pid_user_by_ps\n}\n\nprintStackOfThreads() {\n  local idx=0 pid threadId pcpu user threadId0x\n  while read -r pid threadId pcpu user; do\n    printf -v threadId0x '%#x' \"$threadId\"\n\n    ((idx++ > 0)) && normalOutput\n    local jstackFile=\"$store_file_prefix$((update_round_num + 1))_jstack_$pid\"\n    [ -f \"$jstackFile\" ] || {\n      # shellcheck disable=SC2206\n      local -a jstack_cmd_line=(\"$jstack_path\" $force $mix_native_frames $lock_info $pid)\n      if [ \"$user\" = \"$WHOAMI\" ]; then\n        # run without sudo, when java process user is current user\n        logAndRun \"${jstack_cmd_line[@]}\" >\"$jstackFile\"\n      elif ((UID == 0)); then\n        # if java process user is not current user, must run jstack with sudo\n        logAndRun sudo -u \"$user\" \"${jstack_cmd_line[@]}\" >\"$jstackFile\"\n      else\n        # current user is not root user, so can not run with sudo; print error message and rerun suggestion\n        redOutput \"[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user).\"\n        redOutput \"User of java process($user) is not current user($WHOAMI), need sudo to rerun:\"\n        yellowOutput \"    sudo $(printCallingCommandLine)\"\n        continue\n      fi || {\n        redOutput \"[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user).\"\n        rm \"$jstackFile\" &>/dev/null\n        continue\n      }\n    }\n\n    blueOutput \"[$idx] Busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user):\"\n\n    if [ -n \"$mix_native_frames\" ]; then\n      local sed_script=\"/--------------- $threadId ---------------/,/^---------------/ {\n          /--------------- $threadId ---------------/b # skip first separator line\n          /^---------------/d # delete second separator line\n          p\n        }\"\n    elif [ -n \"$force\" ]; then\n      local sed_script=\"/^Thread $threadId:/,/^$/ {\n          /^$/d; p # delete end separator line\n        }\"\n    else\n      local sed_script=\"/ nid=($threadId0x|$threadId) /,/^$/ {\n          /^$/d; p # delete end separator line\n        }\"\n    fi\n    sed \"$sed_script\" -n -r \"$jstackFile\" | tee ${append_file:+-a \"$append_file\"} ${store_dir:+-a \"$store_file_prefix$PROG\"}\n  done\n}\n\nmain() {\n  local update_round_num timestamp\n  # if update_count <= 0, infinite loop till user interrupted (eg: CTRL+C)\n  for ((update_round_num = 0; update_count <= 0 || update_round_num < update_count; ++update_round_num)); do\n    ((update_round_num > 0)) && {\n      sleep \"$update_delay\"\n      normalOutput\n    }\n\n    timestamp=$(date \"+%Y-%m-%d %H:%M:%S.%N\")\n    [[ -n \"$append_file\" || -n \"$store_dir\" ]] && headInfo \"$timestamp\" |\n      tee ${append_file:+-a \"$append_file\"} ${store_dir:+-a \"$store_file_prefix$PROG\"} >/dev/null\n    ((update_count != 1)) && headInfo \"$timestamp\"\n\n    if [ \"$cpu_sample_interval\" = 0 ]; then\n      findBusyJavaThreadsByPs\n    else\n      findBusyJavaThreadsByTop\n    fi | printStackOfThreads\n  done\n}\n\nmain\n"
  },
  {
    "path": "bin/show-duplicate-java-classes",
    "content": "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# @Function\n# Find duplicate classes among java lib dirs and class dirs.\n#\n# @Usage\n#   $ show-duplicate-java-classes # search jars from current dir\n#   $ show-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2\n#   $ show-duplicate-java-classes -c path/to/class_dir1 -c /path/to/class_dir2\n#   $ show-duplicate-java-classes -c path/to/class_dir1 path/to/lib_dir1\n#   $ show-duplicate-java-classes -L path/to/lib_dir1 # search jars in the subdirectories of lib dir\n#   $ show-duplicate-java-classes -J path/to/lib_dir1 # search jars in the jar file\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/java.md#-show-duplicate-java-classes\n# @author tg123 (farmer1992 at gmail dot com)\n# @author Jerry Lee (oldratlee at gmail dot com)\n\n__author__ = 'tg123'\n\nimport os\nimport re\nimport sys\nfrom glob import glob\nfrom io import BytesIO\nfrom optparse import OptionParser\nfrom os import walk\nfrom os.path import exists, isdir, relpath\nfrom zipfile import BadZipfile, ZipFile\n\n################################################################################\n# utils functions\n################################################################################\nPROG_VERSION = '3.x-dev'\n\n# How to delete line with echo?\n# https://unix.stackexchange.com/questions/26576\n#\n# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php\n# In particular, to clear from the cursor position to the beginning of the line:\n# echo -e \"\\033[1K\"\n# Or everything on the line, regardless of cursor position:\n# echo -e \"\\033[2K\"\n__clear_line = '\\033[2K\\r'\n__show_responsive = True\n\n\ndef __get_terminal_columns_of_stderr():\n    \"\"\"\n    Rewritten for stderr from <shutil.get_terminal_size>\n    \"\"\"\n    try:\n        columns, _ = os.get_terminal_size(sys.stderr.fileno())\n    except (AttributeError, ValueError, OSError):\n        columns = 0\n\n    return columns\n\n\ndef print_responsive_message(msg):\n    if not __show_responsive or not sys.stderr.isatty():\n        return\n    columns = __get_terminal_columns_of_stderr()\n    if columns <= 0:\n        return\n\n    print(__clear_line + msg[:columns], end='', file=sys.stderr)\n\n\ndef clear_responsive_message():\n    if not __show_responsive or not sys.stderr.isatty():\n        return\n    print(__clear_line, end='', file=sys.stderr)\n\n\ndef print_error(msg):\n    clear_responsive_message()\n    print(msg, file=sys.stderr)\n\n\ndef print_box_message(msg):\n    print()\n    print('=' * 80)\n    print(msg)\n    print('=' * 80)\n\n\ndef str_len(x):\n    return len(str(x))\n\n\n# issue 32790: Keep trailing zeros in precision for string format option g - Python tracker\n# https://bugs.python.org/issue32790\ndef percent_str(num):\n    \"\"\"\n    Input            => Output\n    1.4545 / 10 **-1 => 1455%\n    1.4545 / 10 ** 0 => 145%\n    1.4545 / 10 ** 1 => 14.5%\n    1.4545 / 10 ** 2 => 1.45%\n    1.4545 / 10 ** 3 => 0.145%\n    1.4545 / 10 ** 4 => 0.015%\n    1.4545 / 10 ** 5 => 0.001%\n    1.4545 / 10 ** 6 => 0.000%\n    1.4545 / 10 ** 7 => 0.000%\n    \"\"\"\n    num = num * 100\n    if num >= 100:\n        return '%.0f%%' % num\n    elif num >= 10:\n        return '%.1f%%' % num\n    elif num >= 1:\n        return '%.2f%%' % num\n    else:\n        return '%.3f%%' % num\n\n\ndef list_jar_file_under_lib_dirs(lib_dirs, recursive):\n    jar_files = set()\n\n    max_idx_str_len = str_len(len(lib_dirs))\n    for idx, lib_dir in enumerate(lib_dirs, start=1):\n        print_responsive_message('list jar file under lib dir(%*s/%s): %s' % (\n            max_idx_str_len, idx, len(lib_dirs), lib_dir))\n\n        if not exists(lib_dir):\n            print_error('WARN: lib dir %s not exists, ignored!' % lib_dir)\n            continue\n\n        if not isdir(lib_dir):\n            jar_files.add(lib_dir)\n            continue\n\n        if not recursive:\n            jar_files |= {p for p in glob(lib_dir + '/*.jar')}\n            continue\n\n        jar_files |= {\n            dir_path + '/' + filename\n            for dir_path, _, file_names in walk(lib_dir)\n            for filename in file_names if filename.lower().endswith('.jar')\n        }\n\n    return jar_files\n\n\ndef list_class_under_jar_file(jar_file, recursive, progress):\n    \"\"\"\n    :return: map: jar_jar_path('a.jar!b.jar!c.jar') -> classes\n    \"\"\"\n    index = 0\n\n    def list_zip_in_zip(jar_jar_path, zf):\n        nonlocal index\n        index += 1\n        index_marker = ''\n        if recursive:\n            index_marker = ' #%3s' % index\n        print_responsive_message('list class under jar file(%*s/%s%s): %s' % (\n            str_len(progress[1]), progress[0], progress[1], index_marker, jar_jar_path))\n\n        ret = {}\n        classes = {f for f in zf.namelist() if f.lower().endswith('.class')}\n        ret[jar_jar_path] = classes\n        if not recursive:\n            return ret\n\n        jars_in_jar = {f for f in zf.namelist() if f.lower().endswith('.jar')}\n        for jar in jars_in_jar:\n            next_jar_jar_path = jar_jar_path + '!' + jar\n            try:\n                with ZipFile(BytesIO(zf.read(jar))) as f:\n                    ret.update(list_zip_in_zip(next_jar_jar_path, f))\n            except BadZipfile as e:\n                print_error('WARN: %s is bad zip file(%s), ignored!' % (next_jar_jar_path, e))\n\n        return ret\n\n    try:\n        with ZipFile(jar_file) as zip_file:\n            return list_zip_in_zip(jar_file, zip_file)\n    except BadZipfile as error:\n        print_error('WARN: %s is bad zip file(%s), ignored!' % (jar_file, error))\n        return {}\n\n\ndef list_class_under_class_dir(class_dir, progress):\n    print_responsive_message('list class under class dir(%*s/%s): %s' % (\n        str_len(progress[1]), progress[0], progress[1], class_dir))\n\n    if not exists(class_dir):\n        print_error('WARN: class dir %s not exists, ignored!' % class_dir)\n        return {}\n    if not isdir(class_dir):\n        print_error('WARN: class dir %s is not dir, ignored!' % class_dir)\n        return {}\n\n    return {relpath(dir_path + '/' + filename, class_dir)\n            for dir_path, _, file_names in walk(class_dir)\n            for filename in file_names if filename.lower().endswith('.class')}\n\n\ndef collect_class_path_to_classes(class_dirs, jar_files, recursive_jar):\n    class_path_to_classes = {}\n    total_count = len(jar_files) + len(class_dirs)\n    index = 0\n\n    # list all classes in jar files\n    for jar_file in jar_files:\n        index += 1\n        class_path_to_classes.update(\n            list_class_under_jar_file(jar_file, recursive=recursive_jar, progress=(index, total_count)))\n    # list all classes in class dirs\n    for class_dir in class_dirs:\n        index += 1\n        class_path_to_classes[class_dir] = list_class_under_class_dir(class_dir, progress=(index, total_count))\n    return class_path_to_classes\n\n\ndef invert_as_class_to_class_paths(class_path_to_classes):\n    class_to_class_paths = {}\n    for class_path, classes in class_path_to_classes.items():\n        for clazz in classes:\n            class_to_class_paths.setdefault(clazz, set()).add(class_path)\n    return class_to_class_paths\n\n\n################################################################################\n# biz functions\n################################################################################\n\n__java9_module_file_pattern = re.compile(r'(^|.*/)module-info\\.class$')\n\n\ndef find_duplicate_classes(class_to_class_paths):\n    class_paths_to_duplicate_classes = {}\n\n    for clazz, class_paths in class_to_class_paths.items():\n        # skip java 9 module-info files\n        if len(class_paths) == 1 or __java9_module_file_pattern.match(clazz):\n            continue\n\n        classes = class_paths_to_duplicate_classes.setdefault(frozenset(class_paths), set())\n        classes.add(clazz)\n\n    return class_paths_to_duplicate_classes\n\n\ndef print_duplicate_classes_info(class_paths_to_duplicate_classes, class_path_to_classes):\n    if not class_paths_to_duplicate_classes:\n        print('COOL! No duplicate classes found!')\n        return\n\n    duplicate_classes_total_count = sum(len(dcs) for dcs in class_paths_to_duplicate_classes.values())\n    class_paths_total_count = sum(len(cps) for cps in class_paths_to_duplicate_classes)\n    print('Found %s duplicate classes in %s class paths and %s class path sets:' % (\n        duplicate_classes_total_count, class_paths_total_count, len(class_paths_to_duplicate_classes)))\n\n    # sort key(class_paths) and value(duplicate_classes)\n    class_paths_to_duplicate_classes = [(sorted(cps), sorted(dcs))\n                                        for cps, dcs in class_paths_to_duplicate_classes.items()]\n    # sort kv pairs\n    #\n    # sort by multiple keys:\n    #    1. class paths count, *descending*; aka. sort by len(item[0]) *reverse=True*\n    #    2. duplicate classes count, *descending*; aka. sort by len(item[1]) *reverse=True*\n    #    3. class paths, ascending; aka. sort by item[0]\n    # sort also ensure output consistent for same input.\n    #\n    # How to sort objects by multiple keys in Python?\n    # https://stackoverflow.com/questions/1143671\n    # Sort a list by multiple attributes?\n    # https://stackoverflow.com/questions/4233476\n    #\n    # use - operator of number key for reverse sort key\n    class_paths_to_duplicate_classes.sort(key=lambda item: (-len(item[0]), -len(item[1]), item[0]))\n\n    max_idx_str_len = str_len(len(class_paths_to_duplicate_classes))\n    for idx, (class_paths, classes) in enumerate(class_paths_to_duplicate_classes, start=1):\n        duplicate_ratio = len(classes) / min((len(class_path_to_classes[cp]) for cp in class_paths))\n        print('[%*s] found %s(%s) duplicate classes in %s class paths:' % (\n            max_idx_str_len, idx, len(classes), percent_str(duplicate_ratio), len(class_paths)))\n\n        max_class_path_idx_str_len = str_len(len(class_paths))\n        max_classes_count_str_len = str_len(max(len(class_path_to_classes[cp]) for cp in class_paths))\n        for i, cp in enumerate(class_paths, start=1):\n            print('    %*s: (contain %*s classes) %s' % (\n                max_class_path_idx_str_len, i, max_classes_count_str_len, len(class_path_to_classes[cp]), cp))\n\n    print_box_message('Duplicate classes detail info:')\n    for idx, (class_paths, classes) in enumerate(class_paths_to_duplicate_classes, start=1):\n        print('[%*s] found %s duplicate classes in %s class paths %s :' % (\n            max_idx_str_len, idx, len(classes), len(class_paths), ' '.join(class_paths)))\n\n        max_class_idx_str_len = str_len(len(classes))\n        for i, c in enumerate(classes, start=1):\n            print('    %*s: %s' % (max_class_idx_str_len, i, c))\n\n\ndef print_class_paths_info(class_path_to_classes):\n    if not class_path_to_classes:\n        return\n\n    max_idx_str_len = str_len(len(class_path_to_classes))\n    max_classes_count_str_len = str_len(max(len(classes) for classes in class_path_to_classes.values()))\n\n    class_path_to_classes = sorted(class_path_to_classes.items(), key=lambda item: item[0])\n    print_box_message('Find in %s class paths:' % len(class_path_to_classes))\n    for idx, (cp, classes) in enumerate(class_path_to_classes, start=1):\n        print('%*s: (contain %*s classes) %s' % (\n            max_idx_str_len, idx, max_classes_count_str_len, len(classes), cp))\n\n\ndef main():\n    option_parser = OptionParser(\n        usage='%prog [OPTION]...'\n              ' [-c class-dir1 [-c class-dir2] ...]'\n              ' [lib-dir1|jar-file1 [lib-dir2|jar-file2] ...]'\n              '\\nFind duplicate classes among java lib dirs and class dirs.'\n              '\\n\\nExamples:'\n              '\\n  %prog  # search jars from current dir'\n              '\\n  %prog path/to/lib_dir1 /path/to/lib_dir2'\n              '\\n  %prog -c path/to/class_dir1 -c /path/to/class_dir2'\n              '\\n  %prog -c path/to/class_dir1 path/to/lib_dir1'\n              '\\n  %prog -L path/to/lib_dir1'\n              '\\n  %prog -J path/to/lib_dir1',\n        version='%prog ' + PROG_VERSION)\n    option_parser.add_option('-L', '--recursive-lib', dest='recursive_lib', default=False,\n                             action='store_true', help='search jars in the sub-directories of lib dir')\n    option_parser.add_option('-J', '--recursive-jar', dest='recursive_jar', default=False,\n                             action='store_true', help='search jars in the jar file')\n    option_parser.add_option('-c', '--class-dir', dest='class_dirs', default=[],\n                             action='append', help='add class dir')\n    option_parser.add_option('-R', '--no-find-progress', dest='show_responsive', default=True,\n                             action='store_false', help='do not display responsive find progress')\n\n    options, lib_dirs = option_parser.parse_args()\n    class_dirs = options.class_dirs\n    if not lib_dirs and not class_dirs:\n        lib_dirs = ['.']\n    global __show_responsive\n    __show_responsive = options.show_responsive\n\n    jar_files = list_jar_file_under_lib_dirs(lib_dirs, recursive=options.recursive_lib)\n    if not jar_files and not class_dirs:\n        clear_responsive_message()\n        print('search no jar files under lib dirs, and class dirs is absent.')\n        return 0\n    class_path_to_classes = collect_class_path_to_classes(class_dirs, jar_files, options.recursive_jar)\n    if all(not classes for classes in class_path_to_classes.values()):\n        clear_responsive_message()\n        print('find no class files in jar files or class dirs.')\n        return 0\n\n    print_responsive_message('find duplicate classes...')\n    class_to_class_paths = invert_as_class_to_class_paths(class_path_to_classes)\n    class_paths_to_duplicate_classes = find_duplicate_classes(class_to_class_paths)\n\n    clear_responsive_message()\n    print_duplicate_classes_info(class_paths_to_duplicate_classes, class_path_to_classes)\n    print_class_paths_info(class_path_to_classes)\n\n    return int(bool(class_paths_to_duplicate_classes))\n\n\nif __name__ == '__main__':\n    exit(main())\n"
  },
  {
    "path": "bin/taoc",
    "content": "#!/usr/bin/env bash\n# @Function\n# tac lines colorfully. taoc means coat(*CO*lorful c*AT*) in reverse(last line first).\n#\n# @Usage\n#   $ echo -e 'Hello\\nWorld' | taoc\n#   $ taoc /path/to/file1\n#   $ taoc /path/to/file1 /path/to/file2\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-coat\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# parse options\n################################################################################\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [FILE]...\ntac lines colorfully.\n\nSupport options:\n  --help     display this help and exit\n  --version  output version information and exit\nAll other options and arguments are delegated to command tac,\nmore info see the help/man of command tac(e.g. tac --help).\ntac executable: $(type -P tac)\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s version: %s\\n' \"$PROG\" \"$PROG_VERSION\"\n  printf 'tac executable: %s\\n' \"$(type -P tac)\"\n  exit\n}\n\nargs=(\"$@\")\n# check arguments in reverse, so last option wins.\nfor ((idx = $# - 1; idx >= 0; --idx)); do\n  [ \"${args[idx]}\" = --help ] && usage\n  [ \"${args[idx]}\" = --version ] && progVersion\ndone\nunset args idx\n\n################################################################################\n# biz logic\n################################################################################\n\n# if stdout is not a terminal, use `tac` directly.\n#   '-t' check: is a terminal?\n#   check isatty in bash https://stackoverflow.com/questions/10022323\n[ -t 1 ] || exec tac \"$@\"\n\nreadonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)\nCOLOR_INDEX=0\n# CAUTION: print content WITHOUT new line\nrotateColorPrint() {\n  local content=$*\n  # skip color for white space\n  if [[ $content =~ ^[[:space:]]*$ ]]; then\n    printf %s \"$content\"\n  else\n    local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}\n    printf '\\e[1;%sm%s\\e[0m' \"$color\" \"$content\"\n  fi\n}\n\nrotateColorPrintln() {\n  # NOTE: $'foo' is the escape sequence syntax of bash\n  rotateColorPrint \"$*\"$'\\n'\n}\n\ncolorLines() {\n  local line\n  # Bash read line does not read leading spaces\n  # https://stackoverflow.com/questions/29689172\n  while IFS= read -r line; do\n    rotateColorPrintln \"$line\"\n  done\n  # How to use `while read` (Bash) to read the last line in a file\n  #   if there’s no newline at the end of the file?\n  # https://stackoverflow.com/questions/4165135\n  [ -z \"$line\" ] || rotateColorPrint \"$line\"\n}\n\ntac \"$@\" | colorLines\n"
  },
  {
    "path": "bin/tcp-connection-state-counter",
    "content": "#!/usr/bin/env bash\n# @Function\n# show count of tcp connection stat.\n#\n# @Usage\n#   $ ./tcp-connection-state-counter\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-tcp-connection-state-counter\n# @author Jerry Lee (oldratlee at gmail dot com)\n# @author @sunuslee (sunuslee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# parse options\n################################################################################\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]...\nshow count of tcp connection stat.\n\nExample:\n    $PROG\n\nOptions:\n    -h, --help      display this help and exit\n    -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\nargs=(\"$@\")\n# check arguments in reverse, so last option wins.\nfor ((idx = $# - 1; idx >= 0; --idx)); do\n  [[ \"${args[idx]}\" = -h || \"${args[idx]}\" = --help ]] && usage\n  [[ \"${args[idx]}\" = -V || \"${args[idx]}\" = --version ]] && progVersion\ndone\nunset args idx\n\n################################################################################\n# biz logic\n################################################################################\n\n# On MacOS, netstat need to using -p tcp to get only tcp output.\nUNAME=$(uname)\n[[ $UNAME = Darwin* ]] && option_for_mac=-ptcp\n\n# shellcheck disable=SC2086\nnetstat -tna ${option_for_mac:-} | awk 'NR > 2 {\n    ++s[$NF]\n}\n\nEND {\n    # get max length of stat and count\n    for(v in s) {\n        stat_len = length(v)\n        if(stat_len > max_stat_len) max_stat_len = stat_len\n\n        count_len = length(s[v])\n        if (count_len > max_count_len) max_count_len = count_len\n    }\n\n    for(v in s) {\n        printf \"%-\" max_stat_len \"s %\" max_count_len \"s\\n\", v, s[v]\n    }\n}' | sort -nr -k2,2\n"
  },
  {
    "path": "bin/uq",
    "content": "#!/usr/bin/env bash\n# @Function\n# Filter lines from INPUT (or standard input), writing to OUTPUT (or standard output).\n# same as `uniq` command in core utils,\n# but detect repeated lines that are not adjacent, no sorting required.\n#\n# @Usage\n#   uq [OPTION]... [INPUT [OUTPUT]]\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-uq\n# @author Zava Xu (zava.kid at gmail dot com)\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\n# NOTE: $'foo' is the escape sequence syntax of bash\nreadonly NL=$'\\n' # new line\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\nyellowPrint() {\n  if [ -t 1 ]; then\n    printf '\\e[1;33m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nconvertHumanReadableSizeToSize() {\n  local human_readable_size=$1\n\n  [[ \"$human_readable_size\" =~ ^([0-9][0-9]*)([kmg]?)$ ]] || return 1\n\n  local size=${BASH_REMATCH[1]} unit=${BASH_REMATCH[2]}\n  case \"$unit\" in\n  k)\n    ((size *= 1024))\n    ;;\n  m)\n    ((size *= 1024 ** 2))\n    ;;\n  g)\n    ((size *= 1024 ** 3))\n    ;;\n  esac\n\n  echo \"$size\"\n}\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION]... [INPUT [OUTPUT]]\nFilter lines from INPUT (or standard input), writing to OUTPUT (or standard output).\nSame as \\`uniq\\` command in core utils,\nbut detect repeated lines that are not adjacent, no sorting required.\n\nExample:\n  # only one file, output to stdout\n  uq in.txt\n  # more than 1 file, last file argument is output file\n  uq in.txt out.txt\n  # when use - as output file, output to stdout\n  uq in1.txt in2.txt -\n\nOptions:\n  -c, --count           prefix lines by the number of occurrences\n  -d, --repeated        only print duplicate lines, one for each group\n  -D                    print all duplicate lines\n                        combined with -c/-d option usually\n  --all-repeated[=METHOD]  like -D, but allow separating groups\n                           with an empty line;\n                           METHOD={none(default),prepend,separate}\n  -u, --unique          Only output unique lines\n                          that are not repeated in the input\n  -i, --ignore-case     ignore differences in case when comparing\n  -z, --zero-terminated line delimiter is NUL, not newline\n  -XM, --max-input      max input size(count by char), support k,m,g postfix\n                          default is 256m\n                          avoid consuming large memory unexpectedly\n  -h, --help            display this help and exit\n  -V, --version         display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\nuq_opt_count=0\nuq_opt_only_repeated=0\nuq_opt_all_repeated=0\nuq_opt_repeated_method=none\nuq_opt_only_unique=0\nuq_opt_ignore_case=0\nuq_opt_zero_terminated=0\nuq_max_input_human_readable_size=256m\nargv=()\n\nwhile (($# > 0)); do\n  case \"$1\" in\n  -c | --count)\n    uq_opt_count=1\n    shift\n    ;;\n  -d | --repeated)\n    uq_opt_only_repeated=1\n    shift\n    ;;\n  -D)\n    uq_opt_all_repeated=1\n    shift\n    ;;\n  --all-repeated=*)\n    uq_opt_all_repeated=1\n\n    uq_opt_repeated_method=${1#--all-repeated=}\n    [[ $uq_opt_repeated_method = 'none' || $uq_opt_repeated_method = 'prepend' || $uq_opt_repeated_method = 'separate' ]] ||\n      die -h \"invalid argument ‘$uq_opt_repeated_method’ for ‘--all-repeated’${NL}Valid arguments are:$NL  - ‘none’$NL  - ‘prepend’$NL  - ‘separate’\"\n\n    shift\n    ;;\n  -u | --unique)\n    uq_opt_only_unique=1\n    shift\n    ;;\n  -i | --ignore-case)\n    uq_opt_ignore_case=1\n    shift\n    ;;\n  -z | --zero-terminated)\n    uq_opt_zero_terminated=1\n    shift\n    ;;\n  -XM | --max-input)\n    uq_max_input_human_readable_size=$2\n    shift 2\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    argv=(${argv[@]:+\"${argv[@]}\"} \"$@\")\n    break\n    ;;\n  -)\n    argv=(${argv[@]:+\"${argv[@]}\"} \"$1\")\n    shift\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    argv=(${argv[@]:+\"${argv[@]}\"} \"$1\")\n    shift\n    ;;\n  esac\ndone\n\n[[ $uq_opt_only_repeated = 1 && $uq_opt_only_unique = 1 ]] &&\n  die -h \"printing duplicated lines(-d, --repeated) and unique lines(-u, --unique) is meaningless\"\n[[ $uq_opt_all_repeated = 1 && $uq_opt_only_unique = 1 ]] &&\n  die -h \"printing all duplicate lines(-D, --all-repeated) and unique lines(-u, --unique) is meaningless\"\n\n[[ $uq_opt_all_repeated = 1 && $uq_opt_repeated_method = none && ($uq_opt_count = 0 && $uq_opt_only_repeated = 0) ]] &&\n  yellowPrint \"WARN: -D/--all-repeated=none option without -c/-d option, just cat input simply!\" >&2\n\n# DO NOT declare and assign var uq_max_input_size(as readonly) in ONE line!\n#   more info see https://github.com/koalaman/shellcheck/wiki/SC2155\nuq_max_input_size=$(convertHumanReadableSizeToSize \"$uq_max_input_human_readable_size\") ||\n  die -h \"illegal value of option -XM/--max-input: $uq_max_input_human_readable_size\"\n\nreadonly argc=${#argv[@]} argv uq_max_input_size\n\nif ((argc == 0)); then\n  input_files=()\n  output_file=/dev/stdout\nelif ((argc == 1)); then\n  input_files=(\"${argv[0]}\")\n  output_file=/dev/stdout\nelse\n  input_files=(\"${argv[@]:0:argc-1}\")\n  output_file=${argv[argc - 1]}\n  if [ \"$output_file\" = - ]; then\n    output_file=/dev/stdout\n  fi\nfi\nreadonly output_file\n\n# Check input file\nfor f in ${input_files[@]:+\"${input_files[@]}\"}; do\n  # - is stdin, ok\n  [ \"$f\" = - ] && continue\n\n  [ -e \"$f\" ] || die \"input file $f: No such file or directory!\"\n  [ ! -d \"$f\" ] || die \"input file $f exists, but is a directory!\"\n  [ -f \"$f\" ] || die \"input file $f exists, but is not a file!\"\n  [ -r \"$f\" ] || die \"input file $f exists, but is not readable!\"\ndone\nunset f\n\n################################################################################\n# biz logic\n################################################################################\n\n# uq awk script\n#\n# edit in a separated file(eg: uq.awk) then copy here,\n# maybe more convenient(like good syntax highlight)\n\n# shellcheck disable=SC2016\nreadonly uq_awk_script='\n\nfunction printResult(for_lines) {\n    for (idx = 0; idx < length(for_lines); idx++) {\n        line = for_lines[idx]\n        count = line_count_array[caseAwareLine(line)]\n        #printf \"DEBUG: %s %s, index: %s, uq_opt_only_repeated: %s\\n\", count, line, idx, uq_opt_only_repeated\n\n        if (uq_opt_only_unique) {\n            if (count == 1) printLine(count, line)\n        } else {\n            if (uq_opt_only_repeated && count <= 1) continue\n\n            if (uq_opt_repeated_method == \"prepend\" || uq_opt_repeated_method == \"separate\" && previous_output) {\n                if (line != previous_output) print \"\"\n            }\n\n            printLine(count, line)\n            previous_output = line\n        }\n    }\n}\n\nfunction printLine(count, line) {\n    if (uq_opt_count) printf \"%7s %s%s\", count, line, ORS\n    else print line\n}\n\nfunction caseAwareLine(line) {\n    if (IGNORECASE) return tolower(line)\n    else return line\n}\n\nBEGIN {\n    if (uq_opt_zero_terminated) ORS = RS = \"\\0\"\n}\n\n{\n    total_input_size += length + 1\n    if (total_input_size > uq_max_input_size) {\n        printf \"%s: input size exceed max input size %s!\\nuse option -XM/--max-input specify a REASONABLE larger value.\\n\",\n            uq_PROG, uq_max_input_human_readable_size > \"/dev/stderr\"\n        exit(1)\n    }\n\n    # use index to keep lines order\n    original_lines[line_index++] = $0\n\n    case_aware_line = caseAwareLine($0)\n    # line_count_array: line content -> count\n    if (++line_count_array[case_aware_line] == 1) {\n        # use index to keep lines order\n        deduplicated_lines[deduplicated_line_index++] = case_aware_line\n    }\n}\n\nEND {\n    if (uq_opt_all_repeated) printResult(original_lines)\n    else printResult(deduplicated_lines)\n}\n\n'\n\nawk \\\n  -v \"uq_opt_count=$uq_opt_count\" \\\n  -v \"uq_opt_only_repeated=$uq_opt_only_repeated\" \\\n  -v \"uq_opt_all_repeated=$uq_opt_all_repeated\" \\\n  -v \"uq_opt_repeated_method=$uq_opt_repeated_method\" \\\n  -v \"uq_opt_only_unique=$uq_opt_only_unique\" \\\n  -v \"IGNORECASE=$uq_opt_ignore_case\" \\\n  -v \"uq_opt_zero_terminated=$uq_opt_zero_terminated\" \\\n  -v \"uq_max_input_human_readable_size=$uq_max_input_human_readable_size\" \\\n  -v \"uq_max_input_size=$uq_max_input_size\" \\\n  -v \"uq_PROG=$PROG\" \\\n  -f <(printf \"%s\" \"$uq_awk_script\") \\\n  -- ${input_files[@]:+\"${input_files[@]}\"} \\\n  >\"$output_file\"\n"
  },
  {
    "path": "bin/xpf",
    "content": "#!/usr/bin/env bash\n# @Function\n# Open file in file explorer, file is selected.\n# same as xpl --selected [file]...\n#\n# @Usage\n#   $ ./xpf file\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-xpl-and-xpf\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\n################################################################################\n# util functions\n################################################################################\n\n# `realpath` command exists on Linux and macOS, return resolved physical path\n#   - realpath command on macOS do NOT support option `-e`;\n#     combined `[ -e $file ]` to check file existence first.\n#   - How can I get the behavior of GNU's readlink -f on a Mac?\n#     https://stackoverflow.com/questions/1055671\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\n################################################################################\n# biz logic\n################################################################################\n\n# DO NOT inline THIS_SCRIPT into BASE_DIR, because sub-shell:\n#   BASE_DIR=$(dirname -- \"$(realpath \"${BASH_SOURCE[0]}\")\")\nTHIS_SCRIPT=$(realpath \"${BASH_SOURCE[0]}\")\nBASE_DIR=$(dirname -- \"$THIS_SCRIPT\")\n\n# shellcheck disable=SC1091\nsource \"$BASE_DIR/xpl\" \"$@\"\n"
  },
  {
    "path": "bin/xpl",
    "content": "#!/usr/bin/env bash\n# @Function\n# Open file in file explorer.\n#\n# @Usage\n#   $ ./xpf [file [file ...] ]\n#\n# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-xpl-and-xpf\n# @author Jerry Lee (oldratlee at gmail dot com)\nset -eEuo pipefail\n\nreadonly PROG=${0##*/}\nreadonly PROG_VERSION='3.x-dev'\n\n################################################################################\n# util functions\n################################################################################\n\nredPrint() {\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;31m%s\\e[0m\\n' \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\ndie() {\n  local prompt_help=false exit_status=2\n  while (($# > 0)); do\n    case \"$1\" in\n    -h)\n      prompt_help=true\n      shift\n      ;;\n    -s)\n      exit_status=$2\n      shift 2\n      ;;\n    *)\n      break\n      ;;\n    esac\n  done\n\n  (($# > 0)) && redPrint \"$PROG: $*\"\n  $prompt_help && echo \"Try '$PROG --help' for more information.\"\n\n  exit \"$exit_status\"\n} >&2\n\nusage() {\n  cat <<EOF\nUsage: $PROG [OPTION] [FILE]...\nOpen file in file explorer.\nExample: $PROG file.txt\n\nOptions:\n    -s, --selected  select the file or dir\n    -h, --help      display this help and exit\n    -V, --version   display version information and exit\nEOF\n\n  exit\n}\n\nprogVersion() {\n  printf '%s\\n' \"$PROG $PROG_VERSION\"\n  exit\n}\n\n################################################################################\n# parse options\n################################################################################\n\nfiles=()\nselected=false\nwhile (($# > 0)); do\n  case \"$1\" in\n  -s | --selected)\n    selected=true\n    shift\n    ;;\n  -h | --help)\n    usage\n    ;;\n  -V | --version)\n    progVersion\n    ;;\n  --)\n    shift\n    files=(${files[@]:+\"${files[@]}\"} \"$@\")\n    break\n    ;;\n  -*)\n    die -h \"unrecognized option '$1'\"\n    ;;\n  *)\n    files=(${files[@]:+\"${files[@]}\"} \"$1\")\n    shift\n    ;;\n  esac\ndone\n\n# if files is empty, use one element \".\"\nfiles=(\"${files[@]:-.}\")\n\n# if program name is xpf, set option selected!\n[ \"xpf\" = \"$PROG\" ] && selected=true\n\nreadonly files selected\n\n################################################################################\n# biz logic\n################################################################################\n\n# open one file\nopenOneFile() {\n  local file=$1 slt=$selected\n\n  case \"$(uname)\" in\n  Darwin*)\n    [ -f \"$file\" ] && slt=true\n    if $slt; then\n      open -R \"$file\"\n    else\n      open \"$file\"\n    fi\n    ;;\n  CYGWIN*)\n    [ -f \"$file\" ] && slt=true\n    if $slt; then\n      explorer /select, \"$(cygpath -w \"$file\")\"\n    else\n      explorer \"$(cygpath -w \"$file\")\"\n    fi\n    ;;\n  *)\n    if [ -d \"$file\" ]; then\n      nautilus \"$(dirname -- \"$file\")\"\n    else\n      if $slt; then\n        nautilus \"$file\"\n      else\n        nautilus \"$(dirname -- \"$file\")\"\n      fi\n    fi\n    ;;\n  esac\n\n  local selected_msg\n  $slt && selected_msg='with selection'\n  printf 'open %14s: %s\\n' \"$selected_msg\" \"$file\"\n}\n\nhas_error=false\n\nfor file in \"${files[@]}\"; do\n  [ -e \"$file\" ] || {\n    has_error=true\n    redPrint \"$PROG: $file: No such file or directory!\" >&2\n    continue\n  }\n\n  openOneFile \"$file\" || has_error=true\ndone\n\n# set exit status\n! $has_error\n"
  },
  {
    "path": "docs/developer-guide.md",
    "content": "# 📚 `Shell`学习与开发的资料\n\n- 🛠️ 开发规范与工具\n    - [`Google Shell Style Guide`](https://google.github.io/styleguide/shell.xml) | [中文版](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/contents.html)\n    - [`koalaman/shellcheck`](https://github.com/koalaman/shellcheck): `ShellCheck`, a static analysis tool for shell scripts\n    - [`mvdan/sh(shfmt)`](https://github.com/mvdan/sh): `shfmt` formats shell programs\n- 👷 **`Bash/Shell`最佳实践与安全编程**文章\n    - [Use the Unofficial Bash Strict Mode (Unless You Looove Debugging)](http://redsymbol.net/articles/unofficial-bash-strict-mode/)\n    - Bash Pitfalls: 编程易犯的错误 - 团子的小窝：[Part 1](http://kodango.com/bash-pitfalls-part-1) | [Part 2](http://kodango.com/bash-pitfalls-part-2) | [Part 3](http://kodango.com/bash-pitfalls-part-3) | [Part 4](http://kodango.com/bash-pitfalls-part-4) | [英文原文：Bash Pitfalls](http://mywiki.wooledge.org/BashPitfalls)\n    - [编写可靠shell脚本的八个建议 - xshell.net](https://www.xshell.net/shell/1577.html)\n    - [Shell 编码风格 - 团子的小窝](http://kodango.com/shell-script-style)\n    - [Bash 优良编程实践](https://www.techug.com/post/bash-practice.html)\n    - [不要自己去指定`sh`的方式去执行脚本](https://github.com/oldratlee/useful-scripts/issues/57#issuecomment-326485965)\n- 🎶 **Tips**\n    - [让你提升命令行效率的 Bash 快捷键 【完整版】](https://linuxtoy.org/archives/bash-shortcuts.html)  \n      补充：`ctrl + x, ctrl + e` 就地打开文本编辑器来编辑当前命令行，对于复杂命令行特别有用\n    - [应该知道的Linux技巧 | 酷 壳 - CoolShell](https://coolshell.cn/articles/8883.html)\n    - 简洁的 Bash Programming 技巧 - 团子的小窝：[Part 1](http://kodango.com/simple-bash-programming-skills) | [Part 2](http://kodango.com/simple-bash-programming-skills-2) | [Part 3](http://kodango.com/simple-bash-programming-skills-3)\n    - [Bash 测试和比较函数 — `test`、`[`、`[[`、`((`、和 `if-then-else` 解密](https://www.ibm.com/developerworks/cn/linux/l-bash-test.html)\n    - [Filenames and Pathnames in Shell (bash, dash, ash, ksh, and so on): How to do it Correctly](https://dwheeler.com/essays/filenames-in-shell.html)\n    - [理解 IFS - 团子的小窝](http://kodango.com/understand-ifs)\n    - [shell中的IFS详解 – 笑遍世界](http://smilejay.com/2011/12/bash_ifs/)\n    - [Bash脚本：怎样一行行地读文件（最好和最坏的方法）](http://blog.jobbole.com/72185/)\n    - [Shell 脚本避免多次重复 source - 团子的小窝](http://kodango.com/avoid-repeated-source-in-shell)\n    - [一个奇怪的 echo 结果 - 团子的小窝](http://kodango.com/a-strange-echo-result)\n    - [浅谈 Shell 脚本配置文件格式 - 团子的小窝](http://kodango.com/config-file-format-in-shell)\n    - [Bash function 还能这么玩 - 团子的小窝](http://kodango.com/bash-functions)\n    - [Bash 获取当前函数名 - 团子的小窝](http://kodango.com/get-function-name-in-bash)\n    - [Zsh和Bash，究竟有何不同 坑很深](https://www.xshell.net/shell/bash_zsh.html)\n- 💎 **系统学习** — 看文章、了解Tips完全不能替代系统学习才能真正理解并专业开发！\n    - [《Bash Pocket Reference》](https://book.douban.com/subject/26738258/)  \n      力荐！说明简单直接结构体系的佳作，专业`Bash`编程必备！且16年的第二版更新到了新版的`Bash 4`\n    - [《学习bash》](https://book.douban.com/subject/1241361/) 上面那本的展开版\n    - 官方资料\n        - [`bash man`](https://manned.org/bash) | [中文版](http://ahei.info/chinese-bash-man.htm)\n        - [Bash Reference Manual - gnu.org](http://www.gnu.org/software/bash/manual/) | [中文版](https://yiyibooks.cn/Phiix/bash_reference_manual/bash%E5%8F%82%E8%80%83%E6%96%87%E6%A1%A3.html)  \n          Bash参考手册，讲得全面且有深度，比如会全面地讲解不同转义的区别、命令的解析过程，这有助统一深入的方式认识Bash整个执行方式和过程。这些内容在其它书中往往不会讲（因为复杂难于深入浅出的讲解），但却一通百通的关键。\n    - [Advanced Bash-Scripting Guide](https://hangar118.sdf.org/p/bash-scripting-guide/index.html): An in-depth exploration of the art of shell scripting.\n    - [命令行的艺术 - `jlevy/the-art-of-command-line`](https://github.com/jlevy/the-art-of-command-line/blob/master/README-zh.md)\n    - [`awesome-lists/awesome-bash`](https://github.com/awesome-lists/awesome-bash): A curated list of delightful Bash scripts and resources.\n    - [`alebcay/awesome-shell`](https://github.com/alebcay/awesome-shell): A curated list of awesome command-line frameworks, toolkits, guides and gizmos.\n    - [wzb56/13_questions_of_shell: shell十三问 - shell教程](https://github.com/wzb56/13_questions_of_shell)\n    - [实用 Shell 文档 - 团子的小窝](http://kodango.com/useful-documents-about-shell)\n    - 更多书籍参见个人整理的[书籍豆列 **_`Bash/Shell`_**](https://www.douban.com/doulist/1779379/)\n"
  },
  {
    "path": "docs/install.md",
    "content": "🐌 下载使用\n====================================\n\n下载整个工程的脚本\n-------------------\n\n### 直接clone工程\n\n使用简单、方便更新，不过要安装有`git`。\n\n```bash\ngit clone git://github.com/oldratlee/useful-scripts.git\n\ncd useful-scripts\n\n# 使用Release分支的内容\ngit checkout release-3.x\n\n# 更新脚本\ngit pull\n```\n\n包含2个分支：\n\n- `dev-3.x`：开发分支\n- `release-3.x`：发布分支，功能稳定的脚本\n\nPS：  \n我的做法是把`useful-scripts` checkout到`$HOME/bin`目录下，再把`$HOME/bin/useful-scripts/bin`配置到`PATH`变量上，这样方便我本地使用所有的脚本。\n\n### 打包下载\n\n下载文件[release-3.x.zip](https://github.com/oldratlee/useful-scripts/archive/release-3.x.zip)：\n\n```bash\nwget --no-check-certificate https://github.com/oldratlee/useful-scripts/archive/release-3.x.zip\n\nunzip release-3.x.zip\n```\n\n下载和运行单个文件\n-------------------\n\n以[`show-busy-java-threads`](https://raw.github.com/oldratlee/useful-scripts/release-3.x/bin/show-busy-java-threads)为例。\n\n### `curl`文件直接用`bash`运行\n\n```bash\ncurl -sLk 'https://raw.github.com/oldratlee/useful-scripts/release-3.x/bin/show-busy-java-threads' | bash\n```\n\n### 下载单个文件\n\n```bash\nwget --no-check-certificate https://raw.github.com/oldratlee/useful-scripts/release-3.x/bin/show-busy-java-threads\nchmod +x show-busy-java-threads\n\n./show-busy-java-threads\n```\n"
  },
  {
    "path": "docs/java.md",
    "content": "🐌 `Java`相关脚本\n====================================\n\n<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->\n\n- [🍺 show-busy-java-threads](#-show-busy-java-threads)\n    - [用法](#%E7%94%A8%E6%B3%95)\n    - [示例](#%E7%A4%BA%E4%BE%8B)\n    - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85)\n- [🍺 show-duplicate-java-classes](#-show-duplicate-java-classes)\n    - [用法](#%E7%94%A8%E6%B3%95-1)\n        - [`JDK`开发场景使用说明](#jdk%E5%BC%80%E5%8F%91%E5%9C%BA%E6%99%AF%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E)\n            - [对于一般的工程](#%E5%AF%B9%E4%BA%8E%E4%B8%80%E8%88%AC%E7%9A%84%E5%B7%A5%E7%A8%8B)\n            - [对于`Web`工程](#%E5%AF%B9%E4%BA%8Eweb%E5%B7%A5%E7%A8%8B)\n        - [`Android`开发场景使用说明](#android%E5%BC%80%E5%8F%91%E5%9C%BA%E6%99%AF%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E)\n    - [示例](#%E7%A4%BA%E4%BE%8B-1)\n    - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85-1)\n- [🍺 find-in-jars](#-find-in-jars)\n    - [用法](#%E7%94%A8%E6%B3%95-2)\n    - [示例](#%E7%A4%BA%E4%BE%8B-2)\n    - [运行效果](#%E8%BF%90%E8%A1%8C%E6%95%88%E6%9E%9C)\n    - [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99)\n\n<!-- END doctoc generated TOC please keep comment here to allow auto update -->\n\n-------------------------------\n\n关于`Java`排错与诊断，力荐️`Arthas`： ❤️\n\n- `Arthas`用户文档： https://arthas.aliyun.com/doc/quick-start.html\n- GitHub Repo： [alibaba/arthas: Alibaba Java诊断利器](https://github.com/alibaba/arthas)\n\n`Arthas`功能异常(😜)强劲，且在阿里巴巴线上支持使用多年。我自己也常用，一定要看看用用！\n\n`Arthas`是通过`Agent`方式来连接运行的`Java`进程、主要通过交互式来完成功能，与之对应的脚本方式也有其优势，如：\n\n1. 可以在进程不能启动的情况下完成诊断（如依赖中的重复类分析、`ClassPath`上的资源或类查找）\n1. 开销少；简单少依赖（就纯文本的一个脚本文件）\n1. 方便与（已有的）工具（如`awk`、`sed`、`cron`）、流程或设施集成，进一步编程/自动化\n\n请按需按场景选用。\n\n-------------------------------\n\n<a id=\"beer-show-busy-java-threadssh\"></a>\n<a id=\"beer-show-busy-java-threads\"></a>\n\n🍺 [show-busy-java-threads](../bin/show-busy-java-threads)\n----------------------\n\n用于快速排查`Java`的`CPU`性能问题(`top us`值过高)，自动查出运行的`Java`进程中消耗`CPU`多的线程，并打印出其线程栈，从而确定导致性能问题的方法调用。  \n目前只支持`Linux`。原因是`Mac`、`Windows`的`ps`命令不支持列出进程的线程`id`，更多信息参见 [#33](https://github.com/oldratlee/useful-scripts/issues/33)，欢迎提供解法。\n\nPS，如何操作可以参见[`@bluedavy`](http://weibo.com/bluedavy)的[《分布式Java应用》](https://book.douban.com/subject/4848587/)的【5.1.1 `CPU`消耗分析】一节，说得很详细：\n\n1. `top`命令找出消耗`CPU`高的`Java`进程及其线程`id`：\n    1. 开启线程显示模式（`top -H`，或是打开`top`后按`H`）\n    1. 按`CPU`使用率排序（`top`缺省是按`CPU`使用降序，已经合要求；打开`top`后按`P`可以显式指定按`CPU`使用降序）\n    1. 记下`Java`进程`id`及其`CPU`高的线程`id`\n1. 查看消耗`CPU`高的线程栈：\n    1. 用进程`id`作为参数，`jstack`出有问题的`Java`进程\n    1. 手动转换线程`id`成十六进制（可以用`printf %x 1234`）\n    1. 在`jstack`输出中查找十六进制的线程`id`（可以用`vim`的查找功能`/0x1234`，或是`grep 0x1234 -A 20`）\n1. 查看对应的线程栈，分析问题\n\n查问题时，会要多次上面的操作以分析确定问题，这个过程**太繁琐太慢了**。  \n期望整合上面的过程成一个脚本，这样一行命令就可以自动化地搞定。\n\n### 用法\n\n```bash\nshow-busy-java-threads\n# 从所有运行的Java进程中找出最消耗CPU的线程（缺省5个），打印出其线程栈\n\n# 缺省会自动从所有的Java进程中找出最消耗CPU的线程，这样用更方便\n# 当然你可以通过 -p 选项 手动指定要分析的Java进程Id，以保证只会显示你关心的那个Java进程的信息\nshow-busy-java-threads -p <指定的Java进程Id>\nshow-busy-java-threads -p 42\nshow-busy-java-threads -p 42,47\n\nshow-busy-java-threads -c <要展示示的线程栈个数>\n\nshow-busy-java-threads <重复执行的间隔秒数> [<重复执行的次数>]\n# 多次执行；这2个参数的使用方式类似vmstat命令\n\nshow-busy-java-threads -a <运行输出的记录到的文件>\n# 记录到文件以方便回溯查看\n\nshow-busy-java-threads -S <存储jstack输出文件的目录>\n# 指定jstack输出文件的存储目录，方便记录以后续分析\n\n##############################\n# 注意：\n##############################\n# 如果Java进程的用户 与 执行脚本的当前用户 不同，则jstack不了这个Java进程\n# 为了能切换到Java进程的用户，需要加sudo来执行，即可以解决：\nsudo show-busy-java-threads\n\nshow-busy-java-threads -s <指定jstack命令的全路径>\n# 对于sudo方式的运行，JAVA_HOME环境变量不能传递给root，\n# 而root用户往往没有配置JAVA_HOME且不方便配置，不能找到jstack命令。\n# 这时显式指定jstack命令的路径就反而显得更方便了\n\n# -m 选项：执行jstack命令时加上 -m 选项，显示上Native的栈帧，一般应用排查不需要使用\nshow-busy-java-threads -m\n# -F 选项：执行jstack命令时加上 -F 选项（如果直接jstack无响应时，用于强制jstack），一般情况不需要使用\nshow-busy-java-threads -F\n# -l 选项：执行jstack命令时加上 -l 选项，显示上更多相关锁的信息，一般情况不需要使用\n# 注意：和 -m -F 选项一起使用时，可能会大大增加jstack操作的耗时\nshow-busy-java-threads -l\n\n# 帮助信息\n$ show-busy-java-threads -h\nUsage: show-busy-java-threads [OPTION]... [delay [count]]\nFind out the highest cpu consumed threads of java processes,\nand print the stack of these threads.\n\nExample:\n  show-busy-java-threads       # show busy java threads info\n  show-busy-java-threads 1     # update every 1 second, (stop by eg: CTRL+C)\n  show-busy-java-threads 3 10  # update every 3 seconds, update 10 times\n\nOutput control:\n  -p, --pid <java pid(s)>   find out the highest cpu consumed threads from\n                            the specified java process.\n                            support pid list(eg: 42,47).\n                            default from all java process.\n  -c, --count <num>         set the thread count to show, default is 5.\n                            set count 0 to show all threads.\n  -a, --append-file <file>  specifies the file to append output as log.\n  -S, --store-dir <dir>     specifies the directory for storing\n                            the intermediate files, and keep files.\n                            default store intermediate files at tmp dir,\n                            and auto remove after run. use this option to keep\n                            files so as to review jstack/top/ps output later.\n  delay                     the delay between updates in seconds.\n  count                     the number of updates.\n                            delay/count arguments imitates the style of\n                            vmstat command.\n\njstack control:\n  -s, --jstack-path <path>  specifies the path of jstack command.\n  -F, --force               set jstack to force a thread dump.\n                            use when jstack does not respond (process is hung).\n  -m, --mix-native-frames   set jstack to print both java and\n                            native frames (mixed mode).\n  -l, --lock-info           set jstack with long listing.\n                            prints additional information about locks.\n\nCPU usage calculation control:\n  -i, --cpu-sample-interval specifies the delay between cpu samples to get\n                            thread cpu usage percentage during this interval.\n                            default is 0.5 (second).\n                            set interval 0 to get the percentage of time spent\n                            running during the *entire lifetime* of a process.\n\nMiscellaneous:\n  -h, --help                display this help and exit.\n  -V, --version             display version information and exit.\n```\n\n### 示例\n\n```bash\n$ show-busy-java-threads\n[1] Busy(57.0%) thread(23355/0x5b3b) stack of java process(23269) under user(admin):\n\"pool-1-thread-1\" prio=10 tid=0x000000005b5c5000 nid=0x5b3b runnable [0x000000004062c000]\n   java.lang.Thread.State: RUNNABLE\n    at java.text.DateFormat.format(DateFormat.java:316)\n    at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)\n    at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:127)\n    at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)\n    at java.lang.Thread.run(Thread.java:662)\n\n[2] Busy(26.1%) thread(24018/0x5dd2) stack of java process(23269) under user(admin):\n\"pool-1-thread-2\" prio=10 tid=0x000000005a968800 nid=0x5dd2 runnable [0x00000000420e9000]\n   java.lang.Thread.State: RUNNABLE\n    at java.util.Arrays.copyOf(Arrays.java:2882)\n    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)\n    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572)\n    at java.lang.StringBuffer.append(StringBuffer.java:320)\n    - locked <0x00000007908d0030> (a java.lang.StringBuffer)\n    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:890)\n    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869)\n    at java.text.DateFormat.format(DateFormat.java:316)\n    at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)\n    at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:126)\n    at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)\n    at java.lang.Thread.run(Thread.java:662)\n\n......\n```\n\n上面的线程栈可以看出，`CPU`消耗最高的2个线程都在执行`java.text.DateFormat.format`，业务代码对应的方法是`shared.monitor.schedule.AppMonitorDataAvgScheduler.run`。可以基本确定：\n\n- `AppMonitorDataAvgScheduler.run`调用`DateFormat.format`次数比较频繁。\n- `DateFormat.format`比较慢。（这个可以由`DateFormat.format`的实现确定。）\n\n多执行几次`show-busy-java-threads`，如果上面情况高概率出现，则可以确定上面的判定。  \n因为调用越少代码执行越快，则出现在线程栈的概率就越低。  \n脚本有自动多次执行的功能，指定 重复执行的间隔秒数/重复执行的次数 参数。\n\n分析`shared.monitor.schedule.AppMonitorDataAvgScheduler.run`实现逻辑和调用方式，以优化实现解决问题。\n\n### 贡献者\n\n- [silentforce](https://github.com/silentforce) 改进此脚本，增加对环境变量`JAVA_HOME`的判断。 [#15](https://github.com/oldratlee/useful-scripts/pull/15)\n- [liuyangc3](https://github.com/liuyangc3)\n    - 发现并解决`jstack`非当前用户`Java`进程的问题。 [#50](https://github.com/oldratlee/useful-scripts/pull/50)\n    - 优化性能，通过`read -a`简化反复的`awk`操作。 [#51](https://github.com/oldratlee/useful-scripts/pull/51)\n- [superhj1987](https://github.com/superhj1987) / [lirenzuo](https://github.com/lirenzuo)\n    - 提出/实现了多次执行的功能 [superhj1987/awesome-scripts#1](https://github.com/superhj1987/awesome-scripts/issues/1)\n- [xiongchen2012](https://github.com/xiongchen2012) 提出并解决了长用户名截断的Bug [#62](https://github.com/oldratlee/useful-scripts/pull/62)\n- [qsLI](https://github.com/qsLI) / [sdslnmd](https://github.com/sdslnmd)\n    - 发现并提交Issue：show-busy-java-threads支持top来获取cpu占用率，ps的cpu占用率非实时 [#67](https://github.com/oldratlee/useful-scripts/issues/67)\n- [geekMessi](https://github.com/geekMessi)\n    - 发现并提交Issue：在`top v3.2`下提取不正确的Bug [#71](https://github.com/oldratlee/useful-scripts/issues/71)\n    - 发现并提交Issue：support command name jsvc to find java process [#72](https://github.com/oldratlee/useful-scripts/issues/72)\n\n🍺 [show-duplicate-java-classes](../bin/show-duplicate-java-classes)\n----------------------\n\n找出`Java Lib`（`Java`库，即`Jar`文件）或`Class`目录（类目录）中的重复类。  \n全系统支持（`Python 3`实现，安装`Python 3`即可），如`Linux`、`Mac`、`Windows`。\n\n`Java`开发的一个麻烦的问题是`Jar`冲突（即多个版本的`Jar`），或者说重复类。会出`NoSuchMethod`等的问题，还不见得当时出问题。找出有重复类的`Jar`，可以防患未然。\n\n### 用法\n\n- 通过脚本参数 指定 `Libs`目录，查找目录下`Jar`文件，收集`Jar`文件中`Class`文件以分析重复类。可以指定多个`Libs`目录。\n    - 缺省只会查找指定`Lib`目录下`Jar`文件，不会收集`Lib`目录的子目录下`Jar`文件。\n        - 因为`Libs`目录一般不会用子目录再放`Jar`，也避免把去查找不期望的`Jar`文件。\n        - 可以通过 `-L`选项 设置 收集`Lib`子目录下的`Jar`文件；这样可以简化`Lib`目录的设置，不需要指定完整的`Lib`目录路径。\n    - 对于找到的`Jar`文件，缺省不会进一步收集包含在`Jar`文件中的`Jar`。\n        - 即`FatJar`/`UberJar`的场景，随着像`SpringBoot`的广泛使用，`FatJar`/`UberJar`也比较常见。\n        - 可以通过 `-J`选项 设置 收集包含在`Jar`文件中的`Jar`。\n- 通过`-c`选项 指定 `Class`目录，直接收集这个目录下的`Class`文件以分析重复类。可以多次指定多个`Class`目录。\n\n```bash\n# 查找当前目录下所有Jar中的重复类\nshow-duplicate-java-classes\n\n# 查找多个指定目录下所有Jar中的重复类\nshow-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2\n# 通过 -L 选项，收集子目录中的Jar文件\nshow-duplicate-java-classes -L path/to/lib_dir1\n# 通过 -J 选项，收集包含在Jar文件中的Jar文件（即 收集包含在FatJar/UberJar中的Jar）\nshow-duplicate-java-classes -J path/to/lib_dir1\n\n# 查找多个指定Class目录下的重复类。 Class目录 通过 -c 选项指定\nshow-duplicate-java-classes -c path/to/class_dir1 -c /path/to/class_dir2\n\n# 查找指定Class目录和指定目录下所有Jar中的重复类的Jar\nshow-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2 -c path/to/class_dir1 -c path/to/class_dir2\n\n# 帮助信息\n$ show-duplicate-java-classes -h\nUsage: show-duplicate-java-classes [OPTION]... [-c class-dir1 [-c class-dir2] ...] [lib-dir1|jar-file1 [lib-dir2|jar-file2] ...]\nFind duplicate classes among java lib dirs and class dirs.\n\nExamples:\n  show-duplicate-java-classes  # search jars from current dir\n  show-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2\n  show-duplicate-java-classes -c path/to/class_dir1 -c /path/to/class_dir2\n  show-duplicate-java-classes -c path/to/class_dir1 path/to/lib_dir1\n  show-duplicate-java-classes -L path/to/lib_dir1\n  show-duplicate-java-classes -J path/to/lib_dir1\n\nOptions:\n  --version             show program's version number and exit\n  -h, --help            show this help message and exit\n  -L, --recursive-lib   search jars in the sub-directories of lib dir\n  -J, --recursive-jar   search jars in the jar file\n  -c CLASS_DIRS, --class-dir=CLASS_DIRS\n                        add class dir\n  -R, --no-find-progress\n                        do not display responsive find progress\n```\n\n#### `JDK`开发场景使用说明\n\n以`Maven`作为构建工程示意过程。\n\n##### 对于一般的工程\n\n```sh\n# 在项目模块目录下执行，拷贝依赖Jar到目录target/dependency下\n$ mvn dependency:copy-dependencies -DincludeScope=runtime\n...\n# 检查重复类\n$ show-duplicate-java-classes target/dependency\n...\n```\n\n##### 对于`Web`工程\n\n对于`Web`工程，即`war` `maven`模块，会打包生成`war`文件。\n\n```sh\n# 在war模块目录下执行，生成war文件\n$ mvn install\n...\n# 解压war文件，war文件中包含了应用的依赖的Jar文件\n$ unzip target/*.war -d target/war\n...\n# 检查重复类\n$ show-duplicate-java-classes -c target/war/WEB-INF/classes target/war/WEB-INF/lib\n...\n```\n\n#### `Android`开发场景使用说明\n\n`Android`开发，有重复类在编译打包时会报`[Dex Loader] Unable to execute dex: Multiple dex files define Lorg/foo/xxx/Yyy`。\n\n但只会给出一个重复类名，如果重复类比较多时，上面打包/报错/排查会要进行多次，而`Android`的打包比较费时，这个过程比较麻烦，希望可以一次把所有重复类都列出来，一起排查掉。\n\n以`Gradle`作为构建工程示意过程。\n\n在`App`的`build.gradle`中添加拷贝库到目录`build/dependencies`下。\n\n```groovy\ntask copyDependencies(type: Copy) {\n    def dest = new File(buildDir, \"dependencies\")\n\n    // clean dir\n    dest.deleteDir()\n    dest.mkdirs()\n\n    // fill dir with dependencies\n    from configurations.compile into dest\n}\n```\n\n```sh\n# 拷贝依赖\n$ ./gradlew app:copyDependencies\n...\n# 检查重复类\n$ show-duplicate-java-classes app/build/dependencies\n...\n```\n\n### 示例\n\n```bash\n$ show-duplicate-java-classes WEB-INF/lib\nCOOL! No duplicate classes found!\n\n================================================================================\nFind in 150 class paths:\n================================================================================\n  1: (contain   9 classes) WEB-INF/lib/aopalliance-1.0.jar\n  2: (contain  25 classes) WEB-INF/lib/asm-5.0.4.jar\n  3: (contain 313 classes) WEB-INF/lib/aviator-5.0.0.jar\n  4: (contain 687 classes) WEB-INF/lib/cassandra-0.6.1.jar\n...\n\n$ show-duplicate-java-classes -c WEB-INF/classes WEB-INF/lib\nFound 1272 duplicate classes in 345 class paths and 9 class path sets:\n[1] found 188(100%) duplicate classes in 3 class paths:\n    1: (contain 188 classes) WEB-INF/lib/jdom-2.0.2.jar\n    2: (contain 195 classes) WEB-INF/lib/jdom2-2.0.6.jar\n    3: (contain 195 classes) WEB-INF/lib/jdom2-2.0.8.jar\n[2] found 150(33.8%) duplicate classes in 2 class paths:\n    1: (contain 1385 classes) WEB-INF/lib/netty-all-4.0.35.Final.jar\n    2: (contain  444 classes) WEB-INF/lib/netty-common-4.1.31.Final.jar\n[3] found 148(55.4%) duplicate classes in 2 class paths:\n    1: (contain 1385 classes) WEB-INF/lib/netty-all-4.0.35.Final.jar\n    2: (contain  267 classes) WEB-INF/lib/netty-handler-4.1.31.Final.jar\n[4] found 103(82.4%) duplicate classes in 2 class paths:\n    1: (contain 125 classes) WEB-INF/lib/hessian-3.0.14.bugfix.jar\n    2: (contain 275 classes) WEB-INF/lib/hessian-4.0.38.jar\n...\n\n================================================================================\nDuplicate classes detail info:\n================================================================================\n[1] found 188 duplicate classes in 3 class paths WEB-INF/lib/jdom-2.0.2.jar WEB-INF/lib/jdom2-2.0.6.jar WEB-INF/lib/jdom2-2.0.8.jar :\n      1: org/jdom2/Attribute.class\n      2: org/jdom2/AttributeList$1.class\n      3: org/jdom2/AttributeList$ALIterator.class\n      4: org/jdom2/AttributeList.class\n      5: org/jdom2/AttributeType.class\n      ...\n[2] found 150 duplicate classes in 2 class paths WEB-INF/lib/netty-all-4.0.35.Final.jar WEB-INF/lib/netty-common-4.1.31.Final.jar :\n      1: io/netty/util/AbstractReferenceCounted.class\n      2: io/netty/util/Attribute.class\n      3: io/netty/util/AttributeKey.class\n      4: io/netty/util/AttributeMap.class\n      5: io/netty/util/CharsetUtil.class\n      ...\n...\n\n================================================================================\nFind in 232 class paths:\n================================================================================\n  1: (contain  42 classes) WEB-INF/classes\n  2: (contain  70 classes) WEB-INF/lib/HikariCP-2.7.8.jar\n  3: (contain  13 classes) WEB-INF/lib/accessors-smart-1.2.jar\n  4: (contain   9 classes) WEB-INF/lib/aopalliance-1.0.jar\n  5: (contain  25 classes) WEB-INF/lib/asm-5.0.4.jar\n  6: (contain 313 classes) WEB-INF/lib/aviator-5.0.0.jar\n...\n```\n\n### 贡献者\n\n[tgic](https://github.com/tg123) 提供此脚本。友情贡献者的链接 [commandlinefu.cn](http://commandlinefu.cn/) | [微博linux命令行精选](http://weibo.com/u/2674868673)\n\n<a id=\"beer-find-in-jarssh\"></a>\n<a id=\"beer-find-in-jars\"></a>\n\n🍺 [find-in-jars](../bin/find-in-jars)\n----------------------\n\n在当前目录下所有`jar`文件里，查找类或资源文件。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n### 用法\n\n```bash\n# 在当前目录下所有`jar`文件里，查找类或资源文件。\nfind-in-jars 'log4j\\.properties'\nfind-in-jars 'log4j\\.xml$'\nfind-in-jars log4j\\\\.xml$ # 和上面命令一样，Shell转义的不同写法而已\nfind-in-jars 'log4j\\.(properties|xml)$'\n\n# -d选项 指定 查找目录（覆盖缺省的当前目录）\nfind-in-jars 'log4j\\.properties$' -d /path/to/find/directory\n# 支持多个查找目录，多次指定这个选项即可\nfind-in-jars 'log4j\\.properties' -d /path/to/find/directory1 -d /path/to/find/directory2\n\n# -e选项 指定 查找`zip`文件的扩展名，缺省是`jar`\nfind-in-jars 'log4j\\.properties' -e zip\n# 支持多种查找扩展名，多次指定这个选项即可\nfind-in-jars 'log4j\\.properties' -e jar -e zip\n\n# -a选项 指定 查找结果中的Jar文件使用绝对路径\n# 分享给别人时，Jar文件路径是完整的，方便别人找到文件\nfind-in-jars 'log4j\\.properties' -a\n\n# -s选项 指定 查找结果中的Jar文件和Jar文件里的查找Entry间分隔符，缺省是『!』\n# 方便你喜欢的人眼查看，或是与工具脚本如`awk`的处理\nfind-in-jars 'log4j\\.properties' -s ' <-> '\nfind-in-jars 'log4j\\.properties' -s ' ' | awk '{print $2}'\n\n# -l选项 指定 只列出Jar文件，不显示Jar文件内匹配的文件列表\n# 列出 包含log4j.xml文件的Jar文件：\nfind-in-jars -l 'log4j\\.xml$'\n\n# 帮助信息\n$ find-in-jars -h\nUsage: find-in-jars [OPTION]... PATTERN\n\nFind files in the jar files under specified directory,\nsearch jar files recursively(include subdirectory).\nThe pattern default is *extended* regex.\n\nExample:\n  find-in-jars 'log4j\\.properties'\n  # search file log4j.properties/log4j.xml at zip root\n  find-in-jars '^log4j\\.(properties|xml)$'\n  find-in-jars 'log4j\\.properties$' -d /path/to/find/directory\n  find-in-jars '\\.properties$' -d /path/to/find/dir1 -d path/to/find/dir2\n  find-in-jars 'Service\\.class$' -e jar -e zip\n  find-in-jars 'Mon[^$/]*Service\\.class$' -s ' <-> '\n\nFind control:\n  -d, --dir              the directory that find jar files.\n                         default is current directory. this option can specify\n                         multiply times to find in multiply directories.\n  -e, --extension        set find file extension, default is jar. this option\n                         can specify multiply times to find multiply extension.\n  -E, --extended-regexp  PATTERN is an extended regular expression (*default*)\n  -F, --fixed-strings    PATTERN is a set of newline-separated strings\n  -G, --basic-regexp     PATTERN is a basic regular expression\n  -P, --perl-regexp      PATTERN is a Perl regular expression\n  -i, --ignore-case      ignore case distinctions\n\nOutput control:\n  -a, --absolute-path    always print absolute path of jar file\n  -s, --separator        specify the separator between jar file and zip entry.\n                         default is `!'.\n  -L, --files-not-contained-found\n                         print only names of JAR FILEs NOT contained found\n  -l, --files-contained-found\n                         print only names of JAR FILEs contained found\n  -R, --no-find-progress do not display responsive find progress\n\nMiscellaneous:\n  -h, --help             display this help and exit\n  -V, --version          display version information and exit\n```\n\n注意，Pattern缺省是`grep`的 **扩展**正则表达式。\n\n### 示例\n\n```bash\n# 在当前目录下的所有Jar文件中，查找出 log4j.properties文件\n$ find-in-jars 'log4j\\.properties$'\n./hadoop-core-0.20.2-cdh3u3.jar!log4j.properties\n......\n\n# 查找出 以Service结尾的类，Jar文件路径输出成绝对路径\n$ find-in-jars 'Service.class$' -a\n/home/foo/deploy/app/WEB-INF/libs/spring-2.5.6.SEC03.jar!org/springframework/stereotype/Service.class\n/home/foo/deploy/app/WEB-INF/libs/rpc-hello-0.0.1-SNAPSHOT.jar!com/taobao/biz/HelloService.class\n......\n\n# 在指定的多个目录的Jar文件中，查找出 properties文件\n$ find-in-jars '\\.properties$' -d WEB-INF/lib -d ../deploy/lib | grep -v '/pom\\.properties$'\nWEB-INF/lib/aspectjtools-1.6.2.jar!org/aspectj/ajdt/ajc/messages.properties\nWEB-INF/lib/aspectjweaver-1.8.8.jar!org/aspectj/weaver/XlintDefault.properties\n../deploy/lib/groovy-all-1.1-rc-1.jar!groovy/ui/InteractiveShell.properties\n../deploy/lib/httpcore-4.3.3.jar!org/apache/http/version.properties\n../deploy/lib/javax.servlet-api-3.0.1.jar!javax/servlet/http/LocalStrings_es.properties\n......\n\n# 列出 包含properties文件的Jar文件\n$ find-in-jars '\\.properties$' -l -d WEB-INF/lib\nWEB-INF/lib/aspectjtools-1.6.2.jar\nWEB-INF/lib/aspectjweaver-1.8.8.jar\nWEB-INF/lib/javax.servlet-api-3.0.1.jar\n......\n```\n\n### 运行效果\n\n支持彩色输出，文件名中的匹配部分以`grep`的高亮方式显示。\n\n![find-in-jar screenshot](https://user-images.githubusercontent.com/1063891/33545067-9eb66072-d8a2-11e7-8a77-d815c0979e5e.gif)\n\n### 参考资料\n\n[在多个Jar(Zip)文件查找Log4J配置文件的Shell命令行](http://oldratlee.github.io/458/tech/shell/find-file-in-jar-zip-files.html)\n"
  },
  {
    "path": "docs/logo.meta.txt",
    "content": "logo is created by https://www.logoly.pro\n\nfont: Zilla Slab\n\nlogo.fond-size: 60\nlogo-social.fond-size: 160\n"
  },
  {
    "path": "docs/shell.md",
    "content": "🐌 `Shell`相关脚本\n====================================\n\n<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->\n\n- [`Shell`使用加强](#shell%E4%BD%BF%E7%94%A8%E5%8A%A0%E5%BC%BA)\n    - [🍺 c](#-c)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B)\n        - [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99)\n    - [🍺 coat and taoc](#-coat-and-taoc)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-1)\n    - [🍺 a2l](#-a2l)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-2)\n    - [🍺 uq](#-uq)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-3)\n    - [🍺 ap and rp](#-ap-and-rp)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-4)\n    - [🍺 cp-into-docker-run](#-cp-into-docker-run)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-5)\n    - [🍺 tcp-connection-state-counter](#-tcp-connection-state-counter)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-6)\n        - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85)\n    - [🍺 xpl and xpf](#-xpl-and-xpf)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-7)\n        - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85-1)\n- [`Shell`开发/测试加强](#shell%E5%BC%80%E5%8F%91%E6%B5%8B%E8%AF%95%E5%8A%A0%E5%BC%BA)\n    - [🍺 echo-args](#-echo-args)\n        - [用法/示例](#%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B-8)\n        - [使用方式](#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F)\n    - [🍺 console-text-color-themes.sh](#-console-text-color-themessh)\n        - [用法](#%E7%94%A8%E6%B3%95)\n        - [示例](#%E7%A4%BA%E4%BE%8B)\n        - [运行效果](#%E8%BF%90%E8%A1%8C%E6%95%88%E6%9E%9C)\n        - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85-2)\n        - [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99-1)\n    - [🍺 parseOpts.sh](#-parseoptssh)\n        - [用法](#%E7%94%A8%E6%B3%95-1)\n        - [示例](#%E7%A4%BA%E4%BE%8B-1)\n        - [兼容性](#%E5%85%BC%E5%AE%B9%E6%80%A7)\n        - [贡献者](#%E8%B4%A1%E7%8C%AE%E8%80%85-3)\n\n<!-- END doctoc generated TOC please keep comment here to allow auto update -->\n\n`Shell`使用加强\n====================================\n\n🍺 [c](../bin/c)\n----------------------\n\n原样命令行输出，并拷贝标准输出到系统剪贴板，省去`CTRL+C`操作，优化命令行与其它应用之间的操作流。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n命令名`c`的意思是`Copy`，因为这个命令我平时非常常用，所以使用一个字符的命令名，方便快速键入。\n\n更多说明参见[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.github.io/post/2012-12-23/command-output-to-clip)。\n\n### 用法/示例\n\n有3种使用风格，根据需要或是你的偏好选取。\n\n```bash\n############################################################\n# 1. 前缀方式，后面跟上要运行的命令\n############################################################\n$ c pwd\n/Users/jerry\n$ c echo -e 'a\\nb'\na\nb\n# 这种使用方式，后面跟的命令不能是别名（alias），对于别名可以用下面的使用方式。\n\n############################################################\n# 2. 后缀方式，管道\n############################################################\n$ echo -e 'a\\nb' | nl | c\n1   a\n2   b\n# gb是oh-my-zsh的别名，列出git的分支，需要后缀的方式的使用。\n$ gb | c\n\n############################################################\n# 3. 从标准输入读取内容。拷贝文件内容时这种方式最直接。\n############################################################\n$ c < ~/.ssh/id_rsa.pub\nssh-rsa EAAAABIwAAAQEAz+ETZEgoLeIiC0rjWewdDs0sbo8c...== a@b.com\n\n############################################################\n# -q选项：拷贝但不输出。\n# 当输出内容比较多、又不关心输出内容和命令执行进展时，可以使用这个选项。\n############################################################\n$ c -q < ~/.ssh/id_rsa.pub\n\n# 帮助信息\n$ c --help\nUsage: c [OPTION]... [command [command_args ...]]\nRun command and put output to system clipper.\nIf no command is specified, read from stdin(pipe).\n\nExample:\n  c grep -i 'hello world' menu.h main.c\n  set | c\n  c -q < ~/.ssh/id_rsa.pub\n\nOptions:\n  -k, --keep-eol  do not trim new line at end of file\n  -q, --quiet     suppress all normal output, default is false\n  -h, --help      display this help and exit\n  -V, --version   display version information and exit\n```\n\n### 参考资料\n\n- [拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.github.io/post/2012-12-23/command-output-to-clip)，给出了不同系统可用命令。\n- 关于文本文件最后的换行，参见[Why should text files end with a newline?](https://stackoverflow.com/questions/729692)\n\n<a id=\"-coat\"></a>\n\n🍺 [coat](../bin/coat) and [taoc](../bin/taoc)\n----------------------\n\n彩色`cat`/`tac`出文件行，方便人眼区分不同的行。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n命令支持选项、功能和使用方式与[`cat`](https://manned.org/cat)/[`tac`](https://manned.org/tac)命令完全一样。  \n文件操作在实现上完全代理给了`cat`/`tac`命令。\n\n- 命令名`coat`的意思是`COlorful cAT`；同时单词`coat`是外套，而彩色的输出行就像件漂亮的外套～ 🌈 😆\n- 命令名`taoc`是`coat`倒序拼写；命名方式就像`tac`之于`cat`。 🐈\n\n### 用法/示例\n\n```bash\n$ echo Hello world | coat\nHello world\n$ echo -e 'Hello\\nWorld' | coat\nHello\nWorld\n$ echo -e 'Hello\\nWorld' | taoc\nWorld\nHello\n$ echo -e 'Hello\\nWorld' | nl | coat\n     1\tHello\n     2\tWorld\n$ coat file1 file2.txt\nline1 of file1\nline2 of file1\n...\nline1 of file2\nline2 of file2\n...\n\n# 帮助信息\n#   可以看到本人机器上实现代理的`cat`/`tac`命令是GNU的实现。\n$ coat --help\nUsage: coat [OPTION]... [FILE]...\ncat lines colorfully.\n\nSupport options:\n  --help     display this help and exit\n  --version  output version information and exit\nAll other options and arguments are delegated to command cat,\nmore info see the help/man of command cat(e.g. cat --help).\ncat executable: /usr/local/opt/coreutils/libexec/gnubin/cat\n\n$ taoc --help\nUsage: taoc [OPTION]... [FILE]...\ntac lines colorfully.\n\nSupport options:\n  --help     display this help and exit\n  --version  output version information and exit\nAll other options and arguments are delegated to command tac,\nmore info see the help/man of command tac(e.g. tac --help).\ntac executable: /usr/local/opt/coreutils/libexec/gnubin/tac\n```\n\n注：上面示例中，没有彩色；在控制台上运行可以看出彩色效果，如下：  \n![coat screenshot](../docs/coat.png)\n\n🍺 [a2l](../bin/a2l)\n----------------------\n\n按行彩色输出参数，方便人眼查看。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n命令名`a2l`的意思是`Arguments to(2) Lines`。\n\n### 用法/示例\n\n```bash\n$ a2l *.java\nA.java\nB.java\n...\n\n# zsh支持 **/* 跨目录glob，可以方便搜索，但是输出内容是空格分隔的不方便查看。\n# 把参数按行输出方便查看 或是 grep\n$ a2l **/*.sh\nlib/console-text-color-themes.sh\ntest/parseOpts_test.sh\ntest/self-installer.sh\n...\n```\n\n注：上面示例中，没有彩色；在控制台上运行可以看出彩色效果，和上面的`coat`命令一样。\n\n🍺 [uq](../bin/uq)\n----------------------\n\n不重排序输入完成整个输入行的去重。相比系统的`uniq`命令加强的是可以跨行去重，不需要排序输入。  \n使用方式与支持的选项 模仿系统的`uniq`命令。支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n> ‼️ **_注意_**： 去重过程会在内存持有整个输入（因为全局去重）！\n>\n> 对于输入大小较大的场景（如输入量有几G），需谨慎使用以避免占用过多内存；往往需要结合业务场景开发对应的优化实现。  \n> 虽然平时的大部分场景输入量非常有限（如几M），一个简单没有充分优化的实现是快速够用的。\n>\n> `uq`处理的最大输入量缺省是 256m（字符数），超过了最大输入量则出错退出，以避免意外消耗了过大的内存；  \n> 可以通过`-XM, --max-input`选项 设置 消耗更多内存可接受的合理最大输入量，如`uq --max-input 1g ...`\n\n因为系统的`uniq`命令去重相邻的行，需要组合`sort`命令以对整个输入去重，并且有下面的问题：\n\n```bash\n# 示例输入\n$ cat foo.txt\nc\nc\nb\na\na\nc\nc\n\n$ uniq foo.txt\nc\nb\na\nc\n# c输出了2次，原因是第二个c与第一个c不是相邻的重复行\n\n# 可以通过 sort -u 来完成整个输入去重，但这样操作，顺序与输入行不一致\n$ sort -u foo.txt\na\nb\nc\n# 输入行重排序了！\n\n# 另外一个经典的用法 sort 与 uniq -c，输出重复次数\n$ sort foo.txt | uniq -c\n      2 a\n      1 b\n      4 c\n# 输入行重排序了！\n```\n\n### 用法/示例\n\n```bash\n$ uq foo.txt # 输入是文件\n$ cat foo.txt | uq # 或是 标准输入/管道\nc\nb\na\n# 对整个输入行去重，且顺序与输入行一致（保留第一次出现的位置）\n\n# -c 选项：输出重复次数\n$ uq -c foo.txt\n      4 c\n      1 b\n      2 a\n\n# -d, --repeated 选项：只输出 重复行\n$ uq -d foo.txt\nc\na\n# -u, --unique 选项：只输出 唯一行（即不重复的行）\n$ uq -u foo.txt\nb\n\n# -D 选项：重复行都输出，即重复了几次就输出几次\n$ uq -D -c foo.txt\n      4 c\n      4 c\n      1 b\n      2 a\n      2 a\n      4 c\n      4 c\n\n# 有多个文件参数时，最后一个参数 是 输出文件\n$ uq in1.txt in2.txt out.txt\n# 当有多个输入文件时，但要输出到控制台时，指定输出文件（最后一个文件参数）为 `-` 即可\n$ uq in1.txt in2.txt -\n\n# 如果消耗更多内存可接受的合理的，可以通过 -XM, --max-input 选项设置更大的最大输入量（缺省是256m）\n$ uq -MI 768m large-file-input\n$ uq --max-input 10g huge-file-input\n\n# 帮助信息\n$ uq -h\nUsage: uq [OPTION]... [INPUT [OUTPUT]]\nFilter lines from INPUT (or standard input), writing to OUTPUT (or standard output).\nSame as `uniq` command in core utils,\nbut detect repeated lines that are not adjacent, no sorting required.\n\nExample:\n  # only one file, output to stdout\n  uq in.txt\n  # more than 1 file, last file argument is output file\n  uq in.txt out.txt\n  # when use - as output file, output to stdout\n  uq in1.txt in2.txt -\n\nOptions:\n  -c, --count           prefix lines by the number of occurrences\n  -d, --repeated        only print duplicate lines, one for each group\n  -D                    print all duplicate lines\n                        combined with -c/-d option usually\n  --all-repeated[=METHOD]  like -D, but allow separating groups\n                           with an empty line;\n                           METHOD={none(default),prepend,separate}\n  -u, --unique          Only output unique lines\n                          that are not repeated in the input\n  -i, --ignore-case     ignore differences in case when comparing\n  -z, --zero-terminated line delimiter is NUL, not newline\n  -XM, --max-input      max input size(count by char), support k,m,g postfix\n                          default is 256m\n                          avoid consuming large memory unexpectedly\n  -h, --help            display this help and exit\n  -V, --version         display version information and exit\n```\n\n🍺 [ap](../bin/ap) and [rp](../bin/rp)\n----------------------\n\n批量转换文件路径为绝对路径/相对路径，会自动跟踪链接并规范化路径。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n命令名`ap`的意思是`Absolute Path`，`rp`是`Relative Path`。\n\n### 用法/示例\n\n```bash\n# ap缺省打印当前路径的绝对路径\n$ ap\n/home/admin/useful-scripts/test\n$ ap ..\n/home/admin/useful-scripts\n# 支持多个参数\n$ ap .. ../.. /etc /etc/../etc\n/home/admin/useful-scripts\n/home/admin\n/etc\n/etc\n\n# rp当一个参数时，打印相对于当前路径的相对路径\n$ rp /home\n../..\n# 多于一个参数时，打印相对于最后一个参数的相对路径\n$ rp /home /etc/../etc /home/admin\n..\n../../etc\n```\n\n🍺 [cp-into-docker-run](../bin/cp-into-docker-run)\n----------------------\n\n一个`Docker`使用的便利脚本。拷贝本机的执行文件到指定的`docker container`中并在`docker container`中执行。   \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n### 用法/示例\n\n```bash\n# 通过 -c 选项 指定 docker container\n$ cp-into-docker-run -c container_foo /path/to/command command_args...\n# 如果 指定的command 不是一个路径，会从 PATH 中查找\n$ cp-into-docker-run -c container_foo a2l command_arg1 command_arg2\n\n# 帮助信息\n$ cp-into-docker-run -h\nUsage: cp-into-docker-run [OPTION]... command [command-args]...\n\nCopy the command into docker container\nand run the command in container.\n\nExample:\n  cp-into-docker-run -c container_foo command_copied_into_container command_arg1\n\ndocker options:\n  -c, --container    destination docker container\n  -u, --docker-user  docker username or UID to run command\n                     optional, docker default is (maybe) root user\n  -w, --workdir      absolute working directory inside the container\n                     optional, docker default is (maybe) root dir\n  -t, --tmpdir       tmp dir in docker to copy command\n                     optional, default is /tmp\n  -p, --cp-path      destination path in docker of the command(including file name)\n                     if specified, command will be kept when run finished\n                     optional, default is under tmp dir and deleted when run finished\n\nrun options:\n  -v, --verbose      show operation step infos\n\nmiscellaneous:\n  -h, --help         display this help and exit\n  -V, --version      display version information and exit\n```\n\n<a id=\"beer-tcp-connection-state-countersh\"></a>\n<a id=\"beer-tcp-connection-state-counter\"></a>\n\n🍺 [tcp-connection-state-counter](../bin/tcp-connection-state-counter)\n----------------------\n\n统计各个`TCP`连接状态的个数。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n像`Nginx`、`Apache`的机器上需要查看，`TCP`连接的个数，以判定\n\n- 连接数、负荷\n- 是否有攻击，查看`SYN_RECV`数（`SYN`攻击）\n- `TIME_WAIT`数，太多会导致`TCP: time wait bucket table overflow`。\n\n### 用法/示例\n\n```bash\n$ tcp-connection-state-counter\nCLOSE_WAIT  584\nESTABLISHED 493\nTIME_WAIT   112\nLISTEN       27\nSYN_SENT      7\n```\n\n### 贡献者\n\n[sunuslee](https://github.com/sunuslee) 改进此脚本，增加对`MacOS`的支持。 [#56](https://github.com/oldratlee/useful-scripts/pull/56)\n\n🍺 [xpl](../bin/xpl) and [xpf](../bin/xpf)\n----------------------\n\n在命令行中快速完成 在文件浏览器中 打开/选中 指定的文件或文件夹的操作，优化命令行与其它应用之间的操作流。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n- `xpl`：在文件浏览器中打开指定的文件或文件夹。  \n  `xpl`是`explorer`的缩写。\n- `xpf`: 在文件浏览器中打开指定的文件或文件夹，并选中。  \n  `xpf`是`EXplorer and select File`的缩写。\n\n### 用法/示例\n\n```bash\nxpl\n# 缺省打开当前目录\nxpl <文件或是目录>...\n# 打开多个文件或目录\n\nxpf\n# 缺省打开当前目录\nxpf <文件或是目录>...\n# 打开多个文件或目录\n\n\n# 示例\nxpl /path/to/dir\nxpl /path/to/foo.txt\nxpl /path/to/dir1 /path/to/foo1.txt\nxpf /path/to/foo1.txt\nxpf /path/to/dir1 /path/to/foo1.txt\n```\n\n### 贡献者\n\n- [Linhua Tan](https://github.com/toolchainX) 修复Linux的选定Bug。\n\n`Shell`开发/测试加强\n====================================\n\n<a id=\"beer-echo-argssh\"></a>\n<a id=\"beer-echo-args\"></a>\n\n🍺 [echo-args](../bin/echo-args)\n----------------------\n\n在编写脚本时，常常要确认输入参数是否是期望的：参数个数，参数值（可能包含有人眼不容易发现的空格问题）。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n这个脚本输出脚本收到的参数。在控制台运行时，把参数值括起的括号显示成 **红色**，方便人眼查看。\n\n### 用法/示例\n\n```bash\n$ ./echo-args 1 \"  2 foo  \" \"3        3\"\n0/3: [./echo-args]\n1/3: [1]\n2/3: [  2 foo  ]\n3/3: [3        3]\n```\n\n### 使用方式\n\n需要查看某个脚本（实际上也可以是其它的可执行程序）输出参数时，可以这么做：\n\n- 把要查看脚本重命名。\n- 建一个`echo-args`脚本的符号链接到要查看参数的脚本的位置，名字和查看脚本一样。\n\n这样可以不改其它的程序，查看到输入参数的信息。\n\n🍺 [console-text-color-themes.sh](../lib/console-text-color-themes.sh)\n----------------------\n\n显示`Terminator`的全部文字彩色组合的效果及其打印方式。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n脚本中，也给出了`colorEcho`和`colorEchoWithoutNewLine`函数更方便输出彩色文本\n\n### 用法\n\n```bash\ncolorEcho <颜色样式> <要输出的文本>...\ncolorEchoWithoutNewLine  <颜色样式> <要输出的文本>...\n```\n\n### 示例\n\n```bash\nsource console-text-color-themes.sh\n\n# 输出红色文本\ncolorEcho \"0;31;40\" \"Hello world!\"\n# 输出黄色带下划线的文本\ncolorEchoWithoutNewLine \"4;33;40\" \"Hello world!\" \"Hello Hell!\"\n```\n\n### 运行效果\n\n![console-text-color-themes.sh的运行效果图](console-colorful-text.png)\n\n### 贡献者\n\n[姜太公](https://github.com/jzwlqx) 提供循环输出彩色组合的脚本。\n\n### 参考资料\n\n- [utensil](https://github.com/utensil)\n  的[在Bash下输出彩色的文本](http://utensil.github.io/tech/2007/09/10/colorful-bash.html)，这是篇很有信息量很钻研的文章！\n\n🍺 [parseOpts.sh](../lib/parseOpts.sh)\n----------------------\n\n命令行选项解析库，加强支持选项有多个值（即数组）。  \n支持`Linux`、`Mac`、`Windows`（`cygwin`、`MSSYS`）。\n\n自己写一个命令行选项解析函数，是因为[`bash`](https://manned.org/bash)的`builtin`命令[`getopts`](https://manned.org/man/getopts.1)和加强版本命令[`getopt`](https://manned.org/getopt)都不支持数组的值。\n\n指定选项的多个值（即数组）的风格模仿[`find`](https://manned.org/find)命令的`-exec`选项：\n\n```bash\n$ find . -name \\*.txt -exec echo \"find file: \" {} \\;\nfind file: foo.txt\nfind file: bar.txt\n...\n```\n\n### 用法\n\n`parseOpts`函数的第一个参数是要解析的选项说明，后面跟实际要解析的输入参数。\n\n选项说明可以长选项和短选项，用逗号分隔，如`a,a-long`。不同选项的说明间用竖号分隔，如`a,a-long|b,b-long:`。\n\n选项说明最后可以有选项类型说明：\n\n- `-`： 无参数的选项。既有选项则把值设置成`true`。这是 ***缺省*** 的类型。\n- `:`： 有参数的选项，值只有一个。\n- `+`： 有多个参数值的选项。值列表要以`;`表示结束。  \n  注意，`;`是`Bash`的元字符（用于一行中多个命令分隔），所以加上转义写成`\\;`（当然也可以按你的喜好写成`\";\"`或`';'`）。\n\n实际要解析的输入参数往往是你的脚本参数，这样`parseOpts`函数调用一般是：\n\n```bash\nparseOpts \"a,a-long|b,b-long:|c,c-long+\" \"$@\"\n# \"$@\" 即是回放你的脚本参数\n```\n\n通过约定的全局变量来获取选项和参数：\n\n- 选项名为`a`，通过全局变量`_OPT_VALUE_a`来获取选项的值。\n- 选项名为`a-long`，通过全局变量`_OPT_VALUE_a_long`来获取选项的值。  \n  即，把选项名的`-`转`_`，再加上前缀`_OPT_VALUE_`对应的全局变量来获得选项值。\n- 除了选项剩下的参数，通过全局变量`_OPT_ARGS`来获取。\n\n按照惯例，输入参数中如果有`--`表示之后参数中不再有选项，即之后都是参数。\n\n### 示例\n\n```bash\n# 导入parseOpts.sh\nsource /path/to/parseOpts.sh\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+\" -a -b bv --c-long c.sh -p pv -q qv arg1 \\; aa bb cc\n# 可以通过下面全局变量来获得解析的参数值：\n#    _OPT_VALUE_a = true\n#    _OPT_VALUE_a_long = true\n#    _OPT_VALUE_b = bv\n#    _OPT_VALUE_b_long = bv\n#    _OPT_VALUE_c = (c.sh -p pv -q qv arg1) ，数组类型\n#    _OPT_VALUE_c_long = (c.sh -p pv -q qv arg1) ，数组类型\n#    _OPT_ARGS = (aa bb cc) ，数组类型\n```\n\n`--`的使用效果示例：\n\n```bash\n# 导入parseOpts.sh\nsource /path/to/parseOpts.sh\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+\" -a -b bv -- --c-long c.sh -p pv -q qv arg1 \\; aa bb cc\n# 可以通过下面全局变量来获得解析的参数值：\n#    _OPT_VALUE_a = true\n#    _OPT_VALUE_a_long = true\n#    _OPT_VALUE_b = bv\n#    _OPT_VALUE_b_long = bv\n#    _OPT_VALUE_c 没有设置过\n#    _OPT_VALUE_c_long 没有设置过\n#    _OPT_ARGS = (--c-long c.sh -p pv -q qv arg1 ';' aa bb cc) ，数组类型\n```\n\n### 兼容性\n\n这个脚本比较复杂，测试过的环境有：\n\n1. `bash --version`  \n   `GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)`  \n   `uname -a`  \n   `Linux foo-host 2.6.32-41-generic #94-Ubuntu SMP Fri Jul 6 18:00:34 UTC 2012 x86_64 GNU/Linux`\n1. `bash --version`  \n   `GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin14)`  \n   `uname -a`  \n   `Darwin foo-host 14.0.0 Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64 i386 MacBookPro10,1 Darwin`\n1. `bash --version`  \n   `GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)`  \n   `uname -a`  \n   `Linux foo-host 2.6.9-103.ELxenU #1 SMP Wed Mar 14 16:31:15 CST 2012 i686 i686 i386 GNU/Linux`\n\n### 贡献者\n\n- [Khotyn Huang](https://github.com/khotyn) 指出`bash` `3.0`下使用有问题，并提供`bash` `3.0`的测试机器。\n\n"
  },
  {
    "path": "test/chore/bump-scripts-version.sh",
    "content": "#!/usr/bin/env bash\nset -eEuo pipefail\n\n################################################################################\n# util functions\n################################################################################\n\n# NOTE: $'foo' is the escape sequence syntax of bash\nreadonly NL=$'\\n' # new line\n\ncolorPrint() {\n  local color=$1\n  shift\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [ -t 1 ]; then\n    printf '\\e[1;%sm%s\\e[0m\\n' \"$color\" \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\nredPrint() {\n  colorPrint 31 \"$@\"\n}\n\nyellowPrint() {\n  colorPrint 33 \"$@\"\n}\n\nbluePrint() {\n  colorPrint 36 \"$@\"\n}\n\nlogAndRun() {\n  local simple_mode=false\n  [ \"$1\" = \"-s\" ] && {\n    simple_mode=true\n    shift\n  }\n\n  if $simple_mode; then\n    echo \"Run under work directory $PWD : $*\"\n    \"$@\"\n  else\n    bluePrint \"Run under work directory $PWD :$NL$*\"\n    time \"$@\"\n  fi\n}\n\ndie() {\n  redPrint \"Error: $*\" >&2\n  exit 1\n}\n\n################################################################################\n# biz logic\n################################################################################\n\n(($# != 1)) && die \"need only 1 argument for version!$NL${NL}usage:$NL  $0 3.x.y\"\nreadonly bump_version=$1\n\n# adjust current dir to project dir\n#\n# Bash Pitfalls#5\n#  http://mywiki.wooledge.org/BashPitfalls#cd_.24.28dirname_.22.24f.22.29\ncd -P -- \"$(dirname -- \"$0\")\"/..\n\n# Bash Pitfalls#1\n#  http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29\nlogAndRun find -D exec bin lib -type f -exec \\\n  sed -ri \"s/^(.*\\bPROG_VERSION\\s*=\\s*')\\S*('.*)$/\\1$bump_version\\2/\" -- \\\n  {} +\n"
  },
  {
    "path": "test/chore/integration-test.sh",
    "content": "#!/usr/bin/env bash\nset -eEuo pipefail\n\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\ncd \"$(dirname -- \"$(realpath \"${BASH_SOURCE[0]}\")\")\"/..\n\n################################################################################\n# common util functions\n################################################################################\n\ncolorEcho() {\n  local color=$1\n  shift\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [[ -t 1 || \"${GITHUB_ACTIONS:-}\" = true ]]; then\n    printf '\\e[1;%sm%s\\e[0m\\n' \"$color\" \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\nredEcho() {\n  colorEcho 31 \"$@\"\n}\n\nyellowEcho() {\n  colorEcho 33 \"$@\"\n}\n\nblueEcho() {\n  colorEcho 36 \"$@\"\n}\n\nlogAndRun() {\n  local simple_mode=false\n  [ \"$1\" == \"-s\" ] && {\n    simple_mode=true\n    shift\n  }\n\n  if $simple_mode; then\n    echo \"Run under work directory $PWD : $*\"\n    \"$@\"\n  else\n    # NOTE: $'foo' is the escape sequence syntax of bash\n    local nl=$'\\n' # new line\n    blueEcho \"Run under work directory $PWD :$nl$*\"\n    time \"$@\"\n  fi\n}\n\n################################################################################\n# run *_test.sh unit test cases\n################################################################################\n\nfor test_case in *_test.sh; do\n  logAndRun ./\"$test_case\"\ndone\n"
  },
  {
    "path": "test/chore/lint.sh",
    "content": "#!/usr/bin/env bash\nset -eEuo pipefail\n\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\n# cd to the root of the project\ncd \"$(dirname -- \"$(realpath \"${BASH_SOURCE[0]}\")\")\"/../..\n\nfind bin lib -type f |\n  grep -Pv '/show-duplicate-java-classes$' |\n  grep -Pv '/\\.editorconfig$' |\n  xargs --verbose shellcheck --shell=bash\n"
  },
  {
    "path": "test/my_unit_test_lib.sh",
    "content": "#!/usr/bin/env bash\n# unit test lib\n\n#################################################\n# commons functions\n#################################################\n\n__ut_colorEcho() {\n  local color=$1\n  shift\n  # if stdout is a terminal, turn on color output.\n  #   '-t' check: is a terminal?\n  #   check isatty in bash https://stackoverflow.com/questions/10022323\n  if [[ -t 1 || \"${GITHUB_ACTIONS:-}\" = true ]]; then\n    printf '\\e[1;%sm%s\\e[0m\\n' \"$color\" \"$*\"\n  else\n    printf '%s\\n' \"$*\"\n  fi\n}\n\nredEcho() {\n  __ut_colorEcho 31 \"$@\"\n}\n\ngreenEcho() {\n  __ut_colorEcho 32 \"$@\"\n}\n\nyellowEcho() {\n  __ut_colorEcho 33 \"$@\"\n}\n\nblueEcho() {\n  __ut_colorEcho 34 \"$@\"\n}\n\nfail() {\n  redEcho \"TEST FAIL: $*\"\n  exit 1\n}\n\ndie() {\n  redEcho \"Error: $*\" >&2\n  exit 1\n}\n\n#################################################\n# assertion functions\n#################################################\n\nassertArrayEquals() {\n  (($# == 2 || $# == 3)) || die \"assertArrayEquals must 2 or 3 arguments!\"\n  local failMsg=\n  (($# == 3)) && {\n    failMsg=$1\n    shift\n  }\n\n  local a1PlaceHolder=\"$1[@]\"\n  local a2PlaceHolder=\"$2[@]\"\n  local a1=(\"${!a1PlaceHolder}\")\n  local a2=(\"${!a2PlaceHolder}\")\n\n  ((${#a1[@]} == ${#a2[@]})) || fail \"assertArrayEquals array length [${#a1[@]}] != [${#a2[@]}]${failMsg:+: $failMsg}\"\n\n  local i\n  for ((i = 0; i < ${#a1[@]}; i++)); do\n    [ \"${a1[$i]}\" = \"${a2[$i]}\" ] || fail \"assertArrayEquals fail element $i: [${a1[$i]}] != [${a2[$i]}]${failMsg:+: $failMsg}\"\n  done\n}\n\nassertEquals() {\n  (($# == 2 || $# == 3)) || die \"assertEqual must 2 or 3 arguments!\"\n  local failMsg=\"\"\n  (($# == 3)) && {\n    failMsg=$1\n    shift\n  }\n  [ \"$1\" == \"$2\" ] || fail \"assertEqual fail [$1] != [$2]${failMsg:+: $failMsg}\"\n}\n\nreadonly __ut_exclude_vars_builtin='^BASH_|^_=|^COLUMNS=|LINES='\nreadonly __ut_exclude_vars_ut_functions='^FUNCNAME=|^test_'\n\nassertAllVarsSame() {\n  local test_afterVars\n  test_afterVars=$(declare)\n\n  diff \\\n    <(echo \"$test_beforeVars\" | grep -Ev \"$__ut_exclude_vars_builtin\") \\\n    <(echo \"$test_afterVars\" | grep -Ev \"$__ut_exclude_vars_builtin|$__ut_exclude_vars_ut_functions\") ||\n    fail \"assertAllVarsSame: Unexpected extra global vars!\"\n}\n\nassertAllVarsExcludeOptVarsSame() {\n  local test_afterVars\n  test_afterVars=$(declare)\n\n  diff \\\n    <(echo \"$test_beforeVars\" | grep -Ev \"$__ut_exclude_vars_builtin\") \\\n    <(echo \"$test_afterVars\" | grep -Ev \"$__ut_exclude_vars_builtin|$__ut_exclude_vars_ut_functions\"'|^_OPT_|^_opts_index_name_') ||\n    fail \"assertAllVarsExcludeOptVarsSame: Unexpected extra global vars!\"\n}\n\ntest_beforeVars=$(declare)\n"
  },
  {
    "path": "test/parseOpts_test.sh",
    "content": "#!/usr/bin/env bash\n\nBASE=\"$(dirname -- \"${BASH_SOURCE[0]}\")\"\n\nsource \"$BASE/../lib/parseOpts.sh\"\n\nsource \"$BASE/my_unit_test_lib.sh\"\n\n#################################################\n# Test\n#################################################\n\n# ========================================\nblueEcho \"Test case: success parse\"\n# ========================================\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+\" aa -a -b bb -c c.sh -p pv -q qv cc \\; bb --d-long d.sh -x xv d1 d2 d3 \\; cc dd ee\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 0)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 4)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n\n[[ $_OPT_VALUE_a == \"true\" && $_OPT_VALUE_a_long == \"true\" ]] || fail \"Wrong option value of a!\"\n[[ $_OPT_VALUE_b == \"bb\" && $_OPT_VALUE_b_long == \"bb\" ]] || fail \"Wrong option value of b!\"\n\ntest_cArray=(c.sh -p pv -q qv cc)\nassertArrayEquals \"Wrong option value of c!\" test_cArray _OPT_VALUE_c\nassertArrayEquals \"Wrong option value of c!\" test_cArray _OPT_VALUE_c_long\n\ntest_dArray=(d.sh -x xv d1 d2 d3)\nassertArrayEquals \"Wrong option value of d!\" test_dArray _OPT_VALUE_d\nassertArrayEquals \"Wrong option value of d!\" test_dArray _OPT_VALUE_d_long\n\ntest_argArray=(aa bb cc dd ee)\nassertArrayEquals \"Wrong args!\" test_argArray _OPT_ARGS\n\nassertAllVarsExcludeOptVarsSame\n\n_opts_cleanOptValueInfoList\nassertAllVarsSame\n\n# ========================================\nblueEcho \"Test case: success parse with -- \"\n# ========================================\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+\" aa -a -b bb -c c.sh -p pv -q qv cc \\; bb -- --d-long d.sh -x xv d1 d2 d3 \\; cc dd ee\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 0)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 4)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n\n[[ $_OPT_VALUE_a == \"true\" && $_OPT_VALUE_a_long == \"true\" ]] || fail \"Wrong option value of a!\"\n[[ $_OPT_VALUE_b == \"bb\" && $_OPT_VALUE_b_long == \"bb\" ]] || fail \"Wrong option value of b!\"\n\ntest_cArray=(c.sh -p pv -q qv cc)\nassertArrayEquals \"Wrong option value of c!\" test_cArray _OPT_VALUE_c\nassertArrayEquals \"Wrong option value of c!\" test_cArray _OPT_VALUE_c_long\n\n[[ \"$_OPT_VALUE_d\" == \"\" && \"$_OPT_VALUE_d_long\" == \"\" ]] || fail \"Wrong option value of d!\"\n\ntest_argArray=(aa bb --d-long d.sh -x xv d1 d2 d3 \\; cc dd ee)\nassertArrayEquals \"Wrong args!\" test_argArray _OPT_ARGS\n\nassertAllVarsExcludeOptVarsSame\n\n_opts_cleanOptValueInfoList\nassertAllVarsSame\n\n# ========================================\nblueEcho \"Test case: illegal option x\"\n# ========================================\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+\" aa -a -b bb -x -c c.sh -p pv -q qv cc \\; bb --d-long d.sh -x xv d1 d2 d3 \\; cc -- dd ee\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 232)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 0)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n[[ \"$_OPT_VALUE_a\" == \"\" && \"$_OPT_VALUE_a_long\" == \"\" ]] || fail \"Wrong option value of a!\"\n[[ \"$_OPT_VALUE_b\" == \"\" && \"$_OPT_VALUE_b_long\" == \"\" ]] || fail \"Wrong option value of b!\"\n[[ \"$_OPT_VALUE_c\" == \"\" && \"$_OPT_VALUE_c_long\" == \"\" ]] || fail \"Wrong option value of c!\"\n[[ \"$_OPT_VALUE_d\" == \"\" && \"$_OPT_VALUE_d_long\" == \"\" ]] || fail \"Wrong option value of d!\"\n[ \"$_OPT_ARGS\" == \"\" ] || fail \"Wrong args!\"\n\n# ========================================\nblueEcho \"Test case: empty options\"\n# ========================================\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+\"\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 0)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 4)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n[[ \"$_OPT_VALUE_a\" == \"\" && \"$_OPT_VALUE_a_long\" == \"\" ]] || fail \"Wrong option value of a!\"\n[[ \"$_OPT_VALUE_b\" == \"\" && \"$_OPT_VALUE_b_long\" == \"\" ]] || fail \"Wrong option value of b!\"\n[[ \"$_OPT_VALUE_c\" == \"\" && \"$_OPT_VALUE_c_long\" == \"\" ]] || fail \"Wrong option value of c!\"\n[[ \"$_OPT_VALUE_d\" == \"\" && \"$_OPT_VALUE_d_long\" == \"\" ]] || fail \"Wrong option value of d!\"\n[ \"$_OPT_ARGS\" == \"\" ] || fail \"Wrong args!\"\n\n# ========================================\nblueEcho \"Test case: illegal option name\"\n# ========================================\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+|#,z-long\" aa -a -b bb -x -c c.sh -p pv -q qv cc \\; bb -d d.sh -x xv d1 d2 d3 \\; cc -- dd ee\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 221)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 0)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n[[ \"$_OPT_VALUE_a\" == \"\" && \"$_OPT_VALUE_a_long\" == \"\" ]] || fail \"Wrong option value of a!\"\n[[ \"$_OPT_VALUE_b\" == \"\" && \"$_OPT_VALUE_b_long\" == \"\" ]] || fail \"Wrong option value of b!\"\n[[ \"$_OPT_VALUE_c\" == \"\" && \"$_OPT_VALUE_c_long\" == \"\" ]] || fail \"Wrong option value of c!\"\n[[ \"$_OPT_VALUE_d\" == \"\" && \"$_OPT_VALUE_d_long\" == \"\" ]] || fail \"Wrong option value of d!\"\n[ \"$_OPT_ARGS\" == \"\" ] || fail \"Wrong args!\"\n\nparseOpts \"a,a-long|b,b-long:|c,c-long+|d,d-long+|z,z-#long\" aa -a -b bb -x -c c.sh -p pv -q qv cc \\; bb -d d.sh -x xv d1 d2 d3 \\; cc -- dd ee\ntest_exitCode=$?\n_opts_showOptDescInfoList\n_opts_showOptValueInfoList\n\n((test_exitCode == 222)) || fail \"Wrong exit code!\"\n((${#_OPT_INFO_LIST_INDEX[@]} == 0)) || fail \"Wrong _OPT_INFO_LIST_INDEX!\"\n[[ \"$_OPT_VALUE_a\" == \"\" && \"$_OPT_VALUE_a_long\" == \"\" ]] || fail \"Wrong option value of a!\"\n[[ \"$_OPT_VALUE_b\" == \"\" && \"$_OPT_VALUE_b_long\" == \"\" ]] || fail \"Wrong option value of b!\"\n[[ \"$_OPT_VALUE_c\" == \"\" && \"$_OPT_VALUE_c_long\" == \"\" ]] || fail \"Wrong option value of c!\"\n[[ \"$_OPT_VALUE_d\" == \"\" && \"$_OPT_VALUE_d_long\" == \"\" ]] || fail \"Wrong option value of d!\"\n[ \"$_OPT_ARGS\" == \"\" ] || fail \"Wrong args!\"\n\nassertAllVarsSame\n\ngreenEcho \"TEST SUCCESS!!!\"\n"
  },
  {
    "path": "test/self-installer.sh",
    "content": "#!/usr/bin/env bash\n\nif [ ! -d \"/tmp/useful-scripts-$USER\" ]; then\n  if type -P git &>/dev/null; then\n    git clone https://github.com/oldratlee/useful-scripts.git \"/tmp/useful-scripts-$USER\"\n  elif type -P svn &>/dev/null; then\n    svn checkout https://github.com/oldratlee/useful-scripts/branches/release-3.x \"/tmp/useful-scripts-$USER\"\n  else\n    echo \"fail to find command git/svn\"\n    return 1\n  fi\nfi\n\nexport PATH=\"$PATH:/tmp/useful-scripts-$USER/bin\"\n"
  },
  {
    "path": "test/uq_test.sh",
    "content": "#!/usr/bin/env bash\nset -eEuo pipefail\n\nrealpath() {\n  [ -e \"$1\" ] && command realpath -- \"$1\"\n}\n\nBASE=$(dirname -- \"$(realpath \"${BASH_SOURCE[0]}\")\")\ncd \"$BASE\"\n\n#################################################\n# commons and test data\n#################################################\n\nreadonly uq=\"../bin/uq\"\n# NOTE: $'foo' is the escape sequence syntax of bash\nreadonly nl=$'\\n' # new line\n\ntest_input=$(cat uq_test_input)\n\n#################################################\n# test cases\n#################################################\n\ntest_uq_simple() {\n  assertEquals \"c${nl}v${nl}a${nl}u\" \\\n    \"$(echo \"$test_input\" | \"$uq\")\"\n  assertEquals \"c${nl}v${nl}a${nl}u\" \\\n    \"$(\"$uq\" uq_test_input)\"\n\n  assertEquals \"c${nl}a\" \\\n    \"$(echo \"$test_input\" | \"$uq\" -d)\"\n  assertEquals \"c${nl}a\" \\\n    \"$(\"$uq\" -d uq_test_input)\"\n\n  assertEquals \"v${nl}u\" \"$(echo \"$test_input\" | \"$uq\" -u)\"\n  assertEquals \"v${nl}u\" \"$(\"$uq\" -u uq_test_input)\"\n}\n\nreadonly test_output_uq_count='      4 c\n      1 v\n      2 a\n      1 u'\n\nreadonly test_output_uq_D_count='      4 c\n      4 c\n      1 v\n      2 a\n      2 a\n      4 c\n      4 c\n      1 u'\n\ntest_uq_count() {\n  assertEquals \"$test_output_uq_count\" \"$(echo \"$test_input\" | \"$uq\" -c)\"\n  assertEquals \"$test_output_uq_count\" \"$(\"$uq\" -c uq_test_input)\"\n\n  assertEquals \"$test_output_uq_D_count\" \"$(echo \"$test_input\" | \"$uq\" -D -c)\"\n  assertEquals \"$test_output_uq_D_count\" \"$(\"$uq\" -D -c uq_test_input)\"\n}\n\ntest_uq_only_D_option__same_as_cat() {\n  assertEquals \"$test_input\" \"$(echo \"$test_input\" | \"$uq\" -D)\"\n  assertEquals \"$test_input\" \"$(\"$uq\" -D uq_test_input)\"\n}\n\ntest_multi_input_files__output_file() {\n  local output_file=\"$SHUNIT_TMPDIR/uq_output_file_${$}_${RANDOM}_${RANDOM}\"\n  \"$uq\" uq_test_input uq_test_another_input \"$output_file\"\n  assertEquals \"c${nl}v${nl}a${nl}u${nl}m${nl}x\" \\\n    \"$(cat \"$output_file\")\"\n\n  local output_file=\"$SHUNIT_TMPDIR/uq_output_file_${$}_${RANDOM}_${RANDOM}\"\n  \"$uq\" -d uq_test_input uq_test_another_input \"$output_file\"\n  assertEquals \"c${nl}a${nl}m\" \\\n    \"$(cat \"$output_file\")\"\n\n  local output_file=\"$SHUNIT_TMPDIR/uq_output_file_${$}_${RANDOM}_${RANDOM}\"\n  \"$uq\" -u uq_test_input uq_test_another_input \"$output_file\"\n  assertEquals \"v${nl}u${nl}x\" \\\n    \"$(cat \"$output_file\")\"\n}\n\ntest_multi_input_files__output_stdout() {\n  assertEquals \"c${nl}v${nl}a${nl}u${nl}m${nl}x\" \"$(\"$uq\" uq_test_input uq_test_another_input -)\"\n\n  assertEquals \"c${nl}a${nl}m\" \"$(\"$uq\" -d uq_test_input uq_test_another_input -)\"\n\n  assertEquals \"v${nl}u${nl}x\" \"$(\"$uq\" -u uq_test_input uq_test_another_input -)\"\n}\n\ntest_ignore_case() {\n  local input=\"a${nl}b${nl}A\"\n\n  assertEquals \"a${nl}b${nl}A\" \"$(echo \"$input\" | \"$uq\")\"\n  assertEquals \"a${nl}b\" \"$(echo \"$input\" | \"$uq\" -i)\"\n}\n\ntest_ignore_case__count() {\n  local input=\"a${nl}b${nl}A\"\n\n  assertEquals \"      1 a${nl}      1 b${nl}      1 A\" \\\n    \"$(echo \"$input\" | \"$uq\" -c)\"\n\n  assertEquals \"      2 a${nl}      1 b\" \\\n    \"$(echo \"$input\" | \"$uq\" -i -c)\"\n\n  assertEquals \"      2 a${nl}      1 b${nl}      2 A\" \\\n    \"$(echo \"$input\" | \"$uq\" -i -D -c)\"\n}\n\ntest_max_input_check() {\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\"'\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\" -XM 4'\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\" -XM 1k'\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\" --max-input 1042k'\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\" --max-input 1m'\n  # shellcheck disable=SC2016\n  assertTrue 'echo 123 | \"$uq\" --max-input 10420g'\n  # shellcheck disable=SC2016\n  assertTrue '\"$uq\" uq_test_input'\n  # shellcheck disable=SC2016\n  assertTrue '\"$uq\" uq_test_input -XM 42m'\n  # shellcheck disable=SC2016\n  assertTrue '\"$uq\" uq_test_input --max-input 1024000g'\n  # shellcheck disable=SC2016\n  assertTrue '\"$uq\" uq_test_input --max-input 1234567890g'\n\n  # shellcheck disable=SC2016\n  assertFalse 'should fail by -XM' 'echo -e 123 | \"$uq\" -XM 1'\n  # shellcheck disable=SC2016\n  assertFalse 'should fail by -XM' 'echo -e 123 | \"$uq\" -XM 3'\n  # shellcheck disable=SC2016\n  assertFalse 'should fail by --max-input' 'echo -e 123 | \"$uq\" --max-input 2'\n  # shellcheck disable=SC2016\n  assertFalse 'should fail by --max-input' '\"$uq\" --max-input 2 uq_test_input'\n\n  # shellcheck disable=SC2016\n  assertFalse 'should fail, number overflow!' '\"$uq\" uq_test_input --max-input 12345678901g'\n}\n\n#################################################\n# Load and run shUnit2.\n#################################################\n\nsource \"$BASE/shunit2-lib/shunit2\"\n"
  },
  {
    "path": "test/uq_test_another_input",
    "content": "a\nm\nm\nx\n"
  },
  {
    "path": "test/uq_test_input",
    "content": "c\nc\nv\na\na\nc\nc\nu\n"
  }
]