xref: /illumos-gate/usr/src/test/util-tests/tests/awk/tests/T.clv (revision 3ee4fc2aa6b5136515cc3eed32d3c6ef33e37471)
1e6d6c189SCody Peter Mello#!/bin/bash
2e6d6c189SCody Peter Mello
3e6d6c189SCody Peter Melloif [[ -z "$AWK" || -z "$WORKDIR" ]]; then
4e6d6c189SCody Peter Mello    printf '$AWK and $WORKDIR must be set\n' >&2
5e6d6c189SCody Peter Mello    exit 1
6e6d6c189SCody Peter Mellofi
7e6d6c189SCody Peter Mello
8e6d6c189SCody Peter MelloTEMP0=$WORKDIR/test.temp.0
9e6d6c189SCody Peter MelloTEMP1=$WORKDIR/test.temp.1
10e6d6c189SCody Peter MelloTEMP2=$WORKDIR/test.temp.2
11e6d6c189SCody Peter Mello
12e6d6c189SCody Peter MelloRESULT=0
13e6d6c189SCody Peter Mello
14e6d6c189SCody Peter Mellofail() {
15e6d6c189SCody Peter Mello	echo "$1" >&2
16e6d6c189SCody Peter Mello	RESULT=1
17e6d6c189SCody Peter Mello}
18e6d6c189SCody Peter Mello
19e6d6c189SCody Peter Melloecho T.clv: check command-line variables
20e6d6c189SCody Peter Mello
21e6d6c189SCody Peter Mellorm -f core
22e6d6c189SCody Peter Mello
23e6d6c189SCody Peter Mello# stdin only, no cmdline asgn
24e6d6c189SCody Peter Melloecho 'hello
25e6d6c189SCody Peter Mellogoodbye' | $AWK '
26e6d6c189SCody Peter MelloBEGIN { x=0; print x; getline; print x, $0 }
27e6d6c189SCody Peter Mello' > $TEMP1
28e6d6c189SCody Peter Melloecho '0
29e6d6c189SCody Peter Mello0 hello' > $TEMP2
30e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (stdin only)'
31e6d6c189SCody Peter Mello
32e6d6c189SCody Peter Mello# cmdline asgn then stdin
33e6d6c189SCody Peter Melloecho 'hello
34e6d6c189SCody Peter Mellogoodbye' | $AWK '
35e6d6c189SCody Peter MelloBEGIN { x=0; print x; getline; print x, $0 }
36e6d6c189SCody Peter Mello' x=1 > $TEMP1
37e6d6c189SCody Peter Melloecho '0
38e6d6c189SCody Peter Mello1 hello' > $TEMP2
39e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=1 only)'
40e6d6c189SCody Peter Mello
41e6d6c189SCody Peter Mello# several cmdline asgn, then stdin
42e6d6c189SCody Peter Melloecho 'hello
43e6d6c189SCody Peter Mellogoodbye' | $AWK '
44e6d6c189SCody Peter MelloBEGIN { x=0; print x; getline; print x, $0 }
45e6d6c189SCody Peter Mello' x=1 x=2 x=3 > $TEMP1
46e6d6c189SCody Peter Melloecho '0
47e6d6c189SCody Peter Mello3 hello' > $TEMP2
48e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=3 only)'
49e6d6c189SCody Peter Mello
50e6d6c189SCody Peter Mello# several cmdline asgn, then file
51e6d6c189SCody Peter Melloecho 'hello
52e6d6c189SCody Peter Mellogoodbye' > $TEMP0
53e6d6c189SCody Peter Mello$AWK '
54e6d6c189SCody Peter MelloBEGIN { x=0; print x; getline; print x, $0 }
55e6d6c189SCody Peter Mello' x=1 x=2 x=3 $TEMP0 > $TEMP1
56e6d6c189SCody Peter Melloecho '0
57e6d6c189SCody Peter Mello3 hello' > $TEMP2
58e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=3 only)'
59e6d6c189SCody Peter Mello
60e6d6c189SCody Peter Mello# cmdline asgn then file
61e6d6c189SCody Peter Melloecho 4 > $TEMP1
62e6d6c189SCody Peter Mello$AWK 'BEGIN { getline; print x}' x=4 /dev/null > $TEMP2
63e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=4 /dev/null)'
64e6d6c189SCody Peter Mello
65e6d6c189SCody Peter Mello#cmdline asgn then file but no read of it
66e6d6c189SCody Peter Melloecho 0 > $TEMP1
67e6d6c189SCody Peter Mello$AWK 'BEGIN { x=0; getline <"/dev/null"; print x}' x=5 /dev/null > $TEMP2
68e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=5 /dev/null)'
69e6d6c189SCody Peter Mello
70e6d6c189SCody Peter Mello#cmdline asgn then file then read
71e6d6c189SCody Peter Melloecho 'xxx
72e6d6c189SCody Peter Melloyyy
73e6d6c189SCody Peter Mellozzz' > $TEMP0
74e6d6c189SCody Peter Melloecho '6
75e6d6c189SCody Peter Melloend' > $TEMP1
76e6d6c189SCody Peter Mello$AWK 'BEGIN { x=0; getline; print x}
77e6d6c189SCody Peter Mello      END { print x }' x=6 $TEMP0 x=end > $TEMP2
78e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=6 /dev/null)'
79e6d6c189SCody Peter Mello
80e6d6c189SCody Peter Mello#cmdline asgn then file then read
81e6d6c189SCody Peter Melloecho '0
82e6d6c189SCody Peter Melloend' > $TEMP1
83e6d6c189SCody Peter Mello$AWK 'BEGIN { x=0; getline <"/dev/null"; print x}
84e6d6c189SCody Peter Mello      END { print x }' x=7 /dev/null x=end > $TEMP2
85e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=7 /dev/null)'
86e6d6c189SCody Peter Mello
87e6d6c189SCody Peter Mello#cmdline asgn then file then read; _ in commandname
88e6d6c189SCody Peter Melloecho '0
89e6d6c189SCody Peter Melloend' > $TEMP1
90e6d6c189SCody Peter Mello$AWK 'BEGIN { _=0; getline <"/dev/null"; print _}
91e6d6c189SCody Peter Mello      END { print _ }' _=7A /dev/null _=end > $TEMP2
92e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (_=7A /dev/null)'
93e6d6c189SCody Peter Mello
94e6d6c189SCody Peter Mello# illegal varname in commandname
95e6d6c189SCody Peter Mello$AWK '{ print }' 99_=$TEMP0 /dev/null > $TEMP0 2> $TEMP2
96e6d6c189SCody Peter Mellogrep "can't open.*test.temp.0" $TEMP2 >/dev/null 2>&1 || fail 'BAD: T.clv (7B: illegal varname)'
97e6d6c189SCody Peter Mello
98e6d6c189SCody Peter Mello# these test the new -v option:  awk ... -v a=1 -v b=2 'prog' does before BEGIN
99e6d6c189SCody Peter Mello
100e6d6c189SCody Peter Melloecho 123 > $TEMP1
101e6d6c189SCody Peter Mello$AWK -v x=123 'BEGIN { print x }' > $TEMP2
102e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=11)'
103e6d6c189SCody Peter Mello
104*3ee4fc2aSCody Peter Melloecho 123 > $TEMP1
105*3ee4fc2aSCody Peter Mello$AWK -vx=123 'BEGIN { print x }' > $TEMP2
106*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=11a)'
107*3ee4fc2aSCody Peter Mello
108e6d6c189SCody Peter Melloecho 123 abc 10.99 > $TEMP1
109e6d6c189SCody Peter Mello$AWK -v x=123 -v y=abc -v z1=10.99 'BEGIN { print x, y, z1 }' > $TEMP2
110e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=12)'
111e6d6c189SCody Peter Mello
112e6d6c189SCody Peter Melloecho 123 abc 10.99 > $TEMP1
113*3ee4fc2aSCody Peter Mello$AWK -vx=123 -vy=abc -vz1=10.99 'BEGIN { print x, y, z1 }' > $TEMP2
114*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=12a)'
115*3ee4fc2aSCody Peter Mello
116*3ee4fc2aSCody Peter Melloecho 123 abc 10.99 > $TEMP1
117e6d6c189SCody Peter Mello$AWK -v x=123 -v y=abc -v z1=10.99 -- 'BEGIN { print x, y, z1 }' > $TEMP2
118e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=12a)'
119e6d6c189SCody Peter Mello
120e6d6c189SCody Peter Melloecho 'BEGIN { print x, y, z1 }' > $TEMP0
121e6d6c189SCody Peter Melloecho 123 abc 10.99 > $TEMP1
122e6d6c189SCody Peter Mello$AWK -v x=123 -v y=abc -f $TEMP0 -v z1=10.99 > $TEMP2
123e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=13)'
124e6d6c189SCody Peter Mello
125e6d6c189SCody Peter Melloecho 'BEGIN { print x, y, z1 }' > $TEMP0
126e6d6c189SCody Peter Melloecho 123 abc 10.99 > $TEMP1
127*3ee4fc2aSCody Peter Mello$AWK -vx=123 -vy=abc -f $TEMP0 -vz1=10.99 > $TEMP2
128*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=13a)'
129*3ee4fc2aSCody Peter Mello
130*3ee4fc2aSCody Peter Melloecho 'BEGIN { print x, y, z1 }' > $TEMP0
131*3ee4fc2aSCody Peter Melloecho 123 abc 10.99 > $TEMP1
132e6d6c189SCody Peter Mello$AWK -f $TEMP0 -v x=123 -v y=abc -v z1=10.99 > $TEMP2
133e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=14)'
134e6d6c189SCody Peter Mello
135*3ee4fc2aSCody Peter Melloecho 'BEGIN { print x, y, z1 }' > $TEMP0
136*3ee4fc2aSCody Peter Melloecho 123 abc 10.99 > $TEMP1
137*3ee4fc2aSCody Peter Mello$AWK -f $TEMP0 -vx=123 -vy=abc -vz1=10.99 > $TEMP2
138*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=14a)'
139*3ee4fc2aSCody Peter Mello
140e6d6c189SCody Peter Melloecho 'BEGIN { print x, y, z1 }
141e6d6c189SCody Peter MelloEND { print x }' > $TEMP0
142e6d6c189SCody Peter Melloecho '123 abc 10.99
143e6d6c189SCody Peter Mello4567' > $TEMP1
144e6d6c189SCody Peter Mello$AWK -f $TEMP0 -v x=123 -v y=abc -v z1=10.99 /dev/null x=4567 /dev/null > $TEMP2
145e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=15)'
146e6d6c189SCody Peter Mello
147e6d6c189SCody Peter Melloecho 'BEGIN { print x, y, z1 }
148*3ee4fc2aSCody Peter MelloEND { print x }' > $TEMP0
149*3ee4fc2aSCody Peter Melloecho '123 abc 10.99
150*3ee4fc2aSCody Peter Mello4567' > $TEMP1
151*3ee4fc2aSCody Peter Mello$AWK -f $TEMP0 -vx=123 -vy=abc -vz1=10.99 /dev/null x=4567 /dev/null > $TEMP2
152*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=15a)'
153*3ee4fc2aSCody Peter Mello
154*3ee4fc2aSCody Peter Melloecho 'BEGIN { print x, y, z1 }
155e6d6c189SCody Peter MelloNR==1 { print x }' > $TEMP0
156e6d6c189SCody Peter Melloecho '123 abc 10.99
157e6d6c189SCody Peter Mello4567' > $TEMP1
158e6d6c189SCody Peter Mello$AWK -v x=123 -v y=abc -v z1=10.99 -f $TEMP0 x=4567 /etc/passwd > $TEMP2
159e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=16)'
160e6d6c189SCody Peter Mello
161*3ee4fc2aSCody Peter Melloecho 'BEGIN { print x, y, z1 }
162*3ee4fc2aSCody Peter MelloNR==1 { print x }' > $TEMP0
163*3ee4fc2aSCody Peter Melloecho '123 abc 10.99
164*3ee4fc2aSCody Peter Mello4567' > $TEMP1
165*3ee4fc2aSCody Peter Mello$AWK -vx=123 -vy=abc -vz1=10.99 -f $TEMP0 x=4567 /etc/passwd > $TEMP2
166*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=16a)'
167*3ee4fc2aSCody Peter Mello
168*3ee4fc2aSCody Peter Mello
169*3ee4fc2aSCody Peter Mello
170e6d6c189SCody Peter Mello# special chars in commandline assigned value;
171e6d6c189SCody Peter Mello# have to use local echo to avoid quoting problems.
172e6d6c189SCody Peter Mello
173e6d6c189SCody Peter Mellocat <<< 'a\\b\z' > $TEMP1
174e6d6c189SCody Peter Melloecho 'hello' | $AWK '{print x}' x='\141\\\\\142\\z' > $TEMP2
175e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=17)'
176e6d6c189SCody Peter Mello
177e6d6c189SCody Peter Melloecho "a
178e6d6c189SCody Peter Melloz" > $TEMP1
179e6d6c189SCody Peter Melloecho 'hello' | $AWK '{print x}' x='a\nz' > $TEMP2
180e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=18)'
181e6d6c189SCody Peter Mello
182e6d6c189SCody Peter Mello# a bit circular here...
183e6d6c189SCody Peter Mello$AWK 'BEGIN { printf("a%c%c%cz\n", "\b", "\r", "\f") }' > $TEMP1
184e6d6c189SCody Peter Melloecho 'hello' | $AWK '{print x}' x='a\b\r\fz' > $TEMP2
185e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=19)'
186e6d6c189SCody Peter Mello
187*3ee4fc2aSCody Peter Melloecho '\' > $TEMP1
188*3ee4fc2aSCody Peter Mello$AWK -v 'x=\' 'BEGIN { print x; }' > $TEMP2
189*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.clv (x=\)'
190*3ee4fc2aSCody Peter Mello
191*3ee4fc2aSCody Peter Mello### newer -v tests
192*3ee4fc2aSCody Peter Mello
193*3ee4fc2aSCody Peter Mello
194*3ee4fc2aSCody Peter Mello$AWK -vx 'BEGIN {print x}' > $TEMP1 2>&1
195*3ee4fc2aSCody Peter Mellogrep 'invalid -v option argument: x' $TEMP1 >/dev/null || fail 'BAD: T.clv (x=20)'
196*3ee4fc2aSCody Peter Mello
197*3ee4fc2aSCody Peter Mello$AWK -v x 'BEGIN {print x}' > $TEMP1 2>&1
198*3ee4fc2aSCody Peter Mellogrep 'invalid -v option argument: x' $TEMP1 >/dev/null || fail 'BAD: T.clv (x=20a)'
199*3ee4fc2aSCody Peter Mello
200*3ee4fc2aSCody Peter Mello
201e6d6c189SCody Peter Melloexit $RESULT
202