📜 ⬆️ ⬇️

ShellCheck Static Analyzer and Script Improvement for Linux and Unix



Writing shell scripts is an exciting experience. Command line scripts help automate daily activities. You can create something beautiful (or some kind of crap), however, if you really need to write something, it would be good to know that the code is obtained exactly as the programmer needs it. A poorly written script can be dangerous. Most newbies write scripts by copying code snippets from StackOverflow, finding what they need, on Google, or using websites with questions and answers on Linux. This approach to programming translates into poor-quality code and the appearance of errors. For example, the rm command, the execution of which will lead to a catastrophe, since the VAR variable is not defined:

 rm -rf "/$VAR/*" 

Many of the problems of scripts can be solved with a linter, such as the ShellCheck static code analyzer, which is written in Haskell. It helps to look for errors in the texts of scripts and display reports on the checks performed. This allows for improved performance and code quality. Today we will talk about how to install and use ShellCheck in Linux and Unix-like operating systems.

Installation


The easiest way to install ShellCheck locally is to use the package manager used in your distribution like apt / apt-get / yum and others.
')

â–Ť Install ShellCheck on Debian / Ubuntu Linux


Here you need the following apt / apt-get command:

 $ sudo apt install shellcheck 

Here is an example of the system response to this command:

 [sudo] password for vivek: Reading package lists... Done Building dependency tree      Reading state information... Done The following NEW packages will be installed: shellcheck 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 1,841 kB of archives. After this operation, 15.5 MB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 shellcheck amd64 0.4.6-1 [1,841 kB] Fetched 1,841 kB in 42s (43.4 kB/s) Selecting previously unselected package shellcheck. (Reading database ... 196100 files and directories currently installed.) Preparing to unpack .../shellcheck_0.4.6-1_amd64.deb ... Unpacking shellcheck (0.4.6-1) ... Setting up shellcheck (0.4.6-1) ... Processing triggers for man-db (2.7.6.1-2) ... 

â–Ť Installing ShellCheck on CentOS / RHEL / Fedora / Oracle Linux


First you need to enable the EPEL repository in CentOS / RHEL:

 $ sudo yum -y install epel-release 

Next, enter the following yum command:

 $ sudo yum install ShellCheck 

This is what will be output in response to this command:

 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: centos.excellmedia.net * epel: mirror.nes.co.id * extras: mirrors.vonline.vn * updates: centos-hcm.viettelidc.com.vn Resolving Dependencies --> Running transaction check ---> Package ShellCheck.x86_64 0:0.3.5-1.el7 will be installed --> Processing Dependency: ghc(ShellCheck-0.3.5-297097a7f5fa37100847be7f096be51e) for package: ShellCheck-0.3.5-1.el7.x86_64 ..... .. ... Dependencies Resolved =============================================================================== Package                Arch         Version                  Repository  Size =============================================================================== Installing: ShellCheck             x86_64       0.3.5-1.el7              epel       495 k Installing for dependencies: ghc-ShellCheck         x86_64       0.3.5-1.el7              epel       540 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-compat       x86_64       0.95.1-35.el7            epel        15 k ghc-regex-posix        x86_64       0.95.2-30.el7            epel        47 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 (+21 Dependent packages) Total download size: 4.6 M Installed size: 28 M Is this ok [y/d/N]: y Downloading packages: (1/22): ghc-bytestring-0.10.0.2-26.4.el7.x86_64.rpm       | 182 kB   00:09    (2/22): ghc-array-0.4.0.1-26.4.el7.x86_64.rpm             | 113 kB   00:09    .... .. ... 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-compat.x86_64 0:0.95.1-35.el7                                      ghc-regex-posix.x86_64 0:0.95.2-30.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! 

If you are using Fedora, run the following dnf command:

 $ sudo dnf install ShellCheck 

â–ŤInstall ShellCheck in Arch Linux


Enter the following pacman command:

 $ sudo pacman -S shellcheck 

â–Ť Install ShellCheck on Gentoo Linux


Enter the following emerge command:

 $ sudo emerge --ask shellcheck 

â–Ť Install ShellCheck on openSUSE Linux


Enter the following zypper command:

 $ sudo zypper in ShellCheck 

â–Ť Install ShellCheck on macOS Unix


Use the following port command if you are working with MacPorts:

 $ port install shellcheck 

If you are using Homebrew on macOS / OS X, enter the following brew command:

 $ brew install shellcheck 

How to use ShellCheck


Test ShellCheck on a script, the contents of which we will scan using the cat command :

 $ cat -n backupme 

Here is his code:

 #!/bin/bash t="/tmp/exclude.$$" source ~/.backup.conf >$t for w in $WHATNOT do   echo $w >> $t done rsync $OPT -avr --exclude-from=$t  $WHAT $SERVER:$WHERE rm -rf $t 

Now check the script using ShellCheck:

 $ shellcheck backupme 

In response, the program will display the following:


ShellCheck in action

The ShellCheck utility offered corrections regarding the use of variables not enclosed in quotes, and also reported other issues. Correct the errors and view the script text again with the following command:

 $ cat -n backupme 

Here's what happened:

 #!/bin/bash t="/tmp/exclude.$$" source ~/.backup.conf touch $t for w in $WHATNOT do   echo "$w" >> $t done rsync "$OPT" -avr --exclude-from=$t  "$WHAT" "$SERVER:$WHERE" rm -rf "$t" 

Integration of ShellCheck into a text editor


ShellCheck can be integrated into vim or emacs, as a result, it will check the script texts directly in the editor. It shows the use of the plugin for vim neomake , an asynchronous tool for linting and building programs. It was installed using the vim-plug plugin manager in ~/.vimrc :

 call plug#begin('~/.vim/plugged') Plug 'pearofducks/ansible-vim' " install and use neomake linting Plug 'neomake/neomake' call plug#end() 

To install ansible-vim and neomake/neomake , enter the following command in vim:

 :PlugInstall 

To use the plugin, enter the following command when editing the bash / sh script:

 :Neomake 

Here are the results of the plug-in in the editor:


Neomake displays warnings and error messages using ShellCheck

Results


We believe ShellCheck is a great tool that allows you to improve and fix Linux command line scripts. It is able to detect many common flaws and errors in their code. If you want to learn more about SpellCheck, here’s the project ’s site , and here’s its repository on GitHub.

Dear readers! Do you test your scripts with something like ShellCheck?

Source: https://habr.com/ru/post/342580/


All Articles