Shell Scripte überprüfen mit ShellCheck auch auf dem Raspberry Pi (Zero W) in 5 Minuten möglich und auch JUnit via xslt

Mit shellcheck kann man gut Shell Scripte überprüfen.Das kann man einfach online, über diese GUI www.shellcheck.net ausprobieren oder aber auch installieren.

Für den Raspberry Pi gibt es schon ein installations Packet, deshalb ist die Installation mit

sudo apt install shellcheck
# Testen welche Version installiert wurde#
# version: 0.4.4

schnell erledigt.

Das ist nun nicht gerade die aktuelle Version 0.5.0 aber immerhin. Ein selbst compilieren kommt für den Pi nicht infrage, da ShellCheck in Haskell programmiert ist und da für den compile mind. 2GB RAM benötigt werden, der Pi hat aber je nach Version max. 1GB.

Nun kann leicht eine Script Datei überprüft werden, mit Aufruf

shellcheck SCRIPT.sh. Hier mal zwei Beispiele:

Shell Scripte überprüfen mit ShellCheck auch auf dem Raspberry Pi (Zero W) in 5 Minuten möglich und auch JUnit via xslt

Shell Scripte überprüfen mit ShellCheck auch auf dem Raspberry Pi (Zero W) in 5 Minuten möglich und auch JUnit via xslt

Ok, komme nun auch in die user-content-gallery-of-bad-code 😉 Hier sind übrigens alle Rules mit Beispielen rechts in der Navi-Leiste zu finden.

JUnit

ShellCheck hat keinen JUnit XML Formatter. Aber man kann im Checkstyle XML Format die Meldungen ausgeben. Z.B.

shellcheck -f checkstyle script.sh

<?xml version='1.0' encoding='UTF-8'?>
<checkstyle version='4.3'>
<file name='dump1090.sh' >
<error line='26' column='11' severity='warning' message='Use cd ... &#124;&#124; exit in case cd fails.' source='ShellCheck.SC2164' />
<error line='27' column='19' severity='info' message='Double quote to prevent globbing and word splitting.' source='ShellCheck.SC2086' />
<error line='27' column='30' severity='error' message='The order of the 2&#62;&#38;1 and the redirect matters. The 2&#62;&#38;1 has to be last.' source='ShellCheck.SC2069' />
</file>
</checkstyle>

Diese Ausgabe kann nun mit dem vorhandenen checkstyle2junit.xslt transformiert werden. Dazu muss auch noch xmlstarlet installiert werden:

sudo apt-get install xmlstarlet

Nun können wir den XML Output über das XSLT convertieren um JUnit XML zu erhalten:


shellcheck -f checkstyle script.sh | xmlstarlet tr checkstyle2junit.xslt

Und wir erhalten die Testsuite mit Testcase …

<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="1" failures="3">
<testcase classname="dump1090.sh" name="dump1090.sh">
<failure type="ShellCheck.SC2164">Line 26: Use cd ... || exit in case cd fails.</failure>
<failure type="ShellCheck.SC2086">Line 27: Double quote to prevent globbing and word splitting.</failure>
<failure type="ShellCheck.SC2069">Line 27: The order of the 2>&amp;1 and the redirect matters. The 2>&amp;1 has to be last.</failure>
</testcase>
</testsuite>

Hier die Datei in der JUnit View:

Shell Scripte überprüfen mit ShellCheck auch auf dem Raspberry Pi (Zero W) in 5 Minuten möglich und auch JUnit via xslt

JSON

Die Ausgabe im JSON Format geht natürlich auch. Z.b. mit

shellcheck -f json script.sh

[{"file":"dump1090.sh","line":26,"column":11,"level":"warning","code":2164,"message":"Use cd ... || exit in case cd fails."},{"file":"dump1090.sh","line":27,"column":19,"level":"info","code":2086,"message":"Double quote to prevent globbing and word splitting."},{"file":"dump1090.sh","line":27,"column":30,"level":"error","code":2069,"message":"The order of the 2>&1 and the redirect matters. The 2>&1 has to be last."}]

oder besser formatiert:

[
	{
		"file": "dump1090.sh",
		"line": 26,
		"column": 11,
		"level": "warning",
		"code": 2164,
		"message": "Use cd ... || exit in case cd fails."
	},
	{
		"file": "dump1090.sh",
		"line": 27,
		"column": 19,
		"level": "info",
		"code": 2086,
		"message": "Double quote to prevent globbing and word splitting."
	},
	{
		"file": "dump1090.sh",
		"line": 27,
		"column": 30,
		"level": "error",
		"code": 2069,
		"message": "The order of the 2>&1 and the redirect matters. The 2>&1 has to be last."
	}
]

GCC

Auch das GCC Format ist möglich:

shellcheck -f gcc script.sh

dump1090.sh:26:11: warning: Use cd ... || exit in case cd fails. [SC2164]
dump1090.sh:27:19: note: Double quote to prevent globbing and word splitting. [SC2086]
dump1090.sh:27:30: error: The order of the 2>&1 and the redirect matters. The 2>&1 has to be last. [SC2069]

Ähnliche Artikel:

  1. Raspberry Pi: COBOL in 5 Minuten installieren oder „Back to the Future“
  2. Kaum ist man im Urlaub, erscheint nicht der Raspberry Pi 4 oder 5 sondern das GA Release von JUnit 5
  3. Neue Annotationen الشروح in JUnit 5 (= JUnit Platform + JUnit Jupiter + JUnit Vintage) für Java 8

wallpaper-1019588
UNIQLO – Neue Kollektion zu Final Fantasy vorgestellt
wallpaper-1019588
Pokémon GO: Neue Funktionen vorgestellt
wallpaper-1019588
Dragon Ball Xenoverse 2 – Neue Charaktere für den „Future Saga“-DLC angekündigt
wallpaper-1019588
One Piece Odyssey erscheint für die Nintendo Switch