Home
» LINUX
»
How to debug Linux Bash scripts with ShellCheck
How to debug Linux Bash scripts with ShellCheck
Are you having trouble with a Bash script you wrote for your Linux PC? Instead of looking through countless threads on StackExchange for programming help, consider using the ShellCheck tool to debug Linux Bash scrips and automatically find mistakes in your code.
Get ShellCheck
ShellCheck is a useful tool, but it doesn’t come with your Linux operating system out of the box. Instead, if you’d like to use it, you’re going to have to install it first. In this tutorial, we’ll cover how to install ShellCheck from various official software sources. Also, we’ll also show you how to get the program working via the generic Linux binary.
Open up a terminal window and update your packages. Then, follow the instructions that correspond to your Linux operating system.
Ubuntu
ShellCheck is in the official Ubuntu software sources, so installing it is quite easy. In a terminal, use the Apt package manager and get the latest version of ShellCheck working.
sudo apt install shellcheck
Debian
Debian Stable has an older version of ShellCheck ready for users to install. Keep in mind that this version is held back due to how Debian does things.
To get ShellCheck on Arch Linux, you’ll first need to enable the “Community” software repository. Enabling “Community” requires editing the Pacman.conf file. To edit the file, open up Pacman.conf in Nano.
sudo nano /etc/pacman.conf
Scroll through (with the arrow keys) and find the “Community” section of the file. Remove all instances of the # symbol (about three lines). Then, save the edits by pressing the Ctrl + O button.
With the new edits to Pacman.conf taken care of, press Ctrl + X to close Nano. Then, resync Pacman to set up access to the Arch Linux “Community” repository.
sudo pacman -Syyuu
Finally, install ShellCheck to Arch Linux with:
sudo pacman -S shellcheck
Fedora
To get the ShellCheck script analysis tool on Fedora Linux, open up a terminal and use the DNF package management tool.
sudo dnf install ShellCheck
OpenSUSE
Installing ShellCheck on OpenSUSE requires no special steps, as it is in the distribution’s main software sources. To get it, enter the following Zypper package command in a terminal window.
sudo zypper install ShellCheck
Generic Linux
Users of lesser-known Linux distributions that want access to the ShellCheck script analysis tool must install the universal binary release. Luckily, this binary release is easy to install and requires no tinkering with source code. To get it working, follow the steps below!
Schritt 1: Exportieren Sie die Version von ShellCheck, die Sie herunterladen möchten, in Ihre Terminalumgebung.
export scversion= " stable " # oder "v0.4.7", oder "latest"
Schritt 2: Holen Sie sich mit dem wget- Download-Tool die neueste ShellCheck-Binärversion.
Der Hauptzweck von ShellCheck besteht darin, ein Skript (normalerweise Bash) zu überprüfen, missbrauchte Befehle zu erkennen und Korrekturen anzubieten. Gehen Sie für die grundlegende Verwendung wie folgt vor.
Zuerst CD , wo das Skript Sie überprüfen möchten , ist auf Ihrem Linux - PC. In diesem Tutorial befindet sich unser Skript in ~/shell-scripts . Ihre können davon abweichen.
cd ~/shell-scripts
Führen Sie den Befehl ls aus und zeigen Sie alle Dateien im aktuellen Verzeichnis an.
Hinweis: Wenn Sie ShellCheck in einem Verzeichnis mit mehreren Dateitypen verwenden möchten, empfiehlt es sich, grep und ls zu kombinieren . Wenn Sie diese beiden Befehle zusammen verwenden, können Sie alle Nicht-Skriptdateien herausfiltern.
ls
Oder kombinieren Sie mit grep , um nur Skriptdateien anzuzeigen.
ls | grep ".sh"
ls | grep ".bash"
Suchen Sie den Namen einer Skriptdatei in der ls- Eingabeaufforderung und markieren Sie den Dateinamen mit der Maus. Wenn Sie fertig sind, führen Sie es über ShellCheck aus und überprüfen Sie Ihren Code.
Shellcheck Dateiname.sh
oder
Shellcheck Dateiname.bash
Unmittelbar nach der Ausführung des Shellcheck- Befehls erkennt er Probleme mit Ihrem Code und fügt ihn in die Terminal-Eingabeaufforderung ein. Lesen Sie durch und erfahren Sie, wie Sie Ihre Programmierung verbessern können, indem Sie die darin enthaltenen Vorschläge befolgen.
ShellCheck-Bericht in einer Datei speichern
Einen sofortigen Bericht für Ihre Bash-Programmierung im Terminal zu erhalten, ist sehr nützlich, Sie können ihn jedoch verlieren, wenn Sie das Terminal schließen. Um den Verlust Ihres Shellcheck-Berichts zu umgehen, empfehlen wir, die Ausgabe in einer Textdatei zu speichern, indem Sie das Programm in Kombination mit der >>- Funktion im Terminal ausführen .