From 4a44794d076659cb244d67e2d1781da864585948 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Tue, 14 Aug 2012 10:39:50 -0600 Subject: [PATCH 1/3] Use sh instead of bash for pt-mysql-summary. Set +u around Bashism check. --- bin/pt-mysql-summary | 7 ++++--- bin/pt-summary | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 3759e31a..9557fc03 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # This program is part of Percona Toolkit: http://www.percona.com/software/ # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal @@ -608,7 +608,7 @@ shorten() { } group_concat () { - sed -e '{H; $!d;}' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' "${1}" + sed -e 'H; $!d;' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' "${1}" } # ########################################################################### @@ -2308,9 +2308,10 @@ main() { # Execute the program if it was not included from another file. # This makes it possible to include without executing, and thus test. +set +u if [ "${0##*/}" = "$TOOL" ] \ || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then - +set -u # Set up temporary dir. mk_tmpdir # Parse command line options. diff --git a/bin/pt-summary b/bin/pt-summary index 68662a0c..37dbdccd 100755 --- a/bin/pt-summary +++ b/bin/pt-summary @@ -2180,9 +2180,10 @@ sigtrap() { local PTFUNCNAME=sigtrap; # Execute the program if it was not included from another file. This makes it # possible to include without executing, and thus test. +set +u if [ "${0##*/}" = "$TOOL" ] \ || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then - +set -u # Set up temporary dir. mk_tmpdir # Parse command line options. From 9ddf90eeca63f24167754212f4c0db15df775968 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Tue, 14 Aug 2012 11:07:10 -0600 Subject: [PATCH 2/3] Use ${@:-""} rather than $@, prevents error on ksh when no opts given. Remove set +u to see if it is no longer needed. --- bin/pt-mysql-summary | 13 ++++++------- bin/pt-summary | 13 ++++++------- lib/bash/parse_options.sh | 6 +++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 9557fc03..27523f5b 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -193,7 +193,7 @@ parse_options() { _parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf" fi - _parse_command_line "$@" + _parse_command_line "${@:-""}" } _parse_pod() { @@ -285,7 +285,7 @@ _eval_po() { _parse_config_files() { - for config_file in "$@"; do + for config_file in "${@:-""}"; do test -f "$config_file" || continue while read config_opt; do @@ -319,7 +319,7 @@ _parse_command_line() { local required_arg="" local spec="" - for opt in "$@"; do + for opt in "${@:-""}"; do if [ "$opt" = "--" -o "$opt" = "----" ]; then HAVE_EXT_ARGV=1 continue @@ -2308,14 +2308,13 @@ main() { # Execute the program if it was not included from another file. # This makes it possible to include without executing, and thus test. -set +u if [ "${0##*/}" = "$TOOL" ] \ || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then -set -u + # Set up temporary dir. mk_tmpdir # Parse command line options. - parse_options "$0" "$@" + parse_options "$0" "${@:-""}" # Verify that --sleep, if present, is positive if [ -n "$OPT_SLEEP" ] && [ "$OPT_SLEEP" -lt 0 ]; then @@ -2336,7 +2335,7 @@ set -u # a connection then. [ "$OPT_READ_SAMPLES" ] || check_mysql - main "$@" + main "${@:-""}" fi # ############################################################################ diff --git a/bin/pt-summary b/bin/pt-summary index 37dbdccd..f60f8387 100755 --- a/bin/pt-summary +++ b/bin/pt-summary @@ -200,7 +200,7 @@ parse_options() { _parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf" fi - _parse_command_line "$@" + _parse_command_line "${@:-""}" } _parse_pod() { @@ -292,7 +292,7 @@ _eval_po() { _parse_config_files() { - for config_file in "$@"; do + for config_file in "${@:-""}"; do test -f "$config_file" || continue while read config_opt; do @@ -326,7 +326,7 @@ _parse_command_line() { local required_arg="" local spec="" - for opt in "$@"; do + for opt in "${@:-""}"; do if [ "$opt" = "--" -o "$opt" = "----" ]; then HAVE_EXT_ARGV=1 continue @@ -2180,14 +2180,13 @@ sigtrap() { local PTFUNCNAME=sigtrap; # Execute the program if it was not included from another file. This makes it # possible to include without executing, and thus test. -set +u if [ "${0##*/}" = "$TOOL" ] \ || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then -set -u + # Set up temporary dir. mk_tmpdir # Parse command line options. - parse_options "$0" "$@" + parse_options "$0" "${@:-""}" usage_or_errors "$0" po_status=$? rm_tmpdir @@ -2196,7 +2195,7 @@ set -u exit $po_status fi - main "$@" + main "${@:-""}" fi diff --git a/lib/bash/parse_options.sh b/lib/bash/parse_options.sh index 5ae56fc7..40cc144d 100644 --- a/lib/bash/parse_options.sh +++ b/lib/bash/parse_options.sh @@ -213,7 +213,7 @@ parse_options() { fi # Finally, parse the command line. - _parse_command_line "$@" + _parse_command_line "${@:-""}" } _parse_pod() { @@ -318,7 +318,7 @@ _eval_po() { _parse_config_files() { - for config_file in "$@"; do + for config_file in "${@:-""}"; do # Next config file if this one doesn't exist. test -f "$config_file" || continue @@ -377,7 +377,7 @@ _parse_command_line() { local required_arg="" local spec="" - for opt in "$@"; do + for opt in "${@:-""}"; do if [ "$opt" = "--" -o "$opt" = "----" ]; then HAVE_EXT_ARGV=1 continue From 3602bb252f78a0666307bf68ed45d782edaa8840 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Tue, 14 Aug 2012 11:46:47 -0600 Subject: [PATCH 3/3] Use non-Bashism indirect reference in parse_options. Change $@ to ${@:-} and update parse_options in all tools. --- bin/pt-ioprofile | 33 ++++++++++++++++++--------------- bin/pt-mysql-summary | 27 +++++++++++++++------------ bin/pt-sift | 2 +- bin/pt-stalk | 39 +++++++++++++++++++++------------------ bin/pt-summary | 27 +++++++++++++++------------ lib/bash/parse_options.sh | 31 +++++++++++++++++-------------- 6 files changed, 87 insertions(+), 72 deletions(-) diff --git a/bin/pt-ioprofile b/bin/pt-ioprofile index 28463753..24f718e7 100755 --- a/bin/pt-ioprofile +++ b/bin/pt-ioprofile @@ -119,19 +119,22 @@ usage_or_errors() { echo echo "Options and values after processing arguments:" echo - for opt in $(ls "$PO_DIR"); do - local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" - local varvalue="${!varname}" - if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then - if [ "$varvalue" -a "$varvalue" = "yes" ]; - then varvalue="TRUE" - else - varvalue="FALSE" + ( + cd "$PO_DIR" + for opt in *; do + local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" + eval local varvalue=\$$varname + if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then + if [ "$varvalue" -a "$varvalue" = "yes" ]; + then varvalue="TRUE" + else + varvalue="FALSE" + fi fi - fi - printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" - echo - done + printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" + echo + done + ) return 1 fi @@ -191,7 +194,7 @@ parse_options() { _parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf" fi - _parse_command_line "$@" + _parse_command_line "${@:-""}" } _parse_pod() { @@ -283,7 +286,7 @@ _eval_po() { _parse_config_files() { - for config_file in "$@"; do + for config_file in "${@:-""}"; do test -f "$config_file" || continue while read config_opt; do @@ -317,7 +320,7 @@ _parse_command_line() { local required_arg="" local spec="" - for opt in "$@"; do + for opt in "${@:-""}"; do if [ "$opt" = "--" -o "$opt" = "----" ]; then HAVE_EXT_ARGV=1 continue diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 27523f5b..584fbac2 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -121,19 +121,22 @@ usage_or_errors() { echo echo "Options and values after processing arguments:" echo - for opt in $(ls "$PO_DIR"); do - local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" - local varvalue="${!varname}" - if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then - if [ "$varvalue" -a "$varvalue" = "yes" ]; - then varvalue="TRUE" - else - varvalue="FALSE" + ( + cd "$PO_DIR" + for opt in *; do + local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" + eval local varvalue=\$$varname + if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then + if [ "$varvalue" -a "$varvalue" = "yes" ]; + then varvalue="TRUE" + else + varvalue="FALSE" + fi fi - fi - printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" - echo - done + printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" + echo + done + ) return 1 fi diff --git a/bin/pt-sift b/bin/pt-sift index 7953239b..0310aea8 100755 --- a/bin/pt-sift +++ b/bin/pt-sift @@ -588,7 +588,7 @@ main() { # possible to include without executing, and thus test. if [ "${0##*/}" = "$TOOL" ] \ || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then - main "$@" + main "${@:-""}" fi # ############################################################################ diff --git a/bin/pt-stalk b/bin/pt-stalk index f92f8263..b571d7e3 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -121,19 +121,22 @@ usage_or_errors() { echo echo "Options and values after processing arguments:" echo - for opt in $(ls "$PO_DIR"); do - local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" - local varvalue="${!varname}" - if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then - if [ "$varvalue" -a "$varvalue" = "yes" ]; - then varvalue="TRUE" - else - varvalue="FALSE" + ( + cd "$PO_DIR" + for opt in *; do + local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" + eval local varvalue=\$$varname + if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then + if [ "$varvalue" -a "$varvalue" = "yes" ]; + then varvalue="TRUE" + else + varvalue="FALSE" + fi fi - fi - printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" - echo - done + printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" + echo + done + ) return 1 fi @@ -193,7 +196,7 @@ parse_options() { _parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf" fi - _parse_command_line "$@" + _parse_command_line "${@:-""}" } _parse_pod() { @@ -285,7 +288,7 @@ _eval_po() { _parse_config_files() { - for config_file in "$@"; do + for config_file in "${@:-""}"; do test -f "$config_file" || continue while read config_opt; do @@ -319,7 +322,7 @@ _parse_command_line() { local required_arg="" local spec="" - for opt in "$@"; do + for opt in "${@:-""}"; do if [ "$opt" = "--" -o "$opt" = "----" ]; then HAVE_EXT_ARGV=1 continue @@ -1197,7 +1200,7 @@ if [ "${0##*/}" = "$TOOL" ] \ # Parse command line options. We must do this first so we can # see if --daemonize was specified. mk_tmpdir - parse_options "$0" "$@" + parse_options "$0" "${@:-""}" # Verify and set TRIGGER_FUNCTION based on --function. if ! set_trg_func "$OPT_FUNCTION"; then @@ -1264,7 +1267,7 @@ if [ "${0##*/}" = "$TOOL" ] \ # the child's PID. make_pid_file "$OPT_PID" $$ - main "$@" >"$OPT_LOG" 2>&1 & + main "${@:-""}" >"$OPT_LOG" 2>&1 & # Update PID file with the child's PID. # The child PID is $BASHPID but that special var is only @@ -1273,7 +1276,7 @@ if [ "${0##*/}" = "$TOOL" ] \ echo "$!" > "$OPT_PID" else [ "$OPT_STALK" ] && make_pid_file "$OPT_PID" $$ - main "$@" + main "${@:-""}" fi fi diff --git a/bin/pt-summary b/bin/pt-summary index f60f8387..6c713804 100755 --- a/bin/pt-summary +++ b/bin/pt-summary @@ -128,19 +128,22 @@ usage_or_errors() { echo echo "Options and values after processing arguments:" echo - for opt in $(ls "$PO_DIR"); do - local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" - local varvalue="${!varname}" - if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then - if [ "$varvalue" -a "$varvalue" = "yes" ]; - then varvalue="TRUE" - else - varvalue="FALSE" + ( + cd "$PO_DIR" + for opt in *; do + local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" + eval local varvalue=\$$varname + if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then + if [ "$varvalue" -a "$varvalue" = "yes" ]; + then varvalue="TRUE" + else + varvalue="FALSE" + fi fi - fi - printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" - echo - done + printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" + echo + done + ) return 1 fi diff --git a/lib/bash/parse_options.sh b/lib/bash/parse_options.sh index 40cc144d..c1999cfa 100644 --- a/lib/bash/parse_options.sh +++ b/lib/bash/parse_options.sh @@ -118,21 +118,24 @@ usage_or_errors() { echo echo "Options and values after processing arguments:" echo - for opt in $(ls "$PO_DIR"); do - local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" - local varvalue="${!varname}" - if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then - # Typeless option, like --version, so it's given/TRUE - # or not given/FALSE. - if [ "$varvalue" -a "$varvalue" = "yes" ]; - then varvalue="TRUE" - else - varvalue="FALSE" + ( + cd "$PO_DIR" + for opt in *; do + local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" + eval local varvalue=\$$varname + if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then + # Typeless option, like --version, so it's given/TRUE + # or not given/FALSE. + if [ "$varvalue" -a "$varvalue" = "yes" ]; + then varvalue="TRUE" + else + varvalue="FALSE" + fi fi - fi - printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" - echo - done + printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" + echo + done + ) return 1 fi