scoped_command revision 1.1.2.2 1 1.1.2.2 tls #!/bin/sh
2 1.1.2.2 tls #
3 1.1.2.2 tls # $NetBSD: scoped_command,v 1.1.2.2 2014/08/10 06:57:02 tls Exp $
4 1.1.2.2 tls #
5 1.1.2.2 tls # Copyright (c) 2014 The NetBSD Foundation, Inc.
6 1.1.2.2 tls # All rights reserved.
7 1.1.2.2 tls #
8 1.1.2.2 tls # This code is derived from software contributed to The NetBSD Foundation
9 1.1.2.2 tls # by Jarmo Jaakkola.
10 1.1.2.2 tls #
11 1.1.2.2 tls # Redistribution and use in source and binary forms, with or without
12 1.1.2.2 tls # modification, are permitted provided that the following conditions
13 1.1.2.2 tls # are met:
14 1.1.2.2 tls # 1. Redistributions of source code must retain the above copyright
15 1.1.2.2 tls # notice, this list of conditions and the following disclaimer.
16 1.1.2.2 tls # 2. Redistributions in binary form must reproduce the above copyright
17 1.1.2.2 tls # notice, this list of conditions and the following disclaimer in the
18 1.1.2.2 tls # documentation and/or other materials provided with the distribution.
19 1.1.2.2 tls #
20 1.1.2.2 tls # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1.2.2 tls # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.2.2 tls # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.2.2 tls # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1.2.2 tls # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1.2.2 tls # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1.2.2 tls # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.2.2 tls # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.2.2 tls # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.2.2 tls # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.2.2 tls # POSSIBILITY OF SUCH DAMAGE.
31 1.1.2.2 tls #
32 1.1.2.2 tls
33 1.1.2.2 tls set -e
34 1.1.2.2 tls
35 1.1.2.2 tls # USAGE:
36 1.1.2.2 tls # scoped_command scope cmd msg var_suffix
37 1.1.2.2 tls #
38 1.1.2.2 tls # Write to stdout a piece of Bourne Shell script with _cmd_ in specific
39 1.1.2.2 tls # _scope_. The execution of _cmd_ is bracketed by prints of "before _msg_"
40 1.1.2.2 tls # and "after _msg_, return value ${?}". If the generated script uses
41 1.1.2.2 tls # variables, __var_suffix_ is appended to their names to allow nesting of
42 1.1.2.2 tls # scripts generated this way.
43 1.1.2.2 tls #
44 1.1.2.2 tls # _scope_ should be one of: case, compound, file, for, func, subshell,
45 1.1.2.2 tls # until, while.
46 1.1.2.2 tls # _cmd_ is the command line to execute. Remember proper quoting!
47 1.1.2.2 tls # _msg_ is text that will be used inside single quotes.
48 1.1.2.2 tls # _var_suffix_ is a syntactically valid identifier name.
49 1.1.2.2 tls
50 1.1.2.2 tls # don't rely on command lists (';')
51 1.1.2.2 tls cmd="echo 'before ${3}'
52 1.1.2.2 tls ${2}
53 1.1.2.2 tls echo 'after ${3}, return value:' ${?}"
54 1.1.2.2 tls
55 1.1.2.2 tls echo "#!/bin/sh"
56 1.1.2.2 tls
57 1.1.2.2 tls [ 'func' = "${1}" ] && cat <<EOF
58 1.1.2.2 tls func()
59 1.1.2.2 tls {
60 1.1.2.2 tls echo 'before ${3}'
61 1.1.2.2 tls \${1}
62 1.1.2.2 tls echo 'after ${3}'
63 1.1.2.2 tls }
64 1.1.2.2 tls
65 1.1.2.2 tls echo 'before function'
66 1.1.2.2 tls func "${2}" "${3}" # don't rely on 'shift'
67 1.1.2.2 tls echo 'after function'
68 1.1.2.2 tls EOF
69 1.1.2.2 tls
70 1.1.2.2 tls [ 'case' = "${1}" ] && cat <<EOF
71 1.1.2.2 tls echo 'before case'
72 1.1.2.2 tls case 'a' in
73 1.1.2.2 tls a) ${cmd};;
74 1.1.2.2 tls esac
75 1.1.2.2 tls echo 'after case'
76 1.1.2.2 tls EOF
77 1.1.2.2 tls
78 1.1.2.2 tls [ 'file' = "${1}" ] && cat <<EOF
79 1.1.2.2 tls ${cmd}
80 1.1.2.2 tls EOF
81 1.1.2.2 tls
82 1.1.2.2 tls [ 'while' = "${1}" ] && cat <<EOF
83 1.1.2.2 tls echo 'before while'
84 1.1.2.2 tls cond_${4}='true true false'
85 1.1.2.2 tls while \${cond_${4}}
86 1.1.2.2 tls do
87 1.1.2.2 tls cond_${4}="\${cond_${4}#* }"
88 1.1.2.2 tls ${cmd}
89 1.1.2.2 tls done
90 1.1.2.2 tls echo 'after while'
91 1.1.2.2 tls EOF
92 1.1.2.2 tls
93 1.1.2.2 tls [ 'until' = "${1}" ] && cat <<EOF
94 1.1.2.2 tls echo 'before until'
95 1.1.2.2 tls cond_${4}='false false true'
96 1.1.2.2 tls until \${cond_${4}}
97 1.1.2.2 tls do
98 1.1.2.2 tls cond_${4}="\${cond_${4}#* }"
99 1.1.2.2 tls ${cmd}
100 1.1.2.2 tls done
101 1.1.2.2 tls echo 'after until'
102 1.1.2.2 tls EOF
103 1.1.2.2 tls
104 1.1.2.2 tls [ 'for' = "${1}" ] && cat <<EOF
105 1.1.2.2 tls echo 'before for'
106 1.1.2.2 tls for i_${4} in 1 2
107 1.1.2.2 tls do
108 1.1.2.2 tls ${cmd}
109 1.1.2.2 tls done
110 1.1.2.2 tls echo 'after for'
111 1.1.2.2 tls EOF
112 1.1.2.2 tls
113 1.1.2.2 tls [ 'subshell' = "${1}" ] && cat <<EOF
114 1.1.2.2 tls (
115 1.1.2.2 tls echo 'subshell start'
116 1.1.2.2 tls ${cmd}
117 1.1.2.2 tls echo 'subshell end'
118 1.1.2.2 tls )
119 1.1.2.2 tls EOF
120 1.1.2.2 tls
121 1.1.2.2 tls [ 'compound' = "${1}" ] && cat <<EOF
122 1.1.2.2 tls {
123 1.1.2.2 tls echo 'compound start'
124 1.1.2.2 tls ${cmd};
125 1.1.2.2 tls echo 'compound end'
126 1.1.2.2 tls }
127 1.1.2.2 tls EOF
128 1.1.2.2 tls
129 1.1.2.2 tls exit 0
130