Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Tool That Shows Warnings and Suggestions for Shell Scripts: ShellCheck  (Read 1242 times)

0 Members and 1 Guest are viewing this topic.

sibij

  • Guest
ShellCheck – A Tool That Shows Warnings and Suggestions for Shell Scripts

ShellCheck is a static analysis tool that shows warnings and suggestions concerning bad code in bash/sh shell scripts. It can be used in several ways: from the web by pasting your shell script in an online editor (Ace – a standalone code editor written in JavaScript) in https://www.shellcheck.net (it is always synchronized to the latest git commit, and is the simplest way to give ShellCheck a go) for instant feedback.

Alternatively, you can install it on your machine and run it from the terminal, integrate it with your text editor as well as in your build or test suites.

There are three things ShellCheck does primarily:

    It points out and explains typical beginner’s syntax issues that cause a shell to give cryptic error messages.
    It points out and explains typical intermediate level semantic problems that cause a shell to behave strangely and counter-intuitively.
    It also points out subtle caveats, corner cases and, pitfalls that may cause an advanced user’s otherwise working script to fail under future circumstances.

In this article, we will show how to install and use ShellCheck in the various ways to find bugs or bad code in your shell scripts in Linux.

How to Install and Use ShellCheck in Linux

On Debian/Ubuntu
Code: [Select]
apt-get install shellcheck
On RHEL/CentOS

Code: [Select]
yum -y install epel-release
Code: [Select]
yum install ShellCheck
On Fedora

Code: [Select]
dnf install ShellCheck
Using ShellCheck From the Web

Go to https://www.shellcheck.net and paste your script in the Ace editor provided, you will view the output at the bottom of the editor

Using ShellCheck From the Terminal

You can also run ShellCheck from the command-line,

Code: [Select]
shellcheck test.sh

Using ShellCheck From the Text Editor

You can also view ShellCheck suggestions and warnings directly in a variety of editors, this is probably a more efficient way of using ShellCheck, once you save a files, it shows you any errors in the code.

In Vim, use ALE or Syntastic

Start by installing Pathogen so that it’s easy to install syntastic. Run the commands below to get the pathogen.vim file and the directories it needs:

Code: [Select]
# mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Then add this to your ~/.vimrc file:

Code: [Select]
execute pathogen#infect()
Once you have installed pathogen, and you now can put syntastic into ~/.vim/bundle as follows:
Code: [Select]
cd ~/.vim/bundle && git clone --depth=1 https://github.com/vim-syntastic/syntastic.git
Next, close vim and start it back up to reload it, then type the command below:

Code: [Select]
:Helptags