diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2019-02-28 20:42:44 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2019-02-28 21:33:06 +0800 |
commit | c0ba16c220900a5d8417018c99c4e70a9f3d4bd7 (patch) | |
tree | cfb994187c5f231899f3fb2d553ecfa86520f76c | |
parent | 78e774321d721c7be31b2d08082302761a9e4af7 (diff) | |
download | wspkg-data-c0ba16c220900a5d8417018c99c4e70a9f3d4bd7.tar.gz wspkg-data-c0ba16c220900a5d8417018c99c4e70a9f3d4bd7.tar.zst wspkg-data-c0ba16c220900a5d8417018c99c4e70a9f3d4bd7.zip |
Use 'printf' to print non-constant strings in shell scripts
It is unsafe to use 'echo' with strings with variable substitutions.
Different 'echo' implementations may accept different options, and they
can interpret strings with escape characters in different ways. The
behavior of 'printf' is much more consistent so it should be preferred.
-rwxr-xr-x | 217-mrtg.sh | 17 | ||||
-rwxr-xr-x | build.sh | 20 | ||||
-rwxr-xr-x | wsbsd.files/patches-apply.sh | 6 | ||||
-rwxr-xr-x | wsbsd.files/patches-update.sh | 8 |
4 files changed, 26 insertions, 25 deletions
diff --git a/217-mrtg.sh b/217-mrtg.sh index 1621f36..ce50701 100755 --- a/217-mrtg.sh +++ b/217-mrtg.sh @@ -1,22 +1,21 @@ #!/bin/sh write_string_to_fd () { - fd="$1" - shift - echo "$@" 1>&"$fd" + printf '%s\n' "$2" 1>&"$1" } write_stdin_to_fd () { - fd="$1" - shift - cat 1>&"$fd" + cat 1>&"$1" } echo_cmd () { - printf "\033[1;33mRunning command => \033[m" - echo "$@" + printf '\033[1;33mRunning command =>\033[m' + for arg in "$@"; do + printf ' %s' "${arg}" + done + printf '\n' "$@" - echo + printf '\n' } export WRKDIR="/tmp/wspkg-build" @@ -1,20 +1,20 @@ #!/bin/sh if [ -z "$WSPKGDIR" ]; then - printf "\033[1;31mWSPKGDIR is not defined so I cannot build package for you :(\033[m\n" + printf '\033[1;31mWSPKGDIR is not defined so I cannot build package for you :(\033[m\n' if [ -f "../wspkg/wspkg-mk/packages.mk" ]; then - echo "You may want to try WSPKGDIR=\"$(dirname "`pwd`")/wspkg\" $0" + printf 'You may want to try WSPKGDIR="%s/wspkg" %s\n' "$(dirname "$(pwd)")" "$0" elif [ -f "/usr/share/wspkg/wspkg-mk/packages.mk" ]; then - echo "You may want to try WSPKGDIR=/usr/share/wspkg $0" + printf 'You may want to try WSPKGDIR=/usr/share/wspkg %s\n' "$0" elif [ -f "/usr/pkg/share/wspkg/wspkg-mk/packages.mk" ]; then - echo "You may want to try WSPKGDIR=/usr/pkg/share/wspkg $0" + printf 'You may want to try WSPKGDIR=/usr/pkg/share/wspkg %s\n' "$0" elif [ -f "/usr/local/share/wspkg/wspkg-mk/packages.mk" ]; then - echo "You may want to try WSPKGDIR=/usr/local/share/wspkg $0" + printf 'You may want to try WSPKGDIR=/usr/local/share/wspkg %s\n' "$0" fi exit 1 fi -[ -z "$1" ] && echo "Usage: $0 makefile" && exit 1 +[ -z "$1" ] && printf 'Usage: %s makefile\n' "$0" && exit 1 if [ -d "$1" ]; then makefile="${1}.mk" else @@ -23,9 +23,11 @@ fi shift : ${MAKE:="make"} -printf "\033[1;33mRunning\033[m: %s -f \"%s\" WSPKGDIR=\"%s\" " "${MAKE}" "$makefile" "$WSPKGDIR" -echo "$@" -echo "" +printf '\033[1;33mRunning\033[m: %s -f "%s" WSPKGDIR="%s"' "${MAKE}" "$makefile" "$WSPKGDIR" +for arg in "$@"; do + printf ' %s' "$arg" +done +printf '\n\n' if ${MAKE} -f "$makefile" WSPKGDIR="$WSPKGDIR" "$@"; then printf '\n\033[1;32mDone! ;-)\033[m\n' else diff --git a/wsbsd.files/patches-apply.sh b/wsbsd.files/patches-apply.sh index 105fdaa..2f07999 100755 --- a/wsbsd.files/patches-apply.sh +++ b/wsbsd.files/patches-apply.sh @@ -2,7 +2,7 @@ : ${PORTSDIR:="/usr/ports"} [ '!' -d "${PORTSDIR}/.svn" ] && \ - echo "${PORTSDIR} is not a svn checkout" && exit 1 + printf '%s is not a svn checkout\n' "${PORTSDIR}" && exit 1 shdir="$(realpath "$(dirname "$0")")" : ${shdir:="."} @@ -12,9 +12,9 @@ patchdir="${shdir}/patches" cd ${PORTSDIR} for patch_file in "${patchdir}"/*; do if patch -s -C -f -p0 < "${patch_file}"; then - echo "==> Applying ${patch_file}" + printf '==> Applying %s\n' "${patch_file}" patch -s -N -p0 < "${patch_file}" else - echo "==> Skipping ${patch_file}" + printf '==> Skipping %s\n' "${patch_file}" fi done diff --git a/wsbsd.files/patches-update.sh b/wsbsd.files/patches-update.sh index 470f078..6292f48 100755 --- a/wsbsd.files/patches-update.sh +++ b/wsbsd.files/patches-update.sh @@ -2,7 +2,7 @@ : ${PORTSDIR:="/usr/ports"} [ '!' -d "${PORTSDIR}/.svn" ] && \ - echo "${PORTSDIR} is not a svn checkout" && exit 1 + printf '%s is not a svn checkout\n' "${PORTSDIR}" && exit 1 shdir="$(realpath "$(dirname "$0")")" : ${shdir:="."} @@ -10,12 +10,12 @@ shdir="$(realpath "$(dirname "$0")")" patchdir="${shdir}/patches" cd ${PORTSDIR} -echo "==> Running svn status ..." +echo '==> Running svn status ...' svn status | sed -e '/^?/d' -e 's#^[A-Z]* *\([^/]*\/[^/]*\)\(/.*\)*$#\1#' | \ sort | uniq | ( while read oneline; do - echo "==> Generating patch for ${oneline}" - patch_name="$(echo "${oneline}" | tr '/' '_')" + printf '==> Generating patch for %s\n' "${oneline}" + patch_name="$(printf '%s\n' "${oneline}" | tr '/' '_')" svn diff --patch-compatible "${oneline}" \ > "${patchdir}/${patch_name}.patch" done ) |