xref: /aosp_15_r20/external/one-true-awk/testdir/T.clv (revision 9a7741de182b2776d7b30d6355f2585c0780a51b)
1*9a7741deSElliott Hughes#!/bin/sh
2*9a7741deSElliott Hughesecho T.clv: check command-line variables
3*9a7741deSElliott Hughes
4*9a7741deSElliott Hughesawk=${awk-../a.out}
5*9a7741deSElliott Hughes
6*9a7741deSElliott Hughesrm -f core
7*9a7741deSElliott Hughes
8*9a7741deSElliott Hughes# stdin only, no cmdline asgn
9*9a7741deSElliott Hughesecho 'hello
10*9a7741deSElliott Hughesgoodbye' | $awk '
11*9a7741deSElliott HughesBEGIN { x=0; print x; getline; print x, $0 }
12*9a7741deSElliott Hughes' >foo1
13*9a7741deSElliott Hughesecho '0
14*9a7741deSElliott Hughes0 hello' >foo2
15*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (stdin only)'
16*9a7741deSElliott Hughes
17*9a7741deSElliott Hughes# cmdline asgn then stdin
18*9a7741deSElliott Hughesecho 'hello
19*9a7741deSElliott Hughesgoodbye' | $awk '
20*9a7741deSElliott HughesBEGIN { x=0; print x; getline; print x, $0 }
21*9a7741deSElliott Hughes' x=1 >foo1
22*9a7741deSElliott Hughesecho '0
23*9a7741deSElliott Hughes1 hello' >foo2
24*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=1 only)'
25*9a7741deSElliott Hughes
26*9a7741deSElliott Hughes# several cmdline asgn, then stdin
27*9a7741deSElliott Hughesecho 'hello
28*9a7741deSElliott Hughesgoodbye' | $awk '
29*9a7741deSElliott HughesBEGIN { x=0; print x; getline; print x, $0 }
30*9a7741deSElliott Hughes' x=1 x=2 x=3 >foo1
31*9a7741deSElliott Hughesecho '0
32*9a7741deSElliott Hughes3 hello' >foo2
33*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=3 only)'
34*9a7741deSElliott Hughes
35*9a7741deSElliott Hughes# several cmdline asgn, then file
36*9a7741deSElliott Hughesecho 'hello
37*9a7741deSElliott Hughesgoodbye' >foo
38*9a7741deSElliott Hughes$awk '
39*9a7741deSElliott HughesBEGIN { x=0; print x; getline; print x, $0 }
40*9a7741deSElliott Hughes' x=1 x=2 x=3 foo >foo1
41*9a7741deSElliott Hughesecho '0
42*9a7741deSElliott Hughes3 hello' >foo2
43*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=3 only)'
44*9a7741deSElliott Hughes
45*9a7741deSElliott Hughes# cmdline asgn then file
46*9a7741deSElliott Hughesecho 4 >foo1
47*9a7741deSElliott Hughes$awk 'BEGIN { getline; print x}' x=4 /dev/null >foo2
48*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=4 /dev/null)'
49*9a7741deSElliott Hughes
50*9a7741deSElliott Hughes#cmdline asgn then file but no read of it
51*9a7741deSElliott Hughesecho 0 >foo1
52*9a7741deSElliott Hughes$awk 'BEGIN { x=0; getline <"/dev/null"; print x}' x=5 /dev/null >foo2
53*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=5 /dev/null)'
54*9a7741deSElliott Hughes
55*9a7741deSElliott Hughes#cmdline asgn then file then read
56*9a7741deSElliott Hughesecho 'xxx
57*9a7741deSElliott Hughesyyy
58*9a7741deSElliott Hugheszzz' >foo
59*9a7741deSElliott Hughesecho '6
60*9a7741deSElliott Hughesend' >foo1
61*9a7741deSElliott Hughes$awk 'BEGIN { x=0; getline; print x}
62*9a7741deSElliott Hughes      END { print x }' x=6 foo x=end >foo2
63*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=6 /dev/null)'
64*9a7741deSElliott Hughes
65*9a7741deSElliott Hughes#cmdline asgn then file then read
66*9a7741deSElliott Hughesecho '0
67*9a7741deSElliott Hughesend' >foo1
68*9a7741deSElliott Hughes$awk 'BEGIN { x=0; getline <"/dev/null"; print x}
69*9a7741deSElliott Hughes      END { print x }' x=7 /dev/null x=end >foo2
70*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=7 /dev/null)'
71*9a7741deSElliott Hughes
72*9a7741deSElliott Hughes#cmdline asgn then file then read; _ in commandname
73*9a7741deSElliott Hughesecho '0
74*9a7741deSElliott Hughesend' >foo1
75*9a7741deSElliott Hughes$awk 'BEGIN { _=0; getline <"/dev/null"; print _}
76*9a7741deSElliott Hughes      END { print _ }' _=7A /dev/null _=end >foo2
77*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (_=7A /dev/null)'
78*9a7741deSElliott Hughes
79*9a7741deSElliott Hughes# illegal varname in commandname
80*9a7741deSElliott Hughes$awk '{ print }' 99_=foo /dev/null >foo 2>foo2
81*9a7741deSElliott Hughesgrep "can't open.*foo" foo2 >/dev/null 2>&1 || echo 'BAD: T.clv (7B: illegal varname)'
82*9a7741deSElliott Hughes
83*9a7741deSElliott Hughes# these test the new -v option:  awk ... -v a=1 -v b=2 'prog' does before BEGIN
84*9a7741deSElliott Hughes
85*9a7741deSElliott Hughesecho 123 >foo1
86*9a7741deSElliott Hughes$awk -v x=123 'BEGIN { print x }' >foo2
87*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=11)'
88*9a7741deSElliott Hughes
89*9a7741deSElliott Hughesecho 123 >foo1
90*9a7741deSElliott Hughes$awk -vx=123 'BEGIN { print x }' >foo2
91*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=11a)'
92*9a7741deSElliott Hughes
93*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
94*9a7741deSElliott Hughes$awk -v x=123 -v y=abc -v z1=10.99 'BEGIN { print x, y, z1 }' >foo2
95*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=12)'
96*9a7741deSElliott Hughes
97*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
98*9a7741deSElliott Hughes$awk -vx=123 -vy=abc -vz1=10.99 'BEGIN { print x, y, z1 }' >foo2
99*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=12a)'
100*9a7741deSElliott Hughes
101*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
102*9a7741deSElliott Hughes$awk -v x=123 -v y=abc -v z1=10.99 -- 'BEGIN { print x, y, z1 }' >foo2
103*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=12a)'
104*9a7741deSElliott Hughes
105*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }' >foo0
106*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
107*9a7741deSElliott Hughes$awk -v x=123 -v y=abc -f foo0 -v z1=10.99 >foo2
108*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=13)'
109*9a7741deSElliott Hughes
110*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }' >foo0
111*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
112*9a7741deSElliott Hughes$awk -vx=123 -vy=abc -f foo0 -vz1=10.99 >foo2
113*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=13a)'
114*9a7741deSElliott Hughes
115*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }' >foo0
116*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
117*9a7741deSElliott Hughes$awk -f foo0 -v x=123 -v y=abc -v z1=10.99 >foo2
118*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=14)'
119*9a7741deSElliott Hughes
120*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }' >foo0
121*9a7741deSElliott Hughesecho 123 abc 10.99 >foo1
122*9a7741deSElliott Hughes$awk -f foo0 -vx=123 -vy=abc -vz1=10.99 >foo2
123*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=14a)'
124*9a7741deSElliott Hughes
125*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }
126*9a7741deSElliott HughesEND { print x }' >foo0
127*9a7741deSElliott Hughesecho '123 abc 10.99
128*9a7741deSElliott Hughes4567' >foo1
129*9a7741deSElliott Hughes$awk -f foo0 -v x=123 -v y=abc -v z1=10.99 /dev/null x=4567 /dev/null >foo2
130*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=15)'
131*9a7741deSElliott Hughes
132*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }
133*9a7741deSElliott HughesEND { print x }' >foo0
134*9a7741deSElliott Hughesecho '123 abc 10.99
135*9a7741deSElliott Hughes4567' >foo1
136*9a7741deSElliott Hughes$awk -f foo0 -vx=123 -vy=abc -vz1=10.99 /dev/null x=4567 /dev/null >foo2
137*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=15a)'
138*9a7741deSElliott Hughes
139*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }
140*9a7741deSElliott HughesNR==1 { print x }' >foo0
141*9a7741deSElliott Hughesecho '123 abc 10.99
142*9a7741deSElliott Hughes4567' >foo1
143*9a7741deSElliott Hughes$awk -v x=123 -v y=abc -v z1=10.99 -f foo0 x=4567 /etc/passwd >foo2
144*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=16)'
145*9a7741deSElliott Hughes
146*9a7741deSElliott Hughesecho 'BEGIN { print x, y, z1 }
147*9a7741deSElliott HughesNR==1 { print x }' >foo0
148*9a7741deSElliott Hughesecho '123 abc 10.99
149*9a7741deSElliott Hughes4567' >foo1
150*9a7741deSElliott Hughes$awk -vx=123 -vy=abc -vz1=10.99 -f foo0 x=4567 /etc/passwd >foo2
151*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=16a)'
152*9a7741deSElliott Hughes
153*9a7741deSElliott Hughes
154*9a7741deSElliott Hughes
155*9a7741deSElliott Hughes# special chars in commandline assigned value;
156*9a7741deSElliott Hughes# have to use local echo to avoid quoting problems.
157*9a7741deSElliott Hughes
158*9a7741deSElliott Hughes./echo 'a\\b\z' >foo1
159*9a7741deSElliott Hughes./echo 'hello' | $awk '{print x}' x='\141\\\\\142\\z' >foo2
160*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=17)'
161*9a7741deSElliott Hughes
162*9a7741deSElliott Hughes./echo "a
163*9a7741deSElliott Hughesz" >foo1
164*9a7741deSElliott Hughes./echo 'hello' | $awk '{print x}' x='a\nz' >foo2
165*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=18)'
166*9a7741deSElliott Hughes
167*9a7741deSElliott Hughes# a bit circular here...
168*9a7741deSElliott Hughes$awk 'BEGIN { printf("a%c%c%cz\n", "\b", "\r", "\f") }' >foo1
169*9a7741deSElliott Hughes./echo 'hello' | $awk '{print x}' x='a\b\r\fz' >foo2
170*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.clv (x=19)'
171*9a7741deSElliott Hughes
172*9a7741deSElliott Hughes
173*9a7741deSElliott Hughes### newer -v tests
174*9a7741deSElliott Hughes
175*9a7741deSElliott Hughes
176*9a7741deSElliott Hughes$awk -vx 'BEGIN {print x}' >foo 2>&1
177*9a7741deSElliott Hughesgrep 'invalid -v option argument: x' foo >/dev/null || echo 'BAD: T.clv (x=20)'
178*9a7741deSElliott Hughes
179*9a7741deSElliott Hughes$awk -v x 'BEGIN {print x}' >foo 2>&1
180*9a7741deSElliott Hughesgrep 'invalid -v option argument: x' foo >/dev/null || echo 'BAD: T.clv (x=20a)'
181*9a7741deSElliott Hughes
182