-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprint-arguments-wrapper.sh
More file actions
executable file
·128 lines (98 loc) · 3.35 KB
/
Copy pathprint-arguments-wrapper.sh
File metadata and controls
executable file
·128 lines (98 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
SCRIPT_NAME="print-arguments-wrapper.sh"
VERSION_NUMBER="2.02"
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit 1
}
display_help ()
{
echo
echo "$SCRIPT_NAME version $VERSION_NUMBER"
echo "Copyright (c) 2011-2014 R. Diez - Licensed under the GNU AGPLv3"
echo
echo "When writing complex shell scripts, sometimes you wonder if a particular process is getting the right arguments and the right environment variables. Just prefix a command with the name of this script, and it will dump all arguments and environment variables to the console before starting the child process."
echo
echo "Syntax:"
echo " $SCRIPT_NAME <options...> command <command arguments...>"
echo
echo "Options:"
echo " --help displays this help text"
echo " --version displays the tool's version number (currently $VERSION_NUMBER)"
echo " --license prints license information"
echo
echo "Usage examples"
echo " ./$SCRIPT_NAME echo \"test\""
echo
echo "Caveat: Some shell magic may be lost in the way. Consider the following example:"
echo " ./$SCRIPT_NAME ls -la"
echo "Command 'ls' may be actually be an internal shell function or an alias to 'ls --color=auto', but that will not be taken into consideration any more when using this wrapper script. For example, the external /bin/echo tool will be executed instead of the shell built-in version."
echo
echo "Exit status: Same as the command executed."
echo
echo "Feedback: Please send feedback to rdiezmail-tools at yahoo.de"
echo
}
display_license()
{
cat - <<EOF
Copyright (c) 2011-2014 R. Diez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 as published by
the Free Software Foundation.
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 Affero General Public License version 3 for more details.
You should have received a copy of the GNU Affero General Public License version 3
along with this program. If not, see L<http://www.gnu.org/licenses/>.
EOF
}
if [ $# -lt 1 ]; then
abort "You need to specify at least an argument. Run this tool with the --help option for usage information."
fi
case "$1" in
--help)
display_help
exit 0;;
--license)
display_license
exit 0;;
--version)
echo "$VERSION_NUMBER"
exit 0;;
--*) abort "Unknown option \"$1\".";;
esac
ACTUAL_SCRIPT_NAME="$0"
echo
echo "Wrapper script \"$ACTUAL_SCRIPT_NAME\" is about to run process \"$1\" with the following environment variables:"
echo
export
echo
echo "Wrapper script \"$ACTUAL_SCRIPT_NAME\" is about to run the following process with $(($# - 1)) argument(s):"
echo
echo "- Process name: $1"
PRINT_CMD="$(printf "%q" "$1")"
if [ $# -gt 1 ]; then
declare -a ARGUMENTS=("$@")
unset 'ARGUMENTS[0]' # Remove the first element.
declare -i COUNTER=1
for arg in "${ARGUMENTS[@]}"
do
printf "%s Argument %02d: %s\\n" "-" "$COUNTER" "$arg"
COUNTER=$COUNTER+1
PRINT_CMD+=" $(printf "%q" "$arg")"
done
fi
echo
echo "The properly shell-quoted command line is:"
echo
echo "$PRINT_CMD"
echo
echo "Wrapper script \"$ACTUAL_SCRIPT_NAME\" will run process \"$1\" now:"
echo
exec -- "$@"