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