fix: avoid echo usage

Echo can interpret operand as an option and checking every variable to
be echoed is troublesome while with printf, if the format specifier is
present before the operand, printing as string can be enforced.
This commit is contained in:
Ben Grande 2024-08-06 18:15:24 +02:00
parent 1b2f1ba941
commit bdd4c789c1
No known key found for this signature in database
GPG key ID: 00C64E14F51F9E56
52 changed files with 318 additions and 270 deletions

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -15,7 +16,7 @@ unset repo_toplevel
for tool in "${@}"; do
if ./scripts/requires-program.sh "${tool}" >/dev/null 2>&1; then
echo "${tool}"
printf '%s\n' "${tool}"
break
fi
done

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -51,7 +52,7 @@ case "${find_tool}" in
files="$(find . -not -path './.github/*' -type f -name "*.md")"
extra_files="$(find .github -type f -name "*.md")"
;;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
if test -n "${extra_files}"; then

View file

@ -6,7 +6,8 @@
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -35,7 +36,7 @@ else
find)
files="$(find . -type f \( -name '*.asc' -o -name '*.gpg' \) | sort -d)"
;;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
fi
@ -46,14 +47,14 @@ fi
for key in ${files}; do
data="$(gpg --no-keyring --no-auto-check-trustdb --no-autostart \
--with-colons --show-keys "${key}")"
nr="$(echo "${data}" | grep -Ec -e '^(p|s)ub:')"
nr="$(printf '%s\n' "${data}" | grep -Ec -e '^(p|s)ub:')"
## Threshold in days.
threshold="${PGP_LINT_THRESHOLD:-30}"
tty_stderr=0
if test -t 2; then
tty_stderr=1
fi
echo "${data}" | awk -v fail="0" -v key="${key}" -v nr="${nr}" \
printf '%s\n' "${data}" | awk -v fail="0" -v key="${key}" -v nr="${nr}" \
-v threshold="${threshold}" -v now="${now}" -v color="${tty_stderr}" \
-F ':' '
BEGIN {

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -34,7 +35,7 @@ fi
case "${find_tool}" in
fd|fdfind) files="$(${find_tool} . -H -t f -e py)";;
find) files="$(find . -type f -name "*.py")";;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
exec pylint ${files}

View file

@ -6,7 +6,8 @@
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -29,21 +30,23 @@ group="$(./scripts/spec-get.sh dom0 group)"
projects="$(find salt/ -mindepth 1 -maxdepth 1 -type d | sort -d |
sed -e "s|^salt/\(\S\+\)| - rpm_spec/${group}-\1.spec|")"
for unwanted_project in ${unwanted}; do
projects="$(echo "${projects}" |
projects="$(printf '%s\n' "${projects}" |
sed -e "\@rpm_spec/${group}-${unwanted_project}.spec@d")"
done
if test "${1-}" = "print"; then
echo "${projects}"
printf '%s\n' "${projects}"
exit 0
fi
sed -e "/@SPEC@/d" -- "${template}" | tee -- "${target}" >/dev/null
echo "${projects}" | tee -a -- "${target}" >/dev/null
printf '%s\n' "${projects}" | tee -a -- "${target}" >/dev/null
if test "${1-}" = "test"; then
if ! cmp -s -- "${target}" "${intended_target}"; then
echo "${0##*/}: error: File ${intended_target} is not up to date" >&2
echo "${0##*/}: error: Update the builder file with: ${0##/*}" >&2
err_msg="${0##*/}: error: File ${intended_target} is not up to date"
printf '%s\n' "${err_msg}" >&2
err_msg="${0##*/}: error: Update the builder file with: ${0##/*}"
printf '%s\n' "${err_msg}" >&2
exit 1
fi
fi

View file

@ -6,7 +6,8 @@
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"

View file

@ -14,6 +14,6 @@ for pkg in "${@}"; do
done
if test -n "${requires_program}"; then
echo "Missing program(s): ${requires_program}" >&2
printf '%s\n' "Missing program(s): ${requires_program}" >&2
exit 1
fi

View file

@ -12,7 +12,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -31,7 +32,7 @@ case "${find_tool}" in
sls_files="$(find salt/ -maxdepth 2 -type f -name '*.sls')"
set -- ${conf_files} ${sls_files}
;;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
## 201 - Fix trailing whitespace:

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -47,7 +48,7 @@ case "${find_tool}" in
-o -name '*.j2' -o -name '*.tmpl' -o -name '*.tst' \) | sort -d)"
set -- ${conf_files} ${sls_files}
;;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
exec salt-lint ${conf} "${@}"

View file

@ -8,7 +8,8 @@ set -eu
# shellcheck disable=3028
hostname="$(hostname)"
test "${hostname}" = "dom0" || { echo "Must be run from dom0" >&2; exit 1; }
test "${hostname}" = "dom0" ||
{ printf '%s\n' "Must be run from dom0" >&2; exit 1; }
uid="$(id -u)"
test "${uid}" = "0" || exec sudo "${0}"

View file

@ -9,7 +9,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -81,10 +82,10 @@ case "${find_tool}" in
files="$(find scripts/ salt/ -not \( -path "*/zsh" -prune \) -type f \
-exec file {} \+ | awk -F ":" '/ shell script,/{ print $1 }')"
;;
*) echo "Unsupported find tool" >&2; exit 1;;
*) printf '%s\n' "Unsupported find tool" >&2; exit 1;;
esac
files="$(echo "${files}" | sort -u)"
files="$(printf '%s\n' "${files}" | sort -u)"
# shellcheck disable=SC2310
show_long_lines ${files} || exit_code=1

View file

@ -7,7 +7,7 @@
set -eu
usage(){
echo "Usage: ${0##*/} PROJECT [PROJECT ...]" >&2
printf '%s\n' "Usage: ${0##*/} PROJECT [PROJECT ...]" >&2
exit 1
}
@ -62,7 +62,8 @@ case "${1-}" in
*) ;;
esac
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"

View file

@ -7,16 +7,17 @@
set -eu
usage(){
echo "Usage: ${0##*/} PROJECT [PROJECT ...]"
printf '%s\n' "Usage: ${0##*/} PROJECT [PROJECT ...]"
}
## Escape multiline strings for sed.
escape_key(){
key_type="${1}"
if test "${key_type}" = "scriptlet"; then
echo "${2}" | sed -e ':a;N;$!ba;s/\n/\\n /g' | sed -e 's/\$/\\$/'
printf '%s\n' "${2}" | sed -e ':a;N;$!ba;s/\n/\\n /g' | \
sed -e 's/\$/\\$/'
elif test "${key_type}" = "text"; then
echo "${2}" | sed -e ':a;N;$!ba;s/\n/\\n/g' | sed -e 's/\$/\\$/'
printf '%s\n' "${2}" | sed -e ':a;N;$!ba;s/\n/\\n/g' | sed -e 's/\$/\\$/'
else
return 1
fi
@ -33,7 +34,7 @@ get_scriptlet(){
"/^<\!${scriptlet_begin}>$/,/^<\!${scriptlet_end}>$/p" \
-- "${readme}" | sed -e '/^```.*/d;/^\S*$/d;/^<\!-- pkg:/d;s/^sudo //')"
if test -z "${scriptlet}"; then
echo true
printf '%s\n' "true"
return 0
fi
escape_key scriptlet "${scriptlet}"
@ -44,14 +45,14 @@ get_spec(){
}
gen_spec(){
project="$(echo "${1}" | sed -e "s|salt/||;s|/.*||")"
if echo "${projects_seen}" | grep -qF -e " ${project} "; then
project="$(printf '%s\n' "${1}" | sed -e "s|salt/||;s|/.*||")"
if printf '%s\n' "${projects_seen}" | grep -qF -e " ${project} "; then
return
fi
projects_seen="${projects_seen} ${project} "
if echo "${unwanted}" | grep -q -e "^${project}$"; then
echo "warn: skipping spec generation of untracked formula: ${project}" >&2
if printf '%s\n' "${unwanted}" | grep -q -e "^${project}$"; then
printf '%s\n' "warn: skipping spec of untracked formula: ${project}" >&2
return 0
fi
@ -74,7 +75,7 @@ gen_spec(){
version="$(get_spec version)"
license_csv="$(get_spec license_csv)"
## Ideally we would query the license, but it is a heavy call.
license="$(echo "${license_csv}" | sed -e "s/\,/ AND /g")"
license="$(printf '%s\n' "${license_csv}" | sed -e "s/\,/ AND /g")"
vendor="$(get_spec vendor)"
packager="$(get_spec packager)"
url="$(get_spec url)"
@ -125,18 +126,19 @@ gen_spec(){
requires_key="${requires_key:-}Requires: ${group}-${r}\n"
done
sed -i -e "s/@REQUIRES@/${requires_key}/" -- "${target}" >/dev/null
echo "${changelog}" | tee -a -- "${target}" >/dev/null
printf '%s\n' "${changelog}" | tee -a -- "${target}" >/dev/null
if test "${2-}" = "test"; then
if ! cmp -s -- "${target}" "${intended_target}"; then
echo "error: ${intended_target} is not up to date" >&2
printf '%s\n' "error: ${intended_target} is not up to date" >&2
diff --color=auto -- "${intended_target}" "${target}" || true
fail=1
else
unstaged_target="$(git diff --name-only -- "${intended_target}")" ||
true
if test -n "${unstaged_target}"; then
echo "warn: ${intended_target} is up to date but it is not staged" >&2
err_msg="warn: ${intended_target} is up to date but it is not staged"
printf '%s\n' "${err_msg}" >&2
fi
fi
fi
@ -147,7 +149,8 @@ case "${1-}" in
*) ;;
esac
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -167,7 +170,8 @@ if test "${1-}" = "test"; then
shift
fi
if echo "${@}" | grep -qE -e "(^scripts/| scripts/|/template.spec)" ||
if printf '%s\n' "${@}" | \
grep -qE -e "(^scripts/| scripts/|/template.spec)" ||
test -z "${1-}"
then
# shellcheck disable=SC2046,SC2312

View file

@ -10,11 +10,11 @@ set -eu
usage(){
names="$(find salt/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
| sort -d | tr "\n" " ")"
keys_trimmed="$(echo "${keys}" | tr "\n" " ")"
echo "Usage: ${0##*/} <NAME> <KEY>"
echo "Example: ${0##*/} qubes-builder description"
echo "Names: ${names}"
echo "Keys: ${keys_trimmed}"
keys_trimmed="$(printf '%s\n' "${keys}" | tr "\n" " ")"
printf '%s\n' "Usage: ${0##*/} <NAME> <KEY>"
printf '%s\n' "Example: ${0##*/} qubes-builder description"
printf '%s\n' "Names: ${names}"
printf '%s\n' "Keys: ${keys_trimmed}"
}
block_max_chars(){
@ -22,8 +22,9 @@ block_max_chars(){
char_value="${2}"
less_than="${3}"
if test "${#char_value}" -ge "${less_than}"; then
echo "Error: ${char_key} is too long. Must be <${less_than} chars." >&2
echo "Key contents: ${char_value}" >&2
err_msg="Error: ${char_key} is too long. Must be <${less_than} chars."
printf '%s\n' "${err_msg}" >&2
printf '%s\n' "Key contents: ${char_value}" >&2
exit 1
fi
}
@ -60,11 +61,12 @@ case "${1-}" in
*) key="${1}"; shift;;
esac
if test -z "${key##* }"; then
echo "Key was not given" >&2
printf '%s\n' "Key was not given" >&2
exit 1
fi
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -87,7 +89,8 @@ bug_url="${SPEC_BUGURL:-"${url}/issues"}"
if test -z "${group}" || test -z "${vendor}" || test -z "${packager}" \
|| test -z "${url}" || test -z "${bug_url}"
then
echo "At least one empty var: group, vendor, packager, url, bug_url" >&2
err_msg="At least one empty var: group, vendor, packager, url, bug_url"
printf '%s\n' "${err_msg}" >&2
exit 1
fi
@ -96,7 +99,7 @@ project="${group}-${name}"
project_dir="salt/${name}"
if ! test -d "${project_dir}"; then
echo "Project doesn't exist: ${project_dir}" >&2
printf '%s\n' "Project doesn't exist: ${project_dir}" >&2
exit 1
fi
@ -104,7 +107,7 @@ fi
read -r version <"${project_dir}/version"
readme="${project_dir}/README.md"
if ! test -f "${readme}"; then
echo "Project ${name} does not have README.md" >&2
printf '%s\n' "Project ${name} does not have README.md" >&2
exit 1
fi
@ -112,7 +115,7 @@ if test "${key}" = "license" || test "${key}" = "license_csv"; then
license_csv="$(reuse --root "${project_dir}" lint |
awk -F ':' '/^\* Used licenses:/{print $2}' | tr " " "\n" | tr -d "," |
sort -d | tr -s "\n" "," | sed "s/^\,//;s/\,$//")"
license="$(echo "${license_csv}" | sed "s/\,/ AND /g")"
license="$(printf '%s\n' "${license_csv}" | sed "s/\,/ AND /g")"
fi
## The macro %autochangelog prints logs of all projects and we separate a
@ -167,25 +170,25 @@ if test "${key}" = "saltfiles" || test "${key}" = "requires"; then
fi
case "${key}" in
branch) echo "${branch}";;
changelog) echo "${changelog}";;
description) echo "${description}";;
file_roots) echo "${file_roots}";;
group) echo "${group}";;
license_csv) echo "${license_csv}";;
license) echo "${license}";;
name) echo "${name}";;
project) echo "${project}";;
project_dir) echo "${project_dir}";;
readme) echo "${readme}";;
requires) echo "${requires}";;
saltfiles) echo "${saltfiles}";;
summary) echo "${summary}";;
url) echo "${url}";;
bug_url) echo "${bug_url}";;
vendor) echo "${vendor}";;
packager) echo "${packager}";;
version) echo "${version}";;
branch) printf '%s\n' "${branch}";;
changelog) printf '%s\n' "${changelog}";;
description) printf '%s\n' "${description}";;
file_roots) printf '%s\n' "${file_roots}";;
group) printf '%s\n' "${group}";;
license_csv) printf '%s\n' "${license_csv}";;
license) printf '%s\n' "${license}";;
name) printf '%s\n' "${name}";;
project) printf '%s\n' "${project}";;
project_dir) printf '%s\n' "${project_dir}";;
readme) printf '%s\n' "${readme}";;
requires) printf '%s\n' "${requires}";;
saltfiles) printf '%s\n' "${saltfiles}";;
summary) printf '%s\n' "${summary}";;
url) printf '%s\n' "${url}";;
bug_url) printf '%s\n' "${bug_url}";;
vendor) printf '%s\n' "${vendor}";;
packager) printf '%s\n' "${packager}";;
version) printf '%s\n' "${version}";;
"") exit 1;;
*) echo "Unsupported key" >&2; exit 1;;
*) printf '%s\n' "Unsupported key" >&2; exit 1;;
esac

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"

View file

@ -8,7 +8,7 @@
set -eu
usage(){
echo "Usage: ${0##*/} <file> [file ...]"
printf '%s\n' "Usage: ${0##*/} <file> [file ...]"
exit 1
}
@ -21,18 +21,19 @@ esac
## update on save.
if ! vim -e -c 'setf markdown' -c 'if !exists(":GenTocGFM") | cq | endif' -c q
then
echo "Error: Vim Plugin mzlogin/vim-markdown-toc is not installed." >&2
err_msg="Error: Vim Plugin mzlogin/vim-markdown-toc isn't installed."
printf '%s\n' "${err_msg}" >&2
exit 1
fi
for f in "${@}"; do
if ! test -f "${f}"; then
echo "Error: Not a regular file: ${f}" >&2
printf '%s\n' "Error: Not a regular file: ${f}" >&2
exit 1
fi
if ! grep -q -e "^## Table of Contents$" -- "${f}"; then
echo "Could not find table of contents in file: ${f}, skipping" >&2
printf '%s\n' "Could not find TOC in file: ${f}, skipping" >&2
continue
fi
## This is fragile, the table of contents should have at least one block
@ -40,5 +41,5 @@ for f in "${@}"; do
## the rest of the file.
vim -c 'norm zRgg' -c '/^## Table of Contents$' -c 'norm jd}k' \
-c ':GenTocGFM' -c 'norm ddgg' -c wq -- "${f}"
echo "Updated TOC in file: ${f}"
printf '%s\n' "Updated TOC in file: ${f}"
done

View file

@ -8,7 +8,8 @@
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -22,7 +23,7 @@ if test -n "${1-}"; then
fi
fi
files="$(echo "${files}" | sort -u)"
files="$(printf '%s\n' "${files}" | sort -u)"
# shellcheck disable=SC2086
unicode_match="$(grep -oPrHn --exclude-dir=.git --exclude-dir=LICENSES \
-e "[^\x00-\x7F]" -- ${files} || true)"
@ -30,14 +31,14 @@ unicode_match="$(grep -oPrHn --exclude-dir=.git --exclude-dir=LICENSES \
match_found=""
if test -n "${unicode_match}"; then
for line in ${unicode_match}; do
line_file="$(echo "${line}" | cut -d ":" -f1)"
line_file="$(printf '%s\n' "${line}" | cut -d ":" -f1)"
case "${line_file}" in
git/*|LICENSES/*|.reuse/dep5|*.asc) continue;;
*) ;;
esac
line_number="$(echo "${line}" | cut -d ":" -f2)"
line_unicode="$(echo "${line}" | cut -d ":" -f3 | od -A n -vt c)"
echo "${line_file}:${line_number}:${line_unicode}"
line_number="$(printf '%s\n' "${line}" | cut -d ":" -f2)"
line_unicode="$(printf '%s\n' "${line}" | cut -d ":" -f3 | od -A n -vt c)"
printf '%s\n' "${line_file}:${line_number}:${line_unicode}"
match_found="1"
done
if test "${match_found}" = 1; then

View file

@ -7,7 +7,8 @@
# shellcheck disable=SC2086
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"

View file

@ -6,7 +6,8 @@
set -eu
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"