A tool to improve the quality of your Shell script writing, it is recommended that everyone should have it!
Author: MC Monkey
In Linux, Linux Shell is indeed a powerful tool and a solution for common automation tasks. It can be used to automatically execute a series of commands, process text, perform logical judgments, and so on.
However, beginners will always encounter various errors when writing Shell scripts, which will greatly undermine their confidence in learning. Today, I will share with you a script artifact: ShellCheck!
A Linux Shell tool that solves your troubles of not being able to write scripts !
Introduction to ShellCheck
ShellCheck is a powerful tool that can help solve the troubles in Linux Shell script writing. It is a static code analysis tool that specifically checks the Shell script language.
-
Github address: https://github.com/koalaman/shellcheck
Features
-
Error Detection and Correction : ShellCheck can find and correct common problems, potential errors, and inconsistencies with best practices in scripts. This includes syntax errors, incorrect variable usage, incorrect command invocation, input/output errors, etc.
-
Coding Style : ShellCheck can also give suggestions on coding style to help improve the readability and consistency of scripts.
-
Best Practices : ShellCheck not only provides error detection, but also gives best practice suggestions, such as avoiding the use of certain deprecated commands or structures and recommending the use of safer and more maintainable writing methods.
-
Cross-platform support : ShellCheck supports running on multiple operating systems such as Linux, macOS, Windows, and supports various common shell interpreters such as Bash, Dash, Zsh, etc.
-
Custom Rules : ShellCheck allows users to define custom rules and filters to suit the needs of a specific project or organization.
-
Integration into editors : ShellCheck can be integrated into various text editors and IDEs, such as Vim, Emacs, Sublime, Atom, and VSCode, for real-time feedback and quick fixes.
ShellCheck Installation
The installation method of ShellCheck depends on the Linux distribution you are using. Here are the installation steps for several common distributions:
RHEL, CentOS, and Fedora systems
First you need to enable EPEL Repo before you can install ShellCheck using yum. The specific steps are as follows:
yum -y install epel-release #启用EPEL Repo。
yum install ShellCheck -y #安装ShellCheck。
Debian and Ubuntu systems
You can install it directly using the apt command:
sudo apt install shellcheck #安装ShellCheck。
Online use
https://www.shellcheck.net
You can paste your script
directly on the web page
and run the test.
Use Operation
Once installed, you can use ShellCheck to check shell scripts in the command line. Just add the name of the shell script after the shellcheck command, for example:
shellcheck test.sh
ShellCheck displays warnings and suggestions to help you improve the quality and security of your scripts.
Practical examples
Let's take a concrete example to demonstrate how to use ShellCheck.
Suppose we have a simple Shell script example.sh with the following content:
#!/bin/bash
echo "Hello, $1"
if [ $1 = "World" ]; then
echo "You said hello to World!"
fi
This script takes one argument and prints "Hello, " plus the argument. If the argument is "World", an additional message is printed.
Now, we want to use ShellCheck to check this script for problems.
Open a terminal, change to the directory containing the example.sh script, and run ShellCheck:
shellcheck example.sh
ShellCheck will output analysis results, which may include warnings and suggestions similar to the following:
In example.sh line 4:
if [ $1 = "World" ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.
For example:
if [ "$1" = "World" ]; then
This warning points out that in the if statement, the variable $1 is not surrounded by double quotes, which may cause some problems. According to ShellCheck's suggestion, we should modify the script as follows:
#!/bin/bash
echo "Hello, $1"
if [ "$1" = "World" ]; then
echo "You said hello to World!"
fi
Now the script is more robust because it avoids potential problems that could result from unquoted variables.
other
An example of using it integrated into the editor (using VSCode as an example). If you are using the VSCode editor and want to integrate ShellCheck into it for real-time feedback, you can follow these steps:
-
Open the Extensions view in VS Code (by clicking the square icon on the left sidebar or pressing Ctrl+Shift+X).
-
Enter "shellcheck" in the search box and find the corresponding extension, such as "ShellCheck for Visual Studio Code".
-
Click the "Install" button to install the plugin.
-
After the installation is complete, reload VSCode or restart the editor.
-
Open your shell script file. VSCode should now use ShellCheck in the background to analyze your script and display warnings or errors if it finds problems.
These warnings and errors are usually marked with a squiggly line or icon in the left sidebar of the code editor, and you can click them to see more details or suggested fixes.
In addition, if you are using an integrated development environment (IDE) such as PyCharm, you can also install the ShellCheck plug-in in PyCharm. In this way, you can check the Shell script directly in the IDE and enjoy a more convenient coding experience.
Summarize
In general, ShellCheck is a powerful shell script development auxiliary tool, which can help you write, maintain and debug shell scripts more effectively, and improve your work efficiency and script quality.
The spring recruitment has begun. If you are not adequately prepared, it will be difficult to find a good job during the spring recruitment.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!