diff options
author | Ting-Wei Lan <lantw44@gmail.com> | 2019-02-28 20:27:39 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2019-02-28 20:42:37 +0800 |
commit | f6d5ca1dc86fbd387e898a47f75d945ae79227c5 (patch) | |
tree | 4dc14cd037a634d4733c662b6825c92b73438c5e | |
parent | 2e6149f8f75b65fc54505b8eee1c58b9f5f9f007 (diff) | |
download | wspkg-f6d5ca1dc86fbd387e898a47f75d945ae79227c5.tar.gz wspkg-f6d5ca1dc86fbd387e898a47f75d945ae79227c5.tar.zst wspkg-f6d5ca1dc86fbd387e898a47f75d945ae79227c5.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 | arch/arch.deps.sh | 12 | ||||
-rwxr-xr-x | debian/debian.deps.sh | 4 | ||||
-rwxr-xr-x | freebsd/freebsd.deps.sh | 2 | ||||
-rwxr-xr-x | freebsd/freebsd.install.sh | 16 | ||||
-rwxr-xr-x | freebsd/freebsd.nopkg-setup.sh | 4 | ||||
-rwxr-xr-x | freebsd/freebsd.ports.sh | 2 | ||||
-rwxr-xr-x | wspkg-sh/packages-cpp.sh | 4 |
7 files changed, 24 insertions, 20 deletions
diff --git a/arch/arch.deps.sh b/arch/arch.deps.sh index f6e6a81..f7162ae 100755 --- a/arch/arch.deps.sh +++ b/arch/arch.deps.sh @@ -1,7 +1,11 @@ #!/bin/sh msg () { - echo "$@" 1>&2 + printf '%s\n' "$1" 1>&2 +} + +quote () { + printf \''%s'\' "$(printf '%s' "$1" | sed s,\',\'\"\'\"\',g)" } list_file="$1" @@ -27,7 +31,7 @@ pkg_groups_add () { # pkg_groups_find <group> pkg_groups_find () { - echo "${pkg_groups_map}" | grep "/$1/" | cut -f 1 -d ' ' + printf '%s\n' "${pkg_groups_map}" | grep "/$1/" | cut -f 1 -d ' ' } # Build the list of groups @@ -99,9 +103,9 @@ for pkg_or_group in `cat "${list_file}"`; do fi for pkg in ${pkgs}; do if [ "${loop_first}" = "1" ]; then - printf "'%s'" "${pkg}" + printf '%s' "$(quote "${pkg}")" else - printf " '%s'" "${pkg}" + printf ' %s' "$(quote "${pkg}")" fi loop_first=0 done diff --git a/debian/debian.deps.sh b/debian/debian.deps.sh index 797b437..4ad0088 100755 --- a/debian/debian.deps.sh +++ b/debian/debian.deps.sh @@ -6,9 +6,9 @@ list_file="$1" loop_first=1 for pkg in `cat "${list_file}"`; do if [ "${loop_first}" = "1" ]; then - printf "%s" "${pkg}" + printf '%s' "${pkg}" else - printf ", %s" "${pkg}" + printf ', %s' "${pkg}" fi loop_first=0 done diff --git a/freebsd/freebsd.deps.sh b/freebsd/freebsd.deps.sh index 91b1329..d21dc1e 100755 --- a/freebsd/freebsd.deps.sh +++ b/freebsd/freebsd.deps.sh @@ -9,7 +9,7 @@ while read -r pkg_name pkg_ports pkg_flavor 0<&4; do if [ "${pkg_flavor}" != "@" ]; then pkg_ports="${pkg_ports}@${pkg_flavor}" fi - printf " %s>=a:%s ^%%" "${pkg_name}" "${pkg_ports}" + printf ' %s>=a:%s ^%%' "${pkg_name}" "${pkg_ports}" done exec 4<&- diff --git a/freebsd/freebsd.install.sh b/freebsd/freebsd.install.sh index 9f24cf3..62d8b16 100755 --- a/freebsd/freebsd.install.sh +++ b/freebsd/freebsd.install.sh @@ -1,22 +1,22 @@ #!/bin/sh msg_and_copy () { - echo "==> Copying $1 to $2" 1>&2 + printf '==> Copying %s to %s\n' "$1" "$2" 1>&2 cp -r "$1" "$2" } msg_and_mkdir () { - echo "==> Creating directory $1" 1>&2 + printf '==> Creating directory %s\n' "$1" 1>&2 mkdir -p "$1" } if [ -z "$4" ]; then - echo "Usage: $0 category cat_makefile cat_makefile_inc dir" - echo "Example: "'`'"$0 local local.makefile local.makefile.inc 217' will do" - echo " mkdir -p /usr/ports/local" - echo " cp -r local.makefile /usr/ports/local/Makefile" - echo " cp -r local.makefile.inc /usr/ports/local/Makefile.inc" - echo " cp -r 217 /usr/ports/local" + printf 'Usage: %s category cat_makefile cat_makefile_inc dir\n' "$0" + printf 'Example: %s local local.makefile local.makefile.inc 217 will do\n' "$0" + echo ' mkdir -p /usr/ports/local' + echo ' cp -r local.makefile /usr/ports/local/Makefile' + echo ' cp -r local.makefile.inc /usr/ports/local/Makefile.inc' + echo ' cp -r 217 /usr/ports/local' exit 1 fi diff --git a/freebsd/freebsd.nopkg-setup.sh b/freebsd/freebsd.nopkg-setup.sh index 788eeae..8601c25 100755 --- a/freebsd/freebsd.nopkg-setup.sh +++ b/freebsd/freebsd.nopkg-setup.sh @@ -11,8 +11,8 @@ tr '/' '_' < "${ports_file}" | paste "${ports_file}" - | ( pkg_ports_var="${pkg_ports_var}_${pkg_flavor}" pkg_ports="${pkg_ports} FLAVOR=${pkg_flavor}" fi - printf 'WSPKG_PACKAGE_NAME_%-40s != printf "\\\\r===> Generating package names (%5d/%5d)" 1>\\&2; $(MAKE) -C ${PORTSDIR}/%-48s -V PKGNAMEPREFIX -V PORTNAME -V PKGNAMESUFFIX \\| tr -d "\\\\n"%%' \ + printf 'WSPKG_PACKAGE_NAME_%-40s != printf '\''\\\\r===> Generating package names (%5d/%5d)'\'' 1>\\&2; $(MAKE) -C ${PORTSDIR}/%-48s -V PKGNAMEPREFIX -V PORTNAME -V PKGNAMESUFFIX \\| tr -d '\''\\\\n'\''%%' \ "${pkg_ports_var}" "${index}" "${count}" "${pkg_ports}" index=$(( ${index} + 1 )) done ) -printf 'WSPKG_PACKAGE_NAME_%-25s != echo 1>\\&2; echo %%' "NULL" +printf 'WSPKG_PACKAGE_NAME_%-40s != echo 1>\\&2; echo %%' "NULL" diff --git a/freebsd/freebsd.ports.sh b/freebsd/freebsd.ports.sh index 1fa4b5d..a35fb5e 100755 --- a/freebsd/freebsd.ports.sh +++ b/freebsd/freebsd.ports.sh @@ -1,7 +1,7 @@ #!/bin/sh msg () { - echo "$@" 1>&2 + printf '%s\n' "$1" 1>&2 } [ -z "$1" ] && msg "Usage: $0 list_file" && exit 1 diff --git a/wspkg-sh/packages-cpp.sh b/wspkg-sh/packages-cpp.sh index 8e517c1..ba251ac 100755 --- a/wspkg-sh/packages-cpp.sh +++ b/wspkg-sh/packages-cpp.sh @@ -1,10 +1,10 @@ #!/bin/sh toupper () { - echo "$1" | sed 's/^-//' | tr '[:lower:]' '[:upper:]' + printf '%s\n' "$1" | sed 's/^-//' | tr '[:lower:]' '[:upper:]' } -[ -z "$1" ] && echo "Usage: $0 input_file cpp_args" && exit 1 +[ -z "$1" ] && printf 'Usage: %s input_file cpp_args\n' "$0" && exit 1 [ -z "$2" ] && default="yes" input_file="$1" |