ShellCheck
1.安装 ShellCheck
ShellCheck 是一个用于静态分析 Shell 脚本的工具,它能够检测出脚本中的语法错误、潜在的问题和最佳实践违规,并提供相应的建议和修复方法。它支持包括 Bash、sh、dash、ksh 等多种 Shell 方言。
[root@moss-test-service-configure-x-9fb68fc98-mw7qh /]# yum install -y ShellCheck
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ShellCheck.x86_64 0:0.3.8-1.el7 will be installed
--> Processing Dependency: ghc(unix-2.6.0.1-4f219f792083f70ec440ce406f12ce57) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(transformers-0.3.0.0-ff2bb6ac67241ebb987351a3db564af0) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(time-1.4.0.1-10dc4804a19dc0000fab79908f1a9f50) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(text-0.11.3.1-e38859e86485c167fa7c9441789e7607) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(syb-0.4.0-c48d52f3188b986ddaa9dd9ae40072f8) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(regex-tdfa-1.1.8-a4710d62e3e3140fab46c3ea204d8204) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(regex-base-0.93.2-f9403610b59f8cc474edd63a82806d18) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(pretty-1.1.1.0-66181c695e6a2e173ba2088cf55cc396) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(parsec-3.1.3-6c6e21cb4ed4ef9d58fdf442940b152e) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(old-locale-1.0.0.5-6729cb9d9cc62d150655de8fc5401b91) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(mtl-2.1.2-94c72af955e94b8d7b2f359dadd0cb62) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(json-0.7-f33acd6f673691de79a840401bdfebca) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(integer-gmp-0.5.0.0-2f15426f5b53fe4c6490832f9b20d8d7) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(ghc-prim-0.3.0.0-d5221a8c8a269b66ab9a07bdc23317dd) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(filepath-1.3.0.1-b12cbe18566fe1532a1fda4c85e31cbe) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(directory-1.2.0.1-91a788fd88acd7f149f0f10f5f1e23f2) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(deepseq-1.3.0.1-5cc4cd89bdc2e8f6db1833d95ec36926) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(containers-0.5.0.0-ab1dae9a94cd3cc84e7b2805636ebfa2) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(bytestring-0.10.0.2-4f93248f75667c2c3321a7a6761b576f) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(base-4.6.0.1-8aa5d403c45ea59dcd2c39f123e27d57) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(array-0.4.0.1-3b78425c10ff2dad7acf7e8c8ae014c3) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: ghc(ShellCheck-0.3.8-d1a7aa838c58150efd69eb3b9c413904) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSunix-2.6.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHStransformers-0.3.0.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHStime-1.4.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHStext-0.11.3.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSsyb-0.4.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSrts-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSregex-tdfa-1.1.8-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSregex-base-0.93.2-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSpretty-1.1.1.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSparsec-3.1.3-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSold-locale-1.0.0.5-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSmtl-2.1.2-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSjson-0.7-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSinteger-gmp-0.5.0.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSghc-prim-0.3.0.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSfilepath-1.3.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSdirectory-1.2.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSdeepseq-1.3.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHScontainers-0.5.0.0-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSbytestring-0.10.0.2-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSbase-4.6.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSarray-0.4.0.1-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Processing Dependency: libHSShellCheck-0.3.8-ghc7.6.3.so()(64bit) for package: ShellCheck-0.3.8-1.el7.x86_64
--> Running transaction check
---> Package ghc-ShellCheck.x86_64 0:0.3.8-1.el7 will be installed
---> Package ghc-array.x86_64 0:0.4.0.1-26.4.el7 will be installed
---> Package ghc-base.x86_64 0:4.6.0.1-26.4.el7 will be installed
---> Package ghc-bytestring.x86_64 0:0.10.0.2-26.4.el7 will be installed
---> Package ghc-containers.x86_64 0:0.5.0.0-26.4.el7 will be installed
---> Package ghc-deepseq.x86_64 0:1.3.0.1-26.4.el7 will be installed
---> Package ghc-directory.x86_64 0:1.2.0.1-26.4.el7 will be installed
---> Package ghc-filepath.x86_64 0:1.3.0.1-26.4.el7 will be installed
---> Package ghc-json.x86_64 0:0.7-4.el7 will be installed
---> Package ghc-mtl.x86_64 0:2.1.2-27.el7 will be installed
---> Package ghc-old-locale.x86_64 0:1.0.0.5-26.4.el7 will be installed
---> Package ghc-parsec.x86_64 0:3.1.3-31.el7 will be installed
---> Package ghc-pretty.x86_64 0:1.1.1.0-26.4.el7 will be installed
---> Package ghc-regex-base.x86_64 0:0.93.2-29.el7 will be installed
---> Package ghc-regex-tdfa.x86_64 0:1.1.8-11.el7 will be installed
---> Package ghc-syb.x86_64 0:0.4.0-35.el7 will be installed
---> Package ghc-text.x86_64 0:0.11.3.1-2.el7 will be installed
---> Package ghc-time.x86_64 0:1.4.0.1-26.4.el7 will be installed
---> Package ghc-transformers.x86_64 0:0.3.0.0-34.el7 will be installed
---> Package ghc-unix.x86_64 0:2.6.0.1-26.4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================================================================================
Installing:
ShellCheck x86_64 0.3.8-1.el7 epel 512 k
Installing for dependencies:
ghc-ShellCheck x86_64 0.3.8-1.el7 epel 555 k
ghc-array x86_64 0.4.0.1-26.4.el7 epel 113 k
ghc-base x86_64 4.6.0.1-26.4.el7 epel 1.6 M
ghc-bytestring x86_64 0.10.0.2-26.4.el7 epel 182 k
ghc-containers x86_64 0.5.0.0-26.4.el7 epel 287 k
ghc-deepseq x86_64 1.3.0.1-26.4.el7 epel 45 k
ghc-directory x86_64 1.2.0.1-26.4.el7 epel 59 k
ghc-filepath x86_64 1.3.0.1-26.4.el7 epel 60 k
ghc-json x86_64 0.7-4.el7 epel 96 k
ghc-mtl x86_64 2.1.2-27.el7 epel 33 k
ghc-old-locale x86_64 1.0.0.5-26.4.el7 epel 50 k
ghc-parsec x86_64 3.1.3-31.el7 epel 105 k
ghc-pretty x86_64 1.1.1.0-26.4.el7 epel 57 k
ghc-regex-base x86_64 0.93.2-29.el7 epel 28 k
ghc-regex-tdfa x86_64 1.1.8-11.el7 epel 553 k
ghc-syb x86_64 0.4.0-35.el7 epel 39 k
ghc-text x86_64 0.11.3.1-2.el7 epel 379 k
ghc-time x86_64 1.4.0.1-26.4.el7 epel 187 k
ghc-transformers x86_64 0.3.0.0-34.el7 epel 100 k
ghc-unix x86_64 2.6.0.1-26.4.el7 epel 160 k
Transaction Summary
==============================================================================================================================================================================================================================================================================
Install 1 Package (+20 Dependent packages)
Total download size: 5.1 M
Installed size: 34 M
Downloading packages:
(1/21): ShellCheck-0.3.8-1.el7.x86_64.rpm | 512 kB 00:00:00
(2/21): ghc-ShellCheck-0.3.8-1.el7.x86_64.rpm | 555 kB 00:00:00
(3/21): ghc-array-0.4.0.1-26.4.el7.x86_64.rpm | 113 kB 00:00:00
(4/21): ghc-bytestring-0.10.0.2-26.4.el7.x86_64.rpm | 182 kB 00:00:00
(5/21): ghc-base-4.6.0.1-26.4.el7.x86_64.rpm | 1.6 MB 00:00:00
(6/21): ghc-containers-0.5.0.0-26.4.el7.x86_64.rpm | 287 kB 00:00:00
(7/21): ghc-deepseq-1.3.0.1-26.4.el7.x86_64.rpm | 45 kB 00:00:00
(8/21): ghc-directory-1.2.0.1-26.4.el7.x86_64.rpm | 59 kB 00:00:00
(9/21): ghc-filepath-1.3.0.1-26.4.el7.x86_64.rpm | 60 kB 00:00:00
(10/21): ghc-json-0.7-4.el7.x86_64.rpm | 96 kB 00:00:00
(11/21): ghc-mtl-2.1.2-27.el7.x86_64.rpm | 33 kB 00:00:00
(12/21): ghc-old-locale-1.0.0.5-26.4.el7.x86_64.rpm | 50 kB 00:00:00
(13/21): ghc-parsec-3.1.3-31.el7.x86_64.rpm | 105 kB 00:00:00
(14/21): ghc-pretty-1.1.1.0-26.4.el7.x86_64.rpm | 57 kB 00:00:00
(15/21): ghc-regex-base-0.93.2-29.el7.x86_64.rpm | 28 kB 00:00:00
(16/21): ghc-syb-0.4.0-35.el7.x86_64.rpm | 39 kB 00:00:00
(17/21): ghc-regex-tdfa-1.1.8-11.el7.x86_64.rpm | 553 kB 00:00:00
(18/21): ghc-text-0.11.3.1-2.el7.x86_64.rpm | 379 kB 00:00:00
(19/21): ghc-time-1.4.0.1-26.4.el7.x86_64.rpm | 187 kB 00:00:00
(20/21): ghc-transformers-0.3.0.0-34.el7.x86_64.rpm | 100 kB 00:00:00
(21/21): ghc-unix-2.6.0.1-26.4.el7.x86_64.rpm | 160 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 9.0 MB/s | 5.1 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ghc-base-4.6.0.1-26.4.el7.x86_64 1/21
Installing : ghc-array-0.4.0.1-26.4.el7.x86_64 2/21
Installing : ghc-deepseq-1.3.0.1-26.4.el7.x86_64 3/21
Installing : ghc-bytestring-0.10.0.2-26.4.el7.x86_64 4/21
Installing : ghc-transformers-0.3.0.0-34.el7.x86_64 5/21
Installing : ghc-mtl-2.1.2-27.el7.x86_64 6/21
Installing : ghc-text-0.11.3.1-2.el7.x86_64 7/21
Installing : ghc-containers-0.5.0.0-26.4.el7.x86_64 8/21
Installing : ghc-old-locale-1.0.0.5-26.4.el7.x86_64 9/21
Installing : ghc-time-1.4.0.1-26.4.el7.x86_64 10/21
Installing : ghc-parsec-3.1.3-31.el7.x86_64 11/21
Installing : ghc-unix-2.6.0.1-26.4.el7.x86_64 12/21
Installing : ghc-regex-base-0.93.2-29.el7.x86_64 13/21
Installing : ghc-syb-0.4.0-35.el7.x86_64 14/21
Installing : ghc-pretty-1.1.1.0-26.4.el7.x86_64 15/21
Installing : ghc-filepath-1.3.0.1-26.4.el7.x86_64 16/21
Installing : ghc-directory-1.2.0.1-26.4.el7.x86_64 17/21
Installing : ghc-json-0.7-4.el7.x86_64 18/21
Installing : ghc-regex-tdfa-1.1.8-11.el7.x86_64 19/21
Installing : ghc-ShellCheck-0.3.8-1.el7.x86_64 20/21
Installing : ShellCheck-0.3.8-1.el7.x86_64 21/21
Verifying : ghc-transformers-0.3.0.0-34.el7.x86_64 1/21
Verifying : ghc-array-0.4.0.1-26.4.el7.x86_64 2/21
Verifying : ShellCheck-0.3.8-1.el7.x86_64 3/21
Verifying : ghc-ShellCheck-0.3.8-1.el7.x86_64 4/21
Verifying : ghc-regex-tdfa-1.1.8-11.el7.x86_64 5/21
Verifying : ghc-mtl-2.1.2-27.el7.x86_64 6/21
Verifying : ghc-syb-0.4.0-35.el7.x86_64 7/21
Verifying : ghc-bytestring-0.10.0.2-26.4.el7.x86_64 8/21
Verifying : ghc-pretty-1.1.1.0-26.4.el7.x86_64 9/21
Verifying : ghc-directory-1.2.0.1-26.4.el7.x86_64 10/21
Verifying : ghc-unix-2.6.0.1-26.4.el7.x86_64 11/21
Verifying : ghc-text-0.11.3.1-2.el7.x86_64 12/21
Verifying : ghc-deepseq-1.3.0.1-26.4.el7.x86_64 13/21
Verifying : ghc-old-locale-1.0.0.5-26.4.el7.x86_64 14/21
Verifying : ghc-containers-0.5.0.0-26.4.el7.x86_64 15/21
Verifying : ghc-base-4.6.0.1-26.4.el7.x86_64 16/21
Verifying : ghc-time-1.4.0.1-26.4.el7.x86_64 17/21
Verifying : ghc-parsec-3.1.3-31.el7.x86_64 18/21
Verifying : ghc-filepath-1.3.0.1-26.4.el7.x86_64 19/21
Verifying : ghc-json-0.7-4.el7.x86_64 20/21
Verifying : ghc-regex-base-0.93.2-29.el7.x86_64 21/21
Installed:
ShellCheck.x86_64 0:0.3.8-1.el7
Dependency Installed:
ghc-ShellCheck.x86_64 0:0.3.8-1.el7 ghc-array.x86_64 0:0.4.0.1-26.4.el7 ghc-base.x86_64 0:4.6.0.1-26.4.el7 ghc-bytestring.x86_64 0:0.10.0.2-26.4.el7 ghc-containers.x86_64 0:0.5.0.0-26.4.el7 ghc-deepseq.x86_64 0:1.3.0.1-26.4.el7
ghc-directory.x86_64 0:1.2.0.1-26.4.el7 ghc-filepath.x86_64 0:1.3.0.1-26.4.el7 ghc-json.x86_64 0:0.7-4.el7 ghc-mtl.x86_64 0:2.1.2-27.el7 ghc-old-locale.x86_64 0:1.0.0.5-26.4.el7 ghc-parsec.x86_64 0:3.1.3-31.el7
ghc-pretty.x86_64 0:1.1.1.0-26.4.el7 ghc-regex-base.x86_64 0:0.93.2-29.el7 ghc-regex-tdfa.x86_64 0:1.1.8-11.el7 ghc-syb.x86_64 0:0.4.0-35.el7 ghc-text.x86_64 0:0.11.3.1-2.el7 ghc-time.x86_64 0:1.4.0.1-26.4.el7
ghc-transformers.x86_64 0:0.3.0.0-34.el7 ghc-unix.x86_64 0:2.6.0.1-26.4.el7
Complete!
2. 使用 shellcheck + shell 脚本
[root@mw7qh /bin]# shellcheck clear.sh
In clear.sh line 1:
> nohup.out
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.
^-- SC2086: Double quote to prevent globbing and word splitting.